Election results in the London Borough of Sutton.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

120 regels
3.4 KiB

  1. require 'dm-core'
  2. require 'dm-validations'
  3. require 'dm-timestamps'
  4. require 'pat'
  5. class Postcode
  6. include DataMapper::Resource
  7. # Postcode natural key, uppercase with space, eg. "SM1 1EA"
  8. property :postcode, String, :key => true
  9. property :created_at, DateTime
  10. property :updated_at, DateTime
  11. property :lat, Float, :required => true
  12. property :lng, Float, :required => true
  13. property :district_name, String, :required => true
  14. property :district_code, String, :required => true
  15. property :ward_name, String, :required => true
  16. property :ward_code, String, :required => true
  17. def postcode=(postcode)
  18. attribute_set(:postcode, postcode.strip.upcase)
  19. end
  20. def self.finder(postcode)
  21. postcode = postcode.strip.upcase
  22. if o = self.get(postcode)
  23. return o
  24. end
  25. result = Pat.get(postcode)
  26. unless result.code == 404
  27. # cache API result
  28. return self.create(
  29. :postcode => postcode,
  30. :lat => result['geo']['lat'],
  31. :lng => result['geo']['lng'],
  32. :district_name => result['administrative']['district']['title'],
  33. :district_code => result['administrative']['district']['uri'].match(/.+\/(.+)$/)[1],
  34. :ward_name => result['administrative']['ward']['title'],
  35. :ward_code => result['administrative']['ward']['uri'].match(/.+\/(.+)$/)[1]
  36. )
  37. else
  38. # invalid postcode
  39. nil
  40. end
  41. end
  42. end
  43. class Ward
  44. include DataMapper::Resource
  45. property :id, Serial
  46. property :ons_id, String, :required => true
  47. property :name, String, :required => true
  48. property :constituency_id, Integer, :required => true
  49. has n, :councilcandidates, :order => ['surname']
  50. belongs_to :constituency
  51. def self.slugify(name)
  52. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  53. end
  54. end
  55. class Party
  56. include DataMapper::Resource
  57. property :id, Serial
  58. property :name, String, :required => true
  59. has n, :councilcandidates, :order => ['surname']
  60. has n, :parliamentcandidates, :order => ['surname']
  61. end
  62. class Councilcandidate
  63. include DataMapper::Resource
  64. property :id, Serial
  65. property :ward_id, Integer, :required => true
  66. property :party_id, Integer, :required => true
  67. property :forenames, String, :required => true
  68. property :surname, String, :required => true
  69. property :address, String, :length => 200
  70. property :postcode, String, :required => true
  71. belongs_to :party
  72. belongs_to :ward
  73. end
  74. class Parliamentcandidate
  75. include DataMapper::Resource
  76. property :id, Serial
  77. property :constituency_id, Integer, :required => true
  78. property :party_id, Integer, :required => true
  79. property :forenames, String, :required => true
  80. property :surname, String, :required => true
  81. property :address, String, :length => 200
  82. property :postcode, String
  83. belongs_to :party
  84. belongs_to :constituency
  85. end
  86. class Constituency
  87. include DataMapper::Resource
  88. property :id, Serial
  89. property :name, String, :required => true
  90. has n, :wards, :order => ['name']
  91. has n, :parliamentcandidates, :order => ['surname']
  92. end
  93. DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db.sqlite3")
  94. DataMapper.auto_upgrade!