get-your-rep 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 400808cc3fb333208695e812d8f422d0d7e2c124
4
- data.tar.gz: 40a5530e00c1b791b98f07cf0ed1cadd929c6da8
3
+ metadata.gz: 9b0541c0a6fc00771ce898665eb5dcd1fc0c73a5
4
+ data.tar.gz: 98b6170d9fa0d37868c826218343002706f43bb2
5
5
  SHA512:
6
- metadata.gz: bca14b9f2f1044a41f887a941dfba903d921100c05b8434bf2b2230103dc5a66fceeb6f81560e713f65343e908efe1793d4a2d1f6d689bc94f3c9865ec24478c
7
- data.tar.gz: 40500826b049505345ad1f70483158367766a31aa8e67dd423e71762f2ea020eb43689168d5fca93aebbacc7ba15ac184c615f0c55adcc4a8eddb23d967365e7
6
+ metadata.gz: d1f40c6e1fc82f6019a00f798eee45ceb30e364e0f2dbc43e5f06bec2ddcde4f866aecec9553408867a38c363edd61c6a76a770c6647c6fc6ac74b4c36f06a76
7
+ data.tar.gz: ebad1ed2bb360ec70761381b36e38562b50bac2f0385fced2ffa2bbcece533401386538fafff33a6b4057d8b1f42a86ecf617e83317f31dd031e837fd0065aa2
data/lib/get-your-rep.rb CHANGED
@@ -1,15 +1,14 @@
1
1
  # require 'pry'
2
2
  require 'httparty'
3
- require_relative 'getyourrep/rep.rb'
3
+ require_relative 'get-your-rep/delegation.rb'
4
4
 
5
- # The GetYourRep class has a collection of class methods that retrieve a representative
5
+ # The GetYourRep class has a collection of class methods that retrieve your elected representatives
6
6
  # from the Google Civic Information API, parse it from JSON, and assemble it into a usable
7
- # Ruby hash-like object called a Rep.
7
+ # Ruby array-like object called a Delegation.
8
8
  class GetYourRep
9
9
 
10
- # Call GetYourRep#now to initiate a chain of method calls on self that will instantiate a Rep
11
- # object and return it.
12
- def self.now(address, level = 'national', role = 'representative')
10
+ # Call GetYourRep#now to initiate a chain of method calls on self that will instantiate a Delegation object and return it.
11
+ def self.now(address, level: 'national', role: 'representative')
13
12
  @address = voter_address(address)
14
13
  @api_key = ENV['GOOGLE_API_KEY']
15
14
  @level = level
@@ -17,20 +16,20 @@ class GetYourRep
17
16
  @response = get_rep
18
17
  if @response.empty?
19
18
  puts 'Could not find your rep. Your location search might be too broad, try refining it.'
20
- return nil
19
+ return Delegation.new
21
20
  elsif @response['error']
22
21
  puts 'Error message received. Confirm and re-enter your address and check your parameters.'
23
- return @response
22
+ return Delegation.new
24
23
  end
25
24
  parse_rep
26
- parse_channels if @official['channels']
27
- @rep
25
+ parse_channels if @officials.any? { |official| official['channels'] }
26
+ @reps
28
27
  end
29
28
 
30
- # Parses a String address and prepares for an HTTP request.
29
+ # Parses a String address and prepares for an HTTP request. Accepts Strings and Integers as args.
31
30
  def self.voter_address(address)
32
- # TODO try to convert FixNum submissions to String
33
- raise "Entry must be of type String" if !address.is_a?(String)
31
+ address = '0%o' % address if address.is_a?(Integer)
32
+ raise "Entry must be of types String or Integer" if !address.is_a?(String)
34
33
  address.tr(',', '').split.join('%20')
35
34
  end
36
35
 
@@ -38,9 +37,9 @@ class GetYourRep
38
37
  def self.get_rep
39
38
  level = case @level
40
39
  when 'national'
41
- "country"
40
+ 'country'
42
41
  when 'state'
43
- "administrativeArea1"
42
+ 'administrativeArea1'
44
43
  end
45
44
 
46
45
  role = case @role
@@ -53,31 +52,42 @@ class GetYourRep
53
52
  HTTParty.get(url).parsed_response
54
53
  end
55
54
 
56
- # Parses the JSON response and assembles it into a Rep object, except for social media attributes,
55
+ # Parses the JSON response and assembles it into a Delegation object, except for social media attributes,
57
56
  # which are handles by ::parse_channels.
58
57
  def self.parse_rep
59
- @rep = Rep.new
60
- @official = @response['officials'].first
58
+ @reps = Delegation.new
59
+ @officials = @response['officials']
61
60
 
