google-civic-ruby 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ecf478592957b5fc9111b2f913dbc780eeae1fc9
4
- data.tar.gz: d6ad081f1789c5b78160b765ed184be3c6de9ad1
3
+ metadata.gz: 7ee66524f2360ca52385c2b44fe98dea5a2c081e
4
+ data.tar.gz: 7e6fa47faf2d296f20bc152e1abb2dd536b7a62d
5
5
  SHA512:
6
- metadata.gz: 348df3a75d235aa9fb9090364b8f9d350801fa1511a6559a3f56d1510c16a8e10f624bc3a1c45a6ff2686f5f93166f120798ed51ed2e9e1a89a9622d3448c1e2
7
- data.tar.gz: 38a8a77b5263029ad0821ad7dbf63baaf266a69374c82e62166cd916ce8efa1525fda257f68898c8f98f43b2b6f93d37bc25433b69b24618bd89fb10f263d6fc
6
+ metadata.gz: 2dc06c70e8b06e0e580f6dd4819b1dc948135926bc020bc2041ed06300a49867a9baf6eb12f643016f08f63a01d6e6d1489f3f9c953ba43837284eb80f82c1d2
7
+ data.tar.gz: c1af46a146bb53d83a160161db814e07a7cd077e06ebc1f15ae2b37278648277e55e1afd45602eca13a562d2367a0ca5ad257a1e338771e74df1c29bafab6f98
data/README.md CHANGED
@@ -21,7 +21,7 @@ Or install it yourself as:
21
21
 
22
22
  ## Contributing
23
23
 
