get_your_rep 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: edd6679eeba76ca59cc68de91dadad4ae1508a12
4
- data.tar.gz: 62bfca1c1df86adc03f0a8ef762e3b858981173e
3
+ metadata.gz: 16f153f283c9e1fa3758cd00a95076e318d2a6d8
4
+ data.tar.gz: 3fc24259b1df82dd1f487ad198d8335901509084
5
5
  SHA512:
6
- metadata.gz: 73543286e2682625a93909c8c2220fa3cd4cb7a3e206ee95a673d013021cfb762ac86eefa75c2a516bdb2f6f01d5b1de3d702134edbc37da520a31cd05b35551
7
- data.tar.gz: 7d0c25329a29390f1a7265e20e08154170c89854b39b466bbb5070c4882dfbdebe755f49c14dd3690cb8b99c24c7a1851c701d09dc1b0395888accb4a9c78a3a
6
+ metadata.gz: 82bcfba877ab98d8240ce1125d93b08a7978f5375100c1cf40067bc4bab00c88e2d809ac7a4ea87fe55117f79a6ef48508e7340a217a8381ddb9bdce6796a10f
7
+ data.tar.gz: e81f56dd850bea280e7b14d7ead80bb3a8f07d7680185de083cceac0805de859dfeaee3ff87649059c1c1ab1b0e638a2ebaf3725132e9c620bf1923546d1455f
@@ -2,14 +2,9 @@ AllCops:
2
2
  TargetRubyVersion: 2.3
3
3
 
4
4
  Metrics/LineLength:
5
- Max: 120
6
-
7
- Metrics/BlockLength:
8
- Exclude:
9
- - 'lib/get_your_rep/patriotic.rb'
5
+ Max: 100
10
6
 
11
7
  Metrics/MethodLength:
12
- Max: 15
13
8
  Exclude:
14
9
  - 'test/*'
15
10
  - 'lib/get_your_rep/cli.rb'
@@ -21,20 +16,18 @@ Style/Documentation:
21
16
  - 'test/**/*'
22
17
 
23
18
  Metrics/ModuleLength:
24
- Max: 200
25
-
26
- Metrics/ClassLength:
27
- Max: 200
19
+ Exclude:
20
+ - 'lib/get_your_rep/cli.rb'
28
21
 
29
22
  Metrics/AbcSize:
30
- Enabled: false
23
+ Exclude:
24
+ - 'lib/get_your_rep/cli.rb'
25
+ - 'lib/get_your_rep/patriotic.rb'
31
26
 
32
27
  Metrics/CyclomaticComplexity:
33
- Enabled: false
28
+ Exclude:
29
+ - 'lib/get_your_rep/cli.rb'
34
30
 
35
31
  Style/MultilineOperationIndentation:
36
32
  Exclude:
37
33
  - 'lib/get_your_rep/patriotic.rb'
38
-
39
- Style/HashSyntax:
40
- Enabled: true
@@ -11,7 +11,7 @@ TooManyStatements:
11
11
 
12
12
  TooManyMethods:
13
13
  exclude:
14
- - ['lib/get_your_rep/representative.rb'] # #business_card is the offender. TODO: write method or delete.
14
+ - ['lib/get_your_rep/representative.rb']
15
15
 
16
16
  UncommunicativeVariableName:
17
17
  exclude:
@@ -17,14 +17,15 @@ Gem::Specification.new do |s|
17
17
  s.add_development_dependency 'minitest', '~> 5.10', '>= 5.10.1'
18
18
  s.add_development_dependency 'minitest-reporters', '~> 1.1', '>= 1.1.13'
19
19
  s.add_development_dependency 'rake', '~> 12.0'
20
- s.summary = "Get yo' rep!"
21
- s.description = "Get your rep(s) from Google's Civic Information API and Open States. Formerly get-your-rep."
22
- s.authors = ['msimonborg']
23
- s.email = 'msimonborg@gmail.com'
24
- s.homepage = 'https://github.com/msimonborg/get-your-rep'
25
- s.license = 'MIT'
26
- s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
- s.bindir = 'exe'
28
- s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ s.summary = 'Get yo\' rep!'
21
+ s.description = 'Get your rep(s) from Google\'s Civic Information API and Open States. '\
22
+ 'Formerly get-your-rep.'
23
+ s.authors = ['msimonborg']
24
+ s.email = 'msimonborg@gmail.com'
25
+ s.homepage = 'https://github.com/msimonborg/get-your-rep'
26
+ s.license = 'MIT'
27
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ s.bindir = 'exe'
29
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
30
  s.require_paths = ['lib']