62
- @rep['name'] = @official['name']
63
- @rep['office'] = @response['offices'].first['name']
64
- @rep['party'] = @official['party']
65
- @rep['phone'] = @official['phones'].first
66
- @rep['address_1'] = @official['address'].first['line1']
67
- @rep['address_2'] = @official['address'].first['line2']
68
- @rep['address_3'] = @official['address'].first['line3']
69
- @rep['address_city'] = @official['address'].first['city'].capitalize
70
- @rep['address_state'] = @official['address'].first['state']
71
- @rep['address_zip'] = @official['address'].first['zip']
72
- @rep['email'] = @official['emails'] if @official['emails']
73
- @rep['url'] = @official['urls'].first
74
- @rep['photo'] = @official['photoUrl']
61
+ i = 0
62
+ @officials.each do |official|
63
+ @reps[i] = {}
64
+
65
+ @reps[i][:name] = official['name']
66
+ @reps[i][:office] = @response['offices'].first['name']
67
+ @reps[i][:party] = official['party']
68
+ @reps[i][:phone] = official['phones'].first
69
+ @reps[i][:address_1] = official['address'].first['line1']
70
+ @reps[i][:address_2] = official['address'].first['line2']
71
+ @reps[i][:address_3] = official['address'].first['line3']
72
+ @reps[i][:address_city] = official['address'].first['city'].capitalize
73
+ @reps[i][:address_state] = official['address'].first['state']
74
+ @reps[i][:address_zip] = official['address'].first['zip']
75
+ @reps[i][:email] = official['emails'] if official['emails']
76
+ @reps[i][:url] = official['urls'].first
77
+ @reps[i][:photo] = official['photoUrl']
78
+ i += 1
79
+ end
75
80
  end
76
81
 
77
- # Parses social media handles and adds them to the Rep object.
82
+ # Parses social media handles and adds them to the Delegation object.
78
83
  def self.parse_channels
79
- @official['channels'].each do |channel|
80
- @rep[channel['type'].downcase] = channel['id']
84
+ i = 0
85
+ @officials.each do |official|
86
+ next unless official['channels']
87
+ official['channels'].each do |channel|
88
+ @reps[i][channel['type'].downcase.to_sym] = channel['id']
89
+ end
90
+ i += 1
81
91
  end
82
92
  end
83
93
  end
@@ -0,0 +1,46 @@
1
+ # The Delegation class inherits from Array and describes methods that can be called on a Delegation for ease of display and database query.
2
+ class Delegation < Array
3
+
4
+ # Overloads the << operator so the receiving object is not overwritten as an Array, and there are no nested Delegations.
5
+ def <<(value)
6
+ if value.is_a?(Delegation) || value.is_a?(Array)
7
+ self.replace(self + value)
8
+ else
9
+ super
10
+ end
11
+ end
12
+
13
+ # Collects the first names of every rep in the Delegation.
14
+ def first_names
15
+ self.map { |rep| rep[:name].split[0..-2].join(' ')}
16
+ end
17
+
18
+ # Collects the last names of every rep in the Delegation.
19
+ def last_names
20
+ self.map { |rep| rep[:name].split.last }
21
+ end
22
+
23
+ # Maps attributes to a simple array for easy printing, iteration, and display. It uses #each rather than #map so it can skip over nil values without mapping them.
24
+ def business_cards
25
+ card = []
26
+ i = 0
27
+ self.each do |rep|
28
+ card[i] = []
29
+ rep.each do |key, value|
30
+ next if value.nil?
31
+ case key
32
+ when :facebook
33
+ card[i] << "#{key.to_s.capitalize}: #{value}"
34
+ when :twitter
35
+ card[i] << "#{key.to_s.capitalize}: #{value}"
36
+ when :youtube
37
+ card[i] << "#{key.to_s.capitalize}: #{value}"
38
+ else
39
+ card[i] << "#{value}"
40
+ end
41
+ end
42
+ i += 1
43
+ end
44
+ card
45
+ end
46
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get-your-rep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - msimonborg
@@ -39,14 +39,14 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Get your rep with Google's Civic Information API using your address and
42
- API key. Work in progress, check back for updates!
42
+ API key.
43
43
  email: msimonborg@gmail.com
44
44
  executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - lib/get-your-rep.rb
49
- - lib/getyourrep/rep.rb
49
+ - lib/get-your-rep/delegation.rb
50
50
  homepage: https://github.com/msimonborg/get-your-rep
51
51
  licenses:
52
52
  - MIT
@@ -1,22 +0,0 @@
1
- # The Rep class inherits from Hash and describes methods that can be called on a Rep
2
- class Rep < Hash
3
-
4
- # business_card can be called on an instance of Rep to map it's attributes to an array for easy printing, iteration, and display. It uses #each so it can skip over nil values without mapping them.
5
- def business_card
6
- card = []
7
- self.each do |key, value|
8
- next if value.nil?
9
- case key
10
- when 'facebook'
11
- card << "#{key.capitalize}: #{value}"
12
- when 'twitter'
13
- card << "#{key.capitalize}: #{value}"
14
- when 'youtube'
15
- card << "#{key.capitalize}: #{value}"
16
- else
17
- card << "#{value}"
18
- end
19
- end
20
- card
21
- end
22
- end