24
- 1. Fork it ( http://github.com/<my-github-username>/google-civic-api-ruby/fork )
24
+ 1. Fork it ( http://github.com/CodingZeal/google-civic-api-ruby/fork )
25
25
  2. Create your feature branch (`git checkout -b my-new-feature`)
26
26
  3. Commit your changes (`git commit -am 'Add some feature'`)
27
27
  4. Push to the branch (`git push origin my-new-feature`)
@@ -17,12 +17,11 @@ Gem::Specification.new do |spec|
17
17
  spec.require_paths = ["lib"]
18
18
 
19
19
  spec.add_dependency "multi_json", "~> 1.10"
20
- spec.add_dependency "hashie", "~> 3.0"
21
20
  spec.add_dependency "faraday", "~> 0.9"
22
21
  spec.add_dependency "faraday_middleware", "~> 0.9"
23
22
 
24
23
  spec.add_development_dependency "bundler", "~> 1.3"
25
24
  spec.add_development_dependency "rspec", "~> 2.14"
26
- spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-debugger"
27
26
  spec.add_development_dependency "webmock", "~> 1.18"
28
27
  end
data/lib/google-civic.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'google-civic/version'
2
+ require "google-civic/representation"
2
3
  require 'google-civic/election'
3
4
  require 'google-civic/elections'
4
5
  # require 'google-civic/address'
@@ -6,4 +7,10 @@ require 'google-civic/elections'
6
7
  require 'google-civic/client'
7
8
  require 'google-civic/router'
8
9
  require 'google-civic/requester'
9
- require "google-civic/connection"
10
+ require "google-civic/connection"
11
+ require "google-civic/address"
12
+ require "google-civic/channel"
13
+ require "google-civic/division"
14
+ require "google-civic/office"
15
+ require "google-civic/official"
16
+ require "google-civic/representative_info"
@@ -0,0 +1,6 @@
1
+ module Google
2
+ module Civic
3
+ class Address < Representation
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,31 @@
1
+ module Google
2
+ module Civic
3
+ class Channel < Representation
4
+ end
5
+
6
+ class Facebook < Channel
7
+ def url
8
+ "http://facebook.com/#{self.id}"
9
+ end
10
+ end
11
+
12
+ class Twitter < Channel
13
+ def url
14
+ "http://twitter.com/#{self.id}"
15
+ end
16
+ end
17
+
18
+ class GooglePlus < Channel
19
+ def url
20
+ "https://plus.google.com/#{self.id}"
21
+ end
22
+ end
23
+
24
+ class YouTube < Channel
25
+ def url
26
+ "http://youtube.com/#{self.id}"
27
+ end
28
+ end
29
+ end
30
+ end
31
+
@@ -10,6 +10,10 @@ module Google
10
10
  Elections.for_zipcode(zipcode, self)
11
11
  end
12
12
 
13
+ def representative_info(address)
14
+ RepresentativeInfo.for_address(address, self)
15
+ end
16
+
13
17
  def voter_info(address)
14
18
  VoterInfo.for_address(address, self)
15
19
  end
@@ -39,4 +39,4 @@ module Google
39
39
  end
40
40
  end
41
41
  end
42
- end
42
+ end
@@ -0,0 +1,7 @@
1
+ module Google
2
+ module Civic
3
+ class Division < Representation
4
+ has_many :offices
5
+ end
6
+ end
7
+ end
@@ -1,14 +1,9 @@
1
- require "multi_json"
2
- require "hashie"
3
-
4
1
  module Google
5
2
  module Civic
6
- class Election < Hashie::Dash
7
- include Hashie::Extensions::Dash::IndifferentAccess
8
-
9
- property :id, required: true
10
- property :name, required: true
11
- property :electionDay, required: true
3
+ class Election < Representation
4
+ def election_day
5
+ self["electionDay"]
6
+ end
12
7
  end
13
8
  end
14
9
  end
@@ -1,5 +1,3 @@
1
- require "multi_json"
2
-
3
1
  module Google
4
2
  module Civic
5
3
  module Elections
@@ -0,0 +1,7 @@
1
+ module Google
2
+ module Civic
3
+ class Office < Representation
4
+ has_many :officials
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ module Google
2
+ module Civic
3
+ class Official < Representation
4
+ def address
5
+ end
6
+
7
+ def addresses
8
+ @__addresses ||= self["address"].map { |address| Address.new(address) }
9
+ end
10
+
11
+ def channels
12
+ @__channels ||= self["channels"].map { |channel| channel_class.new(channel) }
13
+ end
14
+
15
+ private
16
+
17
+ def channel_class
18
+ Object.const_get("Google::Civic::#{channel.fetch("type")}")
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,47 @@
1
+ module Google
2
+ module Civic
3
+ class Representation < OpenStruct
4
+ class << self
5
+ def has_many(association, options={})
6
+ build_getter_for_ids(association)
7
+ build_getter_for_collection(association, options)
8
+ end
9
+
10
+ private
11
+
12
+ def build_getter_for_ids(association)
13
+ singularized_association = __singularized(association)
14
+
15
+ class_eval %{
16
+ def #{singularized_association}_ids
17
+ self["#{singularized_association}Ids"] || []
18
+ end
19
+ }
20
+ end
21
+
22
+ def build_getter_for_collection(association, options)
23
+ class_eval %{
24
+ def #{association}
25
+ @__#{association} ||= #{__singularized(association)}_ids.map do |id|
26
+ #{options[:class_name] || __singularized(association).capitalize}.new(__find_#{association}_by_id(id), @root_scope)
27
+ end
28
+ end
29
+
30
+ def __find_#{association}_by_id(id)
31
+ @root_scope.fetch("#{association}").fetch(id)
32
+ end
33
+ }
34
+ end
35
+
36
+ def __singularized(word)
37
+ word.to_s.gsub(/s\z/, '')
38
+ end
39
+ end
40
+
41
+ def initialize(representation, root_scope = nil)
42
+ @root_scope = root_scope || representation
43
+ super(representation)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,34 @@
1
+ module Google
2
+ module Civic
3
+ class RepresentativeInfo < Representation
4
+
5
+ def self.for_address(address, client)
6
+ representative_info_from_response response(address, client)
7
+ end
8
+
9
+ def divisions
10
+ @divisions ||= self["divisions"].keys.map do |key|
11
+ division_rep = self["divisions"].fetch(key).merge(id: key)
12
+ Division.new(division_rep, @root_scope)
13
+ end
14
+ end
15
+
16
+ def offices
17
+ @offices ||= self["offices"].keys.map do |key|
18
+ office_rep = self["offices"].fetch(key).merge(id: key)
19
+ Office.new(office_rep, @root_scope)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def self.representative_info_from_response(response)
26
+ RepresentativeInfo.new(response.body)
27
+ end
28
+
29
+ def self.response(address, client)
30
+ client.request(:representative_info, address: address)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -4,11 +4,16 @@ module Google
4
4
 
5
5
  BASE_URL = "https://www.googleapis.com"
6
6
  BASE_PATH = "/civicinfo/v1"
7
+
7
8
  ROUTES = {
8
9
  elections: {
9
10
  method: :get,
10
- path: "/elections",
11
- klass: Election
11
+ path: "/elections"
12
+ },
13
+
14
+ representative_info: {
15
+ method: :post,
16
+ path: "/representatives/lookup"
12
17
  }
13
18
  }
14
19
 
@@ -25,11 +30,6 @@ module Google
25
30
  def path_for(kind)
26
31
  [BASE_PATH, route_map_for(kind).fetch(:path)].join('')
27
32
  end
28
-
29
- def klass_for(kind)
30
- route_map_for(kind).fetch(:klass)
31
- end
32
- alias :class_for :klass_for
33
33
  end
34
34
  end
35
35
  end
@@ -1,5 +1,5 @@
1
1
  module Google
2
2
  module Civic
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
- end
5
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ module Google
4
+ module Civic
5
+ describe Division do
6
+ subject { described_class.new(first_division, representation) }
7
+ let(:representation) { MultiJson.load(load_fixture("representative_info")) }
8
+ let(:first_division) {
9
+ divisions = representation.fetch("divisions")
10
+ divisions.keys.map do |key|
11
+ divisions.fetch(key)
12
+ end.select do |division|
13
+ !division["officeIds"].nil? and !division["officeIds"].empty?
14
+ end.first
15
+ }
16
+
17
+ it { expect(subject.name).to be_a_kind_of String }
18
+ it { expect(subject.office_ids).to be_a_kind_of Array }
19
+ it { expect(subject.offices.first).to be_a_kind_of Office }
20
+ end
21
+ end
22
+ end
@@ -1,13 +1,14 @@
1
1
  require "spec_helper"
2
- require "multi_json"
3
2
 
4
- module Google::Civic
5
- describe Election do
6
- subject { Election.new(response) }
7
- let(:response) { MultiJson.load(load_fixture("elections"))["elections"][0] }
3
+ module Google
4
+ module Civic
5
+ describe Election do
6
+ subject { Election.new(response) }
7
+ let(:response) { MultiJson.load(load_fixture("elections"))["elections"][0] }
8
8
 
9
- its(:id) { should_not be_nil }
10
- its(:name) { should_not be_nil }
11
- its(:electionDay) { should_not be_nil }
9
+ its(:id) { should_not be_nil }
10
+ its(:name) { should_not be_nil }
11
+ its(:election_day) { should_not be_nil }
12
+ end
12
13
  end
13
14
  end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ module Google
4
+ module Civic
5
+ describe Office do
6
+ include_context "a connection to the client"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ module Google
4
+ module Civic
5
+ describe Official do
6
+ pending
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ module Google
4
+ module Civic
5
+ describe RepresentativeInfo do
6
+ include_context "a connection to the client"
7
+ let(:method) { :post }
8
+ let(:kind) { "representative_info" }
9
+ let(:path) { "representatives/lookup?key=#{api_key}&address=#{address}" }
10
+
11
+ describe ".for_address" do
12
+ subject { RepresentativeInfo.for_address(address, client) }
13
+ let(:address) { "1263 Pacific Ave. Kansas City KS" }
14
+
15
+ it { expect(subject.divisions).to have(8).record }
16
+ it { expect(subject.offices).to have(19).record }
17
+ it { expect(subject.officials).to have(20).record }
18
+ end
19
+ end
20
+ end
21
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ require 'rspec'
3
3
  require 'rspec/autorun'
4
4
  require 'pry'
5
5
  require 'webmock/rspec'
6
+ require "multi_json"
6
7
 
7
8
  PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
8
9
  $LOAD_PATH << File.join(PROJECT_ROOT, 'lib')
@@ -15,4 +16,4 @@ RSpec.configure do |config|
15
16
  config.include Requests
16
17
  end
17
18
 
18
- require 'google-civic'
19
+ require 'google-civic'
@@ -1,5 +1,5 @@
1
1
  module Requests
2
- def stub_request_for(fixture, path)
3
- stub_request(:get, "https://www.googleapis.com/civicinfo/v1/#{path}").to_return(status: 200, body: Fixtures.load_fixture(fixture), headers: {})
2
+ def stub_request_for(method, fixture, path)
3
+ stub_request(method, "https://www.googleapis.com/civicinfo/v1/#{path}").to_return(status: 200, body: Fixtures.load_fixture(fixture), headers: {})
4
4
  end
5
- end
5
+ end
@@ -9,8 +9,11 @@ module Google
9
9
  base_url: Router::BASE_URL
10
10
  }
11
11
  }
