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.
 
 
 
 

122 lines
3.5 KiB

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