30
31
  end
@@ -6,6 +6,7 @@ require 'colorize'
6
6
 
7
7
  require 'get_your_rep/version'
8
8
  require 'get_your_rep/errors'
9
+ require 'get_your_rep/associations'
9
10
  require 'get_your_rep/get_your_rep_module'
10
11
  require 'get_your_rep/patriotic'
11
12
  require 'get_your_rep/cli'
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GetYourRep
4
+ # Methods for making associations between objects.
5
+ module Associations
6
+ def add_child(other:, model:, children:, parent:, error:)
7
+ if other.is_a?(model)
8
+ instance_variable_get("@#{children}") << other
9
+ other.send("#{parent}=", self) unless other.send(parent.to_s) == self
10
+ else
11
+ error.call
12
+ end
13
+ end
14
+
15
+ def add_parent(other:, model:, parent:, child:, error:)
16
+ if other.is_a?(model)
17
+ instance_variable_set("@#{parent}", other)
18
+ other.send("add_#{child}", self) unless other.send("#{child}s").include?(self)
19
+ else
20
+ error.call
21
+ end
22
+ end
23
+ end
24
+ end
@@ -109,7 +109,8 @@ but you can try a zip code if you insist.".yellow
109
109
 
110
110
  def display_reps
111
111
  rep_list
112
- puts "Select a rep number for more detail. Type 'list' for the list or 'exit' for a new query.".yellow
112
+ puts 'Select a rep number for more detail. '\
113
+ "Type 'list' for the list or 'exit' for a new query.".yellow
113
114
  rep_loop
114
115
  end
115
116
 
@@ -4,8 +4,11 @@ module GetYourRep
4
4
  class Delegation
5
5
  include Enumerable
6
6
  include GetYourRep::Errors
7
+ include GetYourRep::Associations
7
8
 
8
- # Set the reps attribute, which will hold an array of Representative objects, to an empty array by default.
9
+ # Set the reps attribute, which will hold an array of Representative objects,
10
+ # to an empty array by default.
11
+ #
9
12
  def initialize
10
13
  @reps = []
11
14
  end
@@ -38,12 +41,11 @@ module GetYourRep
38
41
 
39
42
  # Add a Representative to the reps array. Only accepts a Representative as an argument.
40
43
  def add_rep(other)
41
- if other.is_a?(Representative)
42
- @reps << other
43
- other.delegation = self unless other.delegation == self
44
- else
45
- not_a_rep_error
46
- end
44
+ add_child other: other,
45
+ model: Representative,
46
+ children: :reps,
47
+ parent: :delegation,
48
+ error: -> { not_a_rep_error }
47
49
  end
48
50
 
49
51
  # Collects the OfficeLocations of all associated reps.
@@ -12,8 +12,8 @@ module GetYourRep
12
12
 
13
13
  # Raise when a rep cannot be found with the given parameters.
14
14
  def reps_not_found
15
- "Error message received. Some reps were not found. Confirm your address and check your parameters. \
16
- Try a full address if you used a zip."
15
+ 'Error message received. Some reps were not found. '\
16
+ 'Confirm your address and check your parameters. Try a full address if you used a zip.'
17
17
  end
18
18
  end
19
19
 
@@ -37,17 +37,24 @@ module GetYourRep
37
37
  end
38
38
 
39
39
  def ask_google_api(congress_only: nil)
