Module: GetYourRep
- Included in:
- CLI, Google, OpenStates
- Defined in:
- lib/get_your_rep.rb,
lib/get_your_rep/cli.rb,
lib/get_your_rep/errors.rb,
lib/get_your_rep/google.rb,
lib/get_your_rep/version.rb,
lib/get_your_rep/patriotic.rb,
lib/get_your_rep/delegation.rb,
lib/get_your_rep/open_states.rb,
lib/get_your_rep/representative.rb,
lib/get_your_rep/responses/base.rb,
lib/get_your_rep/office_location.rb,
lib/get_your_rep/get_your_rep_module.rb,
lib/get_your_rep/responses/google_rep.rb,
lib/get_your_rep/responses/google_office.rb,
lib/get_your_rep/responses/open_states_rep.rb,
lib/get_your_rep/responses/open_states_office.rb
Overview
GetYourRep module functions.
Defined Under Namespace
Modules: CLI, Errors, Google, OpenStates, Patriotic Classes: Base, Delegation, GoogleOffice, GoogleRep, OfficeLocation, OpenStatesOffice, OpenStatesRep, Representative
Constant Summary collapse
- VERSION =
:nodoc:
'1.0.2'
Class Method Summary collapse
-
.address_is_a_zip?(address) ⇒ Boolean
Check if an address value is a zip code or a full address.
-
.all_reps(address) ⇒ Object
Get all reps using Google for national legislators, and OpenStates for state reps.
-
.convert_int_address_to_string(address) ⇒ Object
Converts an integer to a string in case a zip was input as an int.
-
.convert_zip_to_address(address) ⇒ Object
Convert a zip code to a street address using Geocoder to convert zip to coordinates and coordinates to an address.
-
.get_coordinates(address) ⇒ Object
Geocode an address and returns the coordinates.
-
.handle_reps_not_found_error ⇒ Object
Creates an empty delegation if no reps were found.
-
.parse_address(address) ⇒ Object
Parses a String address and prepares for an HTTP request.
-
.trim_street_number(address) ⇒ Object
If a street number is a range reduce it to one number.
Class Method Details
.address_is_a_zip?(address) ⇒ Boolean
Check if an address value is a zip code or a full address.
17 18 19 20 21 22 23 24 |
# File 'lib/get_your_rep/get_your_rep_module.rb', line 17 def address_is_a_zip?(address) address = convert_int_address_to_string(address) if address.is_a?(String) address =~ /^\d{5}(-\d{4})?$/ ? true : false else address_type_error end end |
.all_reps(address) ⇒ Object
Get all reps using Google for national legislators, and OpenStates for state reps.
10 11 12 13 14 |
# File 'lib/get_your_rep/get_your_rep_module.rb', line 10 def all_reps(address) @delegation = Google.all_reps(address, congress_only: true) open_states = OpenStates.all_reps(address) @delegation.merge(open_states) end |
.convert_int_address_to_string(address) ⇒ Object
Converts an integer to a string in case a zip was input as an int.
62 63 64 65 66 67 68 |
# File 'lib/get_your_rep/get_your_rep_module.rb', line 62 def convert_int_address_to_string(address) if address.is_a?(Integer) format('0%o', address) else address end end |
.convert_zip_to_address(address) ⇒ Object
Convert a zip code to a street address using Geocoder to convert zip to coordinates and coordinates to an address.
28 29 30 31 32 |
# File 'lib/get_your_rep/get_your_rep_module.rb', line 28 def convert_zip_to_address(address) @coordinates = Geocoder.coordinates(address) address = Geocoder.address(@coordinates) trim_street_number(address) end |
.get_coordinates(address) ⇒ Object
Geocode an address and returns the coordinates.
52 53 54 55 56 57 58 59 |
# File 'lib/get_your_rep/get_your_rep_module.rb', line 52 def get_coordinates(address) address = convert_int_address_to_string(address) if address.is_a?(String) Geocoder.coordinates(address) else address_type_error end end |
.handle_reps_not_found_error ⇒ Object
Creates an empty delegation if no reps were found.
71 72 73 74 75 |
# File 'lib/get_your_rep/get_your_rep_module.rb', line 71 def handle_reps_not_found_error reps_not_found_error puts response Delegation.new end |
.parse_address(address) ⇒ Object
Parses a String address and prepares for an HTTP request. Accepts Strings and Integers as args.
42 43 44 45 46 47 48 49 |
# File 'lib/get_your_rep/get_your_rep_module.rb', line 42 def parse_address(address) address = convert_int_address_to_string(address) if address.is_a?(String) address.tr(',', '').split.join('%20') else address_type_error end end |
.trim_street_number(address) ⇒ Object
If a street number is a range reduce it to one number.
35 36 37 38 39 |
# File 'lib/get_your_rep/get_your_rep_module.rb', line 35 def trim_street_number(address) address = address.split(' ') address[0] = address[0].split('-').shift address.join(' ') end |