12
+ let(:method) { :get }
13
+ let(:path) { "elections?zipcode=#{zipcode}&key=#{api_key}" }
14
+ let(:kind) { "elections" }
12
15
 
13
- before { stub_request_for("elections", "elections?zipcode=#{zipcode}&key=#{api_key}") }
16
+ before { stub_request_for(method, kind, path) }
14
17
  end
15
18
  end
16
- end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-civic-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Coding ZEAL
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-04 00:00:00.000000000 Z
12
+ date: 2014-06-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -25,20 +25,6 @@ dependencies:
25
25
  - - ~>
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.10'
28
- - !ruby/object:Gem::Dependency
29
- name: hashie
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - ~>
33
- - !ruby/object:Gem::Version
34
- version: '3.0'
35
- type: :runtime
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ~>
40
- - !ruby/object:Gem::Version
41
- version: '3.0'
42
28
  - !ruby/object:Gem::Dependency
43
29
  name: faraday
44
30
  requirement: !ruby/object:Gem::Requirement
@@ -96,7 +82,7 @@ dependencies:
96
82
  - !ruby/object:Gem::Version
97
83
  version: '2.14'
98
84
  - !ruby/object:Gem::Dependency
99
- name: pry
85
+ name: pry-debugger
100
86
  requirement: !ruby/object:Gem::Requirement