40
- if congress_only
41
- url = "https://www.googleapis.com/civicinfo/v2/representatives?address=#{address}%20&includeOffices=true&\
42
- levels=country&roles=legislatorLowerBody&roles=legislatorUpperBody&fields=offices%2Cofficials&key=#{API_KEY}"
43
- else
44
- url = "https://www.googleapis.com/civicinfo/v2/representatives?address=#{address}%20&includeOffices=true&\
45
- levels=country&levels=administrativeArea1&roles=legislatorLowerBody&roles=legislatorUpperBody&roles=headOfGovernment\
46
- &roles=deputyHeadOfGovernment&fields=offices%2Cofficials&key=#{API_KEY}"
47
- end
40
+ url = congress_only ? congress_only_url : all_reps_url
48
41
  self.response = HTTParty.get(url).parsed_response
49
42
  end
50
43
 
44
+ def all_reps_url
45
+ # rubocop:disable Style/StringLiterals
46
+ "https://www.googleapis.com/civicinfo/v2/representatives?address=#{address}%20&"\
47
+ "includeOffices=true&levels=country&levels=administrativeArea1&roles=legislatorLower"\
48
+ "Body&roles=legislatorUpperBody&roles=headOfGovernment&roles=deputyHeadOfGovernment&"\
49
+ "fields=offices%2Cofficials&key=#{API_KEY}"
50
+ end
51
+
52
+ def congress_only_url
53
+ "https://www.googleapis.com/civicinfo/v2/representatives?address=#{address}%20&"\
54
+ "includeOffices=true&levels=country&roles=legislatorLowerBody&roles=legislatorUpper"\
55
+ "Body&fields=offices%2Cofficials&key=#{API_KEY}"
56
+ end # rubocop:enable Style/StringLiterals
57
+
51
58
  def deliver_response
52
59
  if response.empty? || response['error']
53
60
  handle_reps_not_found_error
@@ -3,6 +3,7 @@ module GetYourRep
3
3
  # Office information. Belongs to Representative.
4
4
  class OfficeLocation
5
5
  include GetYourRep::Errors
6
+ include GetYourRep::Associations
6
7
 
7
8
  # Each instance belongs to a Represenative, which has many OfficeLocations.
8
9
  attr_accessor :rep
@@ -26,12 +27,11 @@ module GetYourRep
26
27
 
27
28
  # Set the rep attribute to an instance of a Representative, and make two-way association.
28
29
  def rep=(other)
29
- if other.is_a?(Representative)
30
- @rep = other
31
- other.add_office_location(self) unless other.office_locations.include?(self)
32
- else
33
- not_a_rep_error
34
- end
30
+ add_parent other: other,
31
+ model: Representative,
32
+ parent: :rep,
33
+ child: :office_location,
34
+ error: -> { not_a_rep_error }
35
35
  end
36
36
 
37
37
  # Display self attributes for CLI.
@@ -33,43 +33,26 @@ module GetYourRep
33
33
  def self.welcome
34
34
  puts ''
35
35
  2.times do
36
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e '.red + 'y o u r '.white + 'r e p '.blue +
37
- '- '.red + '- '.white + '- '.blue
38
- sleep(0.05)
39
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e t '.red + 'y o r '.white + 'r e p '.blue +
40
- '- '.red + '- '.white + '- '.blue
41
- sleep(0.05)
42
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e t '.red + 'y o u r '.white + 'r p '.blue +
43
- '- '.red + '- '.white + '- '.blue
44
- sleep(0.05)
45
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e t '.red + 'y o u '.white + 'r e p '.blue +
46
- '- '.red + '- '.white + '- '.blue
47
- sleep(0.05)
48
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e t '.red + ' o u r '.white + 'r e p '.blue +
49
- '- '.red + '- '.white + '- '.blue
50
- sleep(0.05)
51
- print "\r" + '- '.red + '- '.white + '- '.blue + ' e t '.red + 'y o u r '.white + 'r e p '.blue +
52
- '- '.red + '- '.white + '- '.blue
53
- sleep(0.05)
54
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e t '.red + 'y o u r '.white + 'r e '.blue +
55
- '- '.red + '- '.white + '- '.blue
56
- sleep(0.05)
57
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e t '.red + 'y o u r '.white + 'r e p '.blue +
58
- '- '.red + '- '.white + '- '.blue
59
- sleep(0.05)
60
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e t '.red + 'y u r '.white + 'r e p '.blue +
61
- '- '.red + '- '.white + '- '.blue
62
- sleep(0.05)
63
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g t '.red + 'y o u r '.white + 'r e p '.blue +
64
- '- '.red + '- '.white + '- '.blue
65
- sleep(0.05)
66
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e t '.red + 'y o u r '.white + ' e p '.blue +
67
- '- '.red + '- '.white + '- '.blue
68
- sleep(0.05)
69
- print "\r" + '- '.red + '- '.white + '- '.blue + 'g e t '.red + 'y o u r '.white + 'r e p '.blue +
70
- '- '.red + '- '.white + '- '.blue
36
+ sparkle_print 'g e ', 'y o u r ', 'r e p '
37
+ sparkle_print 'g e t ', 'y o r ', 'r e p '
38
+ sparkle_print 'g e t ', 'y o u r ', 'r p '
39
+ sparkle_print 'g e t ', 'y o u ', 'r e p '
40
+ sparkle_print 'g e t ', ' o u r ', 'r e p '
41
+ sparkle_print ' e t ', 'y o u r ', 'r e p '
42
+ sparkle_print 'g e t ', 'y o u r ', 'r e '
43
+ sparkle_print 'g e t ', 'y o u r ', 'r e p '
44
+ sparkle_print 'g e t ', 'y u r ', 'r e p '
45
+ sparkle_print 'g t ', 'y o u r ', 'r e p '
46
+ sparkle_print 'g e t ', 'y o u r ', ' e p '
47
+ sparkle_print 'g e t ', 'y o u r ', 'r e p '
71
48
  end
