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.
 
 
 
 

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