get_your_rep 0.1.9 → 1.0.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.
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ module GetYourRep
3
+ # Base class for external responses
4
+ class Base
5
+ # Accepts an options hash and creates setter and getter attribute methods from the hash keys.
6
+ # After initializing methods the values are added to the attributes.
7
+ # Taken with gratitude from davidbella https://gist.github.com/davidbella/6918455
8
+ def initialize(options = {})
9
+ options.each do |attr, value|
10
+ create_setters_from_opts(attr)
11
+ create_getters_from_opts(attr)
12
+ add_value_to_attr(attr, value)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def add_value_to_attr(attr, value)
19
+ send("#{attr}=".to_sym, value)
20
+ end
21
+
22
+ def create_getters_from_opts(attr)
23
+ self.class.send(:define_method, attr.to_sym) do
24
+ instance_variable_get('@' + attr.to_s)
25
+ end
26
+ end
27
+
28
+ def create_setters_from_opts(attr)
29
+ self.class.send(:define_method, "#{attr}=".to_sym) do |val|
30
+ instance_variable_set('@' + attr.to_s, val)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+ require_relative './base'
3
+
4
+ module GetYourRep
5
+ # Parses office location information from a Google response.
6
+ class GoogleOffice < Base
7
+ # These attributes are defined so they can be called even
8
+ # if they are not defined during initialization.
9
+ attr_accessor :line1, :line2
10
+
11
+ # Builds the hash to pass as constructor options for a new OfficeLocation.
12
+ def build_hash
13
+ office_hash = {}
14
+ office_hash[:type] = determine_office_type
15
+ office_hash[:line_1] = line1
16
+ office_hash[:line_2] = line2
17
+ office_hash[:city] = city.capitalize
18
+ office_hash[:state] = state.upcase
19
+ office_hash[:zip] = zip
20
+ office_hash
21
+ end
22
+
23
+ private
24
+
25
+ def determine_office_type
26
+ if line1.downcase =~
27
+ /(state|house|senate|assembly|capitol|dirksen|reyburn|rayburn|legislative|legislature|government)+/
28
+
29
+ 'capitol'
30
+ else
31
+ 'district'
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+ require_relative './base'
3
+
4
+ module GetYourRep
5
+ # Parses rep information from Google response.
6
+ class GoogleRep < Base
7
+ # An array of phone numbers. Default = []
8
+ attr_accessor :phones
9
+ # An array of email addresses. Default = []
10
+ attr_accessor :emails
11
+ # An array of websites. Default = []
12
+ attr_accessor :urls
13
+ # An array of office addresses to be passes to a GoogleOffice instance for parsing. Default = []
14
+ attr_accessor :address
15
+ # An array of social media handles. Default = []
16
+ attr_accessor :channels
17
+ # Passes the GetYourRep::Representative constructor hash to different methods for assembly.
18
+ attr_accessor :hash
19
+ # The office name must be parsed from an array passed to the build_hash method.
20
+ # the office_array is an array of hashes and is a sibling to the officials array from which
21
+ # the rep info is parsed.
22
+ attr_accessor :office_name
23
+
24
+ # Sets array attributes to empty arrays and and builds other attributes from options hash.
25
+ def initialize(options = {})
26
+ self.phones = []
27
+ self.emails = []
28
+ self.urls = []
29
+ self.address = []
30
+ self.channels = []
31
+ super
32
+ end
33
+
34
+ # Build a hash from attributes to be passed to the GetYourRep::Representative constructor method.
35
+ def build_hash(offices_array, index)
36
+ find_office_name(offices_array, index)
37
+ self.hash = { name: name,
38
+ office: office_name,
39
+ party: party,
40
+ phones: phones,
41
+ office_locations: office_locations,
42
+ email: emails,
43
+ url: url,
44
+ photo: photoUrl }
45
+ parse_channels
46
+ hash
47
+ end
48
+
49
+ private
50
+
51
+ def find_office_name(offices_array, index)
52
+ office_data_hash = offices_array.detect { |office| office['officialIndices'].include?(index) }
53
+ self.office_name = office_data_hash['name']
54
+ end
55
+
56
+ def office_locations
57
+ address.map do |office|
58
+ location = GoogleOffice.new(office)
59
+ office_hash = location.build_hash
60
+ OfficeLocation.new(office_hash)
61
+ end
62
+ end
63
+
64
+ def url
65
+ urls.first
66
+ end
67
+
68
+ # Parses social media handles and adds them to the Delegation object.
69
+ def parse_channels
70
+ channels.each do |channel|
71
+ hash[channel['type'].downcase.to_sym] = channel['id']
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ require_relative './base'
3
+
4
+ module GetYourRep
5
+ # Parses office location information from Open States responses.
6
+ class OpenStatesOffice < Base
7
+ # These accessor attributes are parsed from the :address attribute created at instantiation.
8
+ attr_accessor :line_1, :line_2, :city, :state, :zip
9
+
10
+ # Assign elements of the address array to office attributes.
11
+ def parse_address
12
+ address_to_ary
13
+ state_and_zip = address.pop.split(' ')
14
+ self.state = state_and_zip.first
15
+ self.zip = state_and_zip.last
16
+ self.city = address.pop
17
+ self.line_1 = address.shift
18
+ self.line_2 = address[0]
19
+ end
20
+
21
+ # Splits the address string into an array for hash assembly.
22
+ def address_to_ary
23
+ self.address = address.gsub("\n", ', ')
24
+ self.address = address.split(', ')
25
+ return if name.casecmp('capitol office') || name.casecmp('district office')
26
+ address.unshift(name)
27
+ end
28
+
29
+ # Builds the hash to pass as constructor options for a new OfficeLocation.
30
+ def build_hash
31
+ parse_address
32
+ { type: type,
33
+ line_1: line_1,
34
+ line_2: line_2,
35
+ city: city,
36
+ state: state,
37
+ zip: zip }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+ require_relative './base'
3
+
4
+ module GetYourRep
5
+ # Parses rep information from Open States responses.
6
+ class OpenStatesRep < Base
7
+ # These attributes hold arrays of information, and are empty arrays by default.
8
+ attr_reader :email, :phones
9
+
10
+ def initialize(options = {}) # :nodoc:
11
+ super
12
+ @email = []
13
+ @phones = []
14
+ end
15
+
16
+ # Build a hash from attributes to be passed to the GetYourRep::Representative constructor method.
17
+ def build_hash
18
+ { name: full_name.split(', ').reverse.join(' '),
19
+ office: office,
20
+ party: party,
21
+ phones: phones - [nil],
22
+ office_locations: office_locations,
23
+ email: email - [nil],
24
+ url: url,
25
+ photo: photo_url,
26
+ committees: committees }
27
+ end
28
+
29
+ private
30
+
31
+ def office
32
+ "#{state.upcase} #{chamber.capitalize} Chamber, #{district}"
33
+ end
34
+
35
+ def office_locations
36
+ offices.map do |office|
37
+ location = OpenStatesOffice.new(office)
38
+ @email << office['email']
39
+ @phones << office['phone']
40
+ office_hash = location.build_hash
41
+ OfficeLocation.new(office_hash)
42
+ end
43
+ end
44
+
45
+ # Parses committee involvement.
46
+ def committees
47
+ roles.map { |role| role['committee'] } - [nil]
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module GetYourRep
3
+ VERSION = '1.0.0' # :nodoc:
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get_your_rep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - msimonborg
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-22 00:00:00.000000000 Z
11
+ date: 2017-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -44,6 +44,20 @@ dependencies:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: 1.4.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: colorize
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 0.8.1
47
61
  - !ruby/object:Gem::Dependency