72
49
  puts ''
73
50
  end
51
+
52
+ def self.sparkle_print(string_one, string_two, string_three)
53
+ print "\r" + '- '.red + '- '.white + '- '.blue + string_one.red + string_two.white +
54
+ string_three.blue + '- '.red + '- '.white + '- '.blue
55
+ sleep(0.05)
56
+ end
74
57
  end
75
58
  end
@@ -3,6 +3,7 @@ module GetYourRep
3
3
  # Stores rep info in key/value pairs, and makes values accessible by instance method.
4
4
  class Representative
5
5
  include GetYourRep::Errors
6
+ include GetYourRep::Associations
6
7
 
7
8
  # The Delegation object that the instance belongs to.
8
9
  attr_reader :delegation
@@ -24,12 +25,11 @@ module GetYourRep
24
25
  # Sets the Delegation object that the instance belongs to. Will add self to the Delegation's
25
26
  # reps if not done so already.
26
27
  def delegation=(other)
27
- if other.is_a?(Delegation)
28
- @delegation = other
29
- other.add_rep(self) unless other.reps.include?(self)
30
- else
31
- not_a_del_error
32
- end
28
+ add_parent other: other,
29
+ model: Delegation,
30
+ parent: :delegation,
31
+ child: :rep,
32
+ error: -> { not_a_del_error }
33
33
  end
34
34
 
35
35
  # Returns a frozen duplicate of the office_locations array.
@@ -44,12 +44,11 @@ module GetYourRep
44
44
 
45
45
  # Creates a new OfficeLocation association. Sets self as the other's rep if not done so already.
46
46
  def add_office_location(other)
47
- if other.is_a?(OfficeLocation)
48
- @office_locations << other
49
- other.rep = self unless other.rep == self
50
- else
51
- not_an_office_error
52
- end
47
+ add_child other: other,
48
+ model: OfficeLocation,
49
+ children: :office_locations,
50
+ parent: :rep,
51
+ error: -> { not_an_office_error }
53
52
  end
54
53
 
55
54
  # Assign an individual OfficeLocation, or an array of them.
@@ -63,24 +62,36 @@ module GetYourRep
63
62
 
64
63
  # Parse the first name from the :name.
65
64
  def first_name
66
- @first_name ||= if name_count == 1
67
- nil
68
- elsif (name_count > 3) || (name_array[-2].downcase == name_array[-2])
69
- name_array[0..-3].join(' ')
70
- else
71
- name_array[0..-2].join(' ')
72
- end
65
+ @first_name ||= parse_first_name
66
+ end
67
+
68
+ def parse_first_name
69
+ if name_count == 1
70
+ nil
71
+ elsif four_part_name
72
+ name_array[0..-3].join(' ')
73
+ else
74
+ name_array[0..-2].join(' ')
75
+ end
73
76
  end
