class GetYourRep
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.
Public Class Methods
Returns reps in Congress and State Legislature, plus Governor and Lieutenant Governor.
# File lib/get-your-rep.rb, line 37 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
Sets parameters for and executes Google API request.
# File lib/get-your-rep.rb, line 54 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
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”.
# File lib/get-your-rep.rb, line 17 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
Parses social media handles and adds them to the Delegation
object.
# 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
Parses the JSON response and assembles it into a Delegation
object, except for social media attributes, which are handles by ::parse_channels
.
# File lib/get-your-rep.rb, line 78 def self.parse_rep @delegation = Delegation.new @officials = @response['officials'] i = 0 @officials.each do |official| @delegation[i] = Representative[ :name, official['name'], :office, @response['offices'].first['name'], :party, official['party'], :phone, official['phones'].first, :address_1, official['address'].first['line1'], :address_2, official['address'].first['line2'], :address_3, "#{official['address'].first['city'].capitalize}, #{official['address'].first['state']} #{official['address'].first['zip']}", :email, official['emails'], :url, official['urls'].first, :photo, official['photoUrl'] ] i += 1 end end
Parses a String address and prepares for an HTTP request. Accepts Strings and Integers as args.
# File lib/get-your-rep.rb, line 47 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