get-your-rep 0.0.3.0 → 0.1.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 +4 -4
- data/lib/get-your-rep.rb +41 -25
- data/lib/getyourrep/rep.rb +22 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 400808cc3fb333208695e812d8f422d0d7e2c124
|
4
|
+
data.tar.gz: 40a5530e00c1b791b98f07cf0ed1cadd929c6da8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bca14b9f2f1044a41f887a941dfba903d921100c05b8434bf2b2230103dc5a66fceeb6f81560e713f65343e908efe1793d4a2d1f6d689bc94f3c9865ec24478c
|
7
|
+
data.tar.gz: 40500826b049505345ad1f70483158367766a31aa8e67dd423e71762f2ea020eb43689168d5fca93aebbacc7ba15ac184c615f0c55adcc4a8eddb23d967365e7
|
data/lib/get-your-rep.rb
CHANGED
@@ -1,29 +1,40 @@
|
|
1
|
+
# require 'pry'
|
1
2
|
require 'httparty'
|
3
|
+
require_relative 'getyourrep/rep.rb'
|
4
|
+
|
5
|
+
# The GetYourRep class has a collection of class methods that retrieve a representative
|
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.
|
2
8
|
class GetYourRep
|
3
|
-
attr_accessor :rep, :rep_info, :address, :api_key, :level, :role
|
4
9
|
|
10
|
+
# Call GetYourRep#now to initiate a chain of method calls on self that will instantiate a Rep
|
11
|
+
# object and return it.
|
5
12
|
def self.now(address, level = 'national', role = 'representative')
|
6
13
|
@address = voter_address(address)
|
7
14
|
@api_key = ENV['GOOGLE_API_KEY']
|
8
15
|
@level = level
|
9
16
|
@role = role
|
10
|
-
@
|
11
|
-
if @
|
12
|
-
puts 'Could not find your rep.'
|
17
|
+
@response = get_rep
|
18
|
+
if @response.empty?
|
19
|
+
puts 'Could not find your rep. Your location search might be too broad, try refining it.'
|
13
20
|
return nil
|
14
|
-
elsif @
|
15
|
-
puts 'Error message received. Confirm and re-enter your address'
|
16
|
-
return @
|
21
|
+
elsif @response['error']
|
22
|
+
puts 'Error message received. Confirm and re-enter your address and check your parameters.'
|
23
|
+
return @response
|
17
24
|
end
|
18
25
|
parse_rep
|
19
|
-
parse_channels
|
20
|
-
@
|
26
|
+
parse_channels if @official['channels']
|
27
|
+
@rep
|
21
28
|
end
|
22
29
|
|
30
|
+
# Parses a String address and prepares for an HTTP request.
|
23
31
|
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)
|
24
34
|
address.tr(',', '').split.join('%20')
|
25
35
|
end
|
26
36
|
|
37
|
+
# Sets parameters for and executes Google API request.
|
27
38
|
def self.get_rep
|
28
39
|
level = case @level
|
29
40
|
when 'national'
|
@@ -42,26 +53,31 @@ class GetYourRep
|
|
42
53
|
HTTParty.get(url).parsed_response
|
43
54
|
end
|
44
55
|
|
56
|
+
# Parses the JSON response and assembles it into a Rep object, except for social media attributes,
|
57
|
+
# which are handles by ::parse_channels.
|
45
58
|
def self.parse_rep
|
46
|
-
@
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
59
|
+
@rep = Rep.new
|
60
|
+
@official = @response['officials'].first
|
61
|
+
|
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']
|
60
75
|
end
|
61
76
|
|
77
|
+
# Parses social media handles and adds them to the Rep object.
|
62
78
|
def self.parse_channels
|
63
|
-
@
|
64
|
-
@
|
79
|
+
@official['channels'].each do |channel|
|
80
|
+
@rep[channel['type'].downcase] = channel['id']
|
65
81
|
end
|
66
82
|
end
|
67
83
|
end
|
@@ -0,0 +1,22 @@
|
|
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
|
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.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- msimonborg
|
@@ -39,13 +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
|
42
|
+
API key. Work in progress, check back for updates!
|
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
50
|
homepage: https://github.com/msimonborg/get-your-rep
|
50
51
|
licenses:
|
51
52
|
- MIT
|
@@ -58,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
59
|
requirements:
|
59
60
|
- - ">="
|
60
61
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
62
|
+
version: 1.9.3
|
62
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
64
|
requirements:
|
64
65
|
- - ">="
|