Election results in the London Borough of Sutton.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

123 lignes
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. property :votes_2010, Integer
  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. property :votes_2010, Integer
  84. property :votes_2005, Integer
  85. property :percent_2005, Float
  86. belongs_to :party
  87. belongs_to :constituency
  88. end
  89. class Constituency
  90. include DataMapper::Resource
  91. property :id, Serial
  92. property :name, String, :required => true
  93. has n, :wards, :order => ['name']
  94. has n, :parliamentcandidates, :order => ['surname']
  95. end
  96. DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db.sqlite3")
  97. DataMapper.auto_upgrade!