Class: GetYourRep
- Inherits:
-
Object
- Object
- GetYourRep
- Defined in:
- lib/get-your-rep.rb
Overview
The GetYourRep class has a collection of class methods that retrieve your elected representatives from the Google Civic Information API, parse it from JSON, and assemble it into a usable Ruby array-like object called a Delegation. You must configure your own Google API key as an environment variable using the constant name ‘GOOGLE_API_KEY’ in order for this gem to work.
Class Method Summary collapse
-
.all(address) ⇒ Object
Returns reps in Congress and State Legislature, plus Governor and Lieutenant Governor.
-
.get_rep ⇒ Object
Sets parameters for and executes Google API request.
-
.now(address, level: 'national', role: 'representative') ⇒ Object
Call GetYourRep#now to initiate a chain of method calls on self that will instantiate a Delegation object and return it.
-
.parse_channels ⇒ Object
Parses social media handles and adds them to the Delegation object.
-
.parse_rep ⇒ Object
Parses the JSON response and assembles it into a Delegation object, except for social media attributes, which are handles by ::parse_channels.
-
.voter_address(address) ⇒ Object
Parses a String address and prepares for an HTTP request.
Class Method Details
.all(address) ⇒ Object
Returns reps in Congress and State Legislature, plus Governor and Lieutenant Governor.
36 37 38 39 40 41 42 43 |
# File 'lib/get-your-rep.rb', line 36 def self.all(address) @reps = now(address, level: 'national', role: 'representative') @reps << now(address, level: 'national', role: 'senator') @reps << now(address, level: 'state', role: 'representative') @reps << now(address, level: 'state', role: 'senator') @reps << now(address, level: 'state', role: 'executive') @reps << now(address, level: 'state', role: 'vice executive') end |
.get_rep ⇒ Object
Sets parameters for and executes Google API request.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/get-your-rep.rb', line 53 def self.get_rep level = case @level when 'national' 'country' when 'state' 'administrativeArea1' end role = case @role when 'representative' 'legislatorLowerBody' when 'senator' 'legislatorUpperBody' when 'executive' 'headOfGovernment' when 'vice executive' 'deputyHeadOfGovernment' end url = "https://www.googleapis.com/civicinfo/v2/representatives?address=#{@address}%20&includeOffices=true&levels=#{level}&roles=#{role}&fields=offices%2Cofficials&key=#{@api_key}" HTTParty.get(url).parsed_response end |
.now(address, level: 'national', role: 'representative') ⇒ Object
Call GetYourRep#now to initiate a chain of method calls on self that will instantiate a Delegation object and return it. Supported options for :level are “national”(default) and “state”. Supported options for :role are “representative”(default), “senator”, “executive” and “vice executive”.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/get-your-rep.rb', line 16 def self.now(address, level: 'national', role: 'representative') @address = voter_address(address) @api_key ||= ENV['GOOGLE_API_KEY'] @level = level @role = role @response = get_rep if @response.empty? puts 'Could not find your rep. Your location search might be too broad, try refining it.' return Delegation.new elsif @response['error'] puts 'Error message received. Confirm and re-enter your address and check your parameters.' puts @response return Delegation.new end parse_rep parse_channels if @officials.any? { |official| official['channels'] } @delegation end |
.parse_channels ⇒ Object
Parses social media handles and adds them to the Delegation object.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/get-your-rep.rb', line 103 def self.parse_channels i = 0 @officials.each do |official| next unless official['channels'] official['channels'].each do |channel| @delegation[i][channel['type'].downcase.to_sym] = channel['id'] end i += 1 end end |
.parse_rep ⇒ Object
Parses the JSON response and assembles it into a Delegation object, except for social media attributes, which are handles by ::parse_channels.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/get-your-rep.rb', line 77 def self.parse_rep @delegation = Delegation.new @officials = @response['officials'] i = 0 @officials.each do |official| @delegation[i] = {} @delegation[i][:name] = official['name'] @delegation[i][:office] = @response['offices'].first['name'] @delegation[i][:party] = official['party'] @delegation[i][:phone] = official['phones'].first @delegation[i][:address_1] = official['address'].first['line1'] @delegation[i][:address_2] = official['address'].first['line2'] @delegation[i][:address_3] = official['address'].first['line3'] @delegation[i][:address_city] = official['address'].first['city'].capitalize @delegation[i][:address_state] = official['address'].first['state'] @delegation[i][:address_zip] = official['address'].first['zip'] @delegation[i][:email] = official['emails'] if official['emails'] @delegation[i][:url] = official['urls'].first @delegation[i][:photo] = official['photoUrl'] i += 1 end end |
.voter_address(address) ⇒ Object
Parses a String address and prepares for an HTTP request. Accepts Strings and Integers as args.
46 47 48 49 50 |
# File 'lib/get-your-rep.rb', line 46 def self.voter_address(address) address = '0%o' % address if address.is_a?(Integer) raise "Entry must be of types String or Integer" if !address.is_a?(String) address.tr(',', '').split.join('%20') end |