74
77
 
75
78
  # Parse the last name from the :name.
76
79
  def last_name
77
- @last_name ||= if name_count == 1
78
- name
79
- elsif (name_count > 3) || (name_array[-2].downcase == name_array[-2])
80
- name_array[-2..-1].join(' ')
81
- else
82
- name_array.last
83
- end
80
+ @last_name ||= parse_last_name
81
+ end
82
+
83
+ def parse_last_name
84
+ if name_count == 1
85
+ name
86
+ elsif four_part_name
87
+ name_array[-2..-1].join(' ')
88
+ else
89
+ name_array.last
90
+ end
91
+ end
92
+
93
+ def four_part_name
94
+ (name_count > 3) || (name_array[-2].downcase == name_array[-2])
84
95
  end
85
96
 
86
97
  # Splits the name into an array.
@@ -7,7 +7,8 @@ module GetYourRep
7
7
  # Taken with gratitude from davidbella https://gist.github.com/davidbella/6918455
8
8
  def initialize(options = {})
9
9
  options.each do |attr, value|
10
- # Recent responses from OpenStates have had '+' appended to some keys. Trim that off or it throws an error.
10
+ # Recent responses from OpenStates have had '+' appended to some keys.
11
+ # Trim that off or it throws an error.
11
12
  attr = attr.delete('+')
12
13
  create_setters_from_opts(attr)
13
14
  create_getters_from_opts(attr)
@@ -23,9 +23,11 @@ module GetYourRep
23
23
  private
24
24
 
25
25
  def determine_office_type
26
- if line1.downcase =~
27
- /(state|house|senate|assembly|capitol|dirksen|reyburn|rayburn|legislative|legislature|government)+/
26
+ # rubocop:disable Metrics/LineLength
27
+ matches =
28
+ /(state|house|senate|assembly|capitol|dirksen|reyburn|rayburn|legislative|legislature|government)+/
28
29
 
30
+ if line1.downcase =~ matches
29
31
  'capitol'
30
32
  else
31
33
  'district'
@@ -33,7 +33,9 @@ module GetYourRep
33
33
  super
34
34
  end
35
35
 
36
- # Build a hash from attributes to be passed to the GetYourRep::Representative constructor method.
36
+ # Build a hash from attributes to be passed to
37
+ # the GetYourRep::Representative constructor method.
38
+ #
37
39
  def build_hash(offices_array, index)
38
40
  find_office_name(offices_array, index)
39
41
  self.hash = { name: name,
@@ -44,8 +46,7 @@ module GetYourRep
44
46
  email: emails,
45
47
  url: url,
46
48
  photo: photoUrl }
47
- parse_channels
48
- hash
49
+ parse_channels # returns the hash object
49
50
  end
50
51
 
51
52
  private
@@ -72,6 +73,8 @@ module GetYourRep
72
73
  channels.each do |channel|
73
74
  hash[channel['type'].downcase.to_sym] = channel['id']
74
75
  end
76
+
77
+ hash
75
78
  end
76
79
  end
77
80
  end
@@ -13,7 +13,9 @@ module GetYourRep
13
13
  @phones = []
14
14
  end
15
15
 
16
- # Build a hash from attributes to be passed to the GetYourRep::Representative constructor method.
16
+ # Build a hash from attributes to be passed to
17
+ # the GetYourRep::Representative constructor method.
18
+ #
17
19
  def build_hash
18
20
  { name: full_name.split(', ').reverse.join(' '),
19
21
  office: office,
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module GetYourRep
3
- VERSION = '1.1.0' # :nodoc:
3
+ VERSION = '1.1.1' # :nodoc:
4
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: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - msimonborg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-02 00:00:00.000000000 Z
11
+ date: 2017-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -147,6 +147,7 @@ files:
147
147
  - exe/reps
148
148
  - get_your_rep.gemspec
149
149
  - lib/get_your_rep.rb
150
+ - lib/get_your_rep/associations.rb
150
151
  - lib/get_your_rep/cli.rb
151
152
  - lib/get_your_rep/delegation.rb
152
153
  - lib/get_your_rep/errors.rb