101
87
  requirements:
102
88
  - - '>='
@@ -138,16 +124,27 @@ files:
138
124
  - Rakefile
139
125
  - google-civic-ruby.gemspec
140
126
  - lib/google-civic.rb
127
+ - lib/google-civic/address.rb
128
+ - lib/google-civic/channel.rb
141
129
  - lib/google-civic/client.rb
142
130
  - lib/google-civic/connection.rb
131
+ - lib/google-civic/division.rb
143
132
  - lib/google-civic/election.rb
144
133
  - lib/google-civic/elections.rb
134
+ - lib/google-civic/office.rb
135
+ - lib/google-civic/official.rb
136
+ - lib/google-civic/representation.rb
137
+ - lib/google-civic/representative_info.rb
145
138
  - lib/google-civic/requester.rb
146
139
  - lib/google-civic/router.rb
147
140
  - lib/google-civic/version.rb
148
141
  - spec/google-civic/client_spec.rb
142
+ - spec/google-civic/division_spec.rb
149
143
  - spec/google-civic/election_spec.rb
150
144
  - spec/google-civic/elections_spec.rb
145
+ - spec/google-civic/office_spec.rb
146
+ - spec/google-civic/official_spec.rb
147
+ - spec/google-civic/representative_info_spec.rb
151
148
  - spec/google-civic/requester_spec.rb
152
149
  - spec/spec_helper.rb
153
150
  - spec/support/fixtures/elections.json
@@ -182,8 +179,12 @@ specification_version: 4
182
179
  summary: Ruby wrapper for the Google Civic API
183
180
  test_files:
184
181
  - spec/google-civic/client_spec.rb
182
+ - spec/google-civic/division_spec.rb
185
183
  - spec/google-civic/election_spec.rb
186
184
  - spec/google-civic/elections_spec.rb
185
+ - spec/google-civic/office_spec.rb
186
+ - spec/google-civic/official_spec.rb
187
+ - spec/google-civic/representative_info_spec.rb
187
188
  - spec/google-civic/requester_spec.rb
188
189
  - spec/spec_helper.rb
189
190
  - spec/support/fixtures/elections.json