48
62
  name: pry
49
63
  requirement: !ruby/object:Gem::Requirement
@@ -104,37 +118,56 @@ dependencies:
104
118
  requirements:
105
119
  - - "~>"
106
120
  - !ruby/object:Gem::Version
107
- version: '10'
121
+ version: '12.0'
108
122
  type: :development
109
123
  prerelease: false
110
124
  version_requirements: !ruby/object:Gem::Requirement
111
125
  requirements:
112
126
  - - "~>"
113
127
  - !ruby/object:Gem::Version
114
- version: '10'
115
- description: Get your rep with Google's Civic Information API using your address and
116
- API key. Also access the Open States API for additional information about state
117
- legislators. Formerly get-your-rep. New API, new guts, faster performance, more
118
- extensible, better results. Zip codes are geocoded for improved data returns. Namespaced
119
- to avoid naming collisions. Generally better programmed. Changes to API may persist
120
- until 1.0. Documentation will be updated soon.
128
+ version: '12.0'
129
+ description: Get your rep(s) from Google's Civic Information API and Open States.
130
+ Formerly get-your-rep.
121
131
  email: msimonborg@gmail.com
122
- executables: []
132
+ executables:
133
+ - reps
123
134
  extensions: []
124
135
  extra_rdoc_files: []
125
136
  files:
126
- - lib/core_extensions/array/get_your_rep_mutations.rb
127
- - lib/core_extensions/hash/get_your_rep_mutations.rb
137
+ - ".gitignore"
138
+ - ".rubocop.yml"
139
+ - Gemfile
140
+ - LICENSE
141
+ - README.md
142
+ - Rakefile
143
+ - bin/console
144
+ - bin/setup
145
+ - config.reek
146
+ - exe/reps
147
+ - get_your_rep.gemspec
128
148
  - lib/get_your_rep.rb
149
+ - lib/get_your_rep/cli.rb
129
150
  - lib/get_your_rep/delegation.rb
151
+ - lib/get_your_rep/errors.rb
152
+ - lib/get_your_rep/get_your_rep_module.rb
130
153
  - lib/get_your_rep/google.rb
154
+ - lib/get_your_rep/office_location.rb
131
155
  - lib/get_your_rep/open_states.rb
156
+ - lib/get_your_rep/patriotic.rb
132
157
  - lib/get_your_rep/representative.rb
158
+ - lib/get_your_rep/responses/base.rb
159
+ - lib/get_your_rep/responses/google_office.rb
160
+ - lib/get_your_rep/responses/google_rep.rb
161
+ - lib/get_your_rep/responses/open_states_office.rb
162
+ - lib/get_your_rep/responses/open_states_rep.rb
163
+ - lib/get_your_rep/version.rb
133
164
  homepage: https://github.com/msimonborg/get-your-rep
134
165
  licenses:
135
166
  - MIT
136
167
  metadata: {}
137
- post_install_message:
168
+ post_install_message: If you are upgrading from get_your_rep ~> 0.1, version 1.0 may
169
+ break your app. It is a good idea to upgrade, but please read the documentation
170
+ first!
138
171
  rdoc_options: []
139
172
  require_paths:
140
173
  - lib
@@ -1,23 +0,0 @@
1
- ##
2
- # Core class extensions.
3
- module CoreExtensions
4
- ##
5
- # Extend the core Array class.
6
- module Array
7
- ##
8
- # Patches for GetYourRep namespace.
9
- module GetYourRepMutations
10
-
11
- # Converts an Array to a Delegation
12
- def to_del
13
- GetYourRep::Delegation.new(self)
14
- end
15
-
16
- # Converts an Array to a Representative
17
- def to_rep
18
- GetYourRep::Representative[to_h]
19
- end
20
-
21
- end
22
- end
23
- end
@@ -1,24 +0,0 @@
1
- ##
2
- # Core class extensions.
3
- module CoreExtensions
4
- ##
5
- # Extend the core Hash class.
6
- module Hash
7
- ##
8
- # Patches for GetYourRep namespace.
9
- module GetYourRepMutations
10
-
11
- # Wraps a Hash in a Delegation
12
- def to_del
13
- del = GetYourRep::Delegation.new
14
- del << self
15
- end
16
-
17
- # Converts a Hash to a Representative
18
- def to_rep
19
- GetYourRep::Representative[self]
20
- end
21
-
22
- end
23
- end
24
- end