pyr 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pyr.rb +11 -1
- data/lib/pyr/response.rb +10 -12
- data/lib/pyr/response_object.rb +6 -1
- data/lib/pyr/response_objects/office_location.rb +21 -21
- data/lib/pyr/response_objects/rep.rb +25 -25
- data/lib/pyr/version.rb +1 -1
- data/pyr.gemspec +2 -1
- metadata +21 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 118839e5f2a9f5725eebc66f603f338383ff8e82
|
4
|
+
data.tar.gz: 3879eb7384f392f3e5be09b7fe4161e7e21dd2ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d121f0caaea055c6fe8a09193702b2060d359e964453d85ae94a6c8c85c967109adbc3710d1c72fc13718ba92ef7313e1e81ebf3e484d0b09be0de583321ed73
|
7
|
+
data.tar.gz: 79eef9001f2530c13f9c6104123f0858c24583e803829b80b2a448d4f271b9b30165ff3646427b7433de202cc9a67a46aa44b8092def94f7081b56c0dccec711
|
data/lib/pyr.rb
CHANGED
@@ -20,6 +20,9 @@
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
|
+
require 'faraday'
|
24
|
+
require 'faraday_middleware'
|
25
|
+
|
23
26
|
require 'pyr/param'
|
24
27
|
require 'pyr/parser'
|
25
28
|
require 'pyr/resource'
|
@@ -112,6 +115,13 @@ module PYR
|
|
112
115
|
# The base URI for all requests
|
113
116
|
API_BASE_URI = 'https://phone-your-rep.herokuapp.com/api/beta/'
|
114
117
|
|
118
|
+
# Faraday API Connection
|
119
|
+
API_CONN = Faraday.new url: API_BASE_URI do |conn|
|
120
|
+
conn.response :json, content_type: /\bjson$/
|
121
|
+
conn.response :yaml, content_type: /\byaml$/
|
122
|
+
conn.adapter Faraday.default_adapter
|
123
|
+
end
|
124
|
+
|
115
125
|
# The API resources available to query
|
116
126
|
PYR_RESOURCES = %i[
|
117
127
|
reps
|
@@ -149,7 +159,7 @@ module PYR
|
|
149
159
|
else
|
150
160
|
resource = Request.build(resource, id)
|
151
161
|
yield resource if block_given?
|
152
|
-
request_object = {
|
162
|
+
request_object = { resource: resource }
|
153
163
|
end
|
154
164
|
Response.new request_object
|
155
165
|
end
|
data/lib/pyr/response.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'httparty'
|
4
|
-
|
5
3
|
module PYR
|
6
4
|
# The object returned by a request call to the API.
|
7
5
|
class Response
|
8
|
-
attr_reader :body, :uri, :code, :
|
6
|
+
attr_reader :body, :uri, :code, :reason_phrase, :headers, :objects, :controller
|
9
7
|
|
10
8
|
def initialize(base_url: nil, resource: nil, response_object: nil)
|
11
9
|
assign_url_and_controller(base_url, resource, response_object)
|
@@ -14,24 +12,24 @@ module PYR
|
|
14
12
|
end
|
15
13
|
|
16
14
|
def assign_url_and_controller(base_url, resource, response_object)
|
17
|
-
if
|
15
|
+
if resource
|
18
16
|
@controller = resource.controller
|
19
|
-
@uri =
|
17
|
+
@uri = resource.to_s
|
20
18
|
elsif base_url
|
21
|
-
@controller = base_url.sub(API_BASE_URI, '').split('/').first
|
19
|
+
@controller = base_url.sub!(API_BASE_URI, '').split('/').first
|
22
20
|
@uri = base_url
|
23
21
|
elsif response_object
|
24
22
|
@controller = response_object.controller
|
25
|
-
@uri = response_object.self
|
23
|
+
@uri = response_object.self.sub(API_BASE_URI, '')
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
27
|
def fetch_and_parse_payload
|
30
|
-
|
31
|
-
@body
|
32
|
-
@code
|
33
|
-
@
|
34
|
-
@headers
|
28
|
+
response = API_CONN.get uri
|
29
|
+
@body = response.body
|
30
|
+
@code = response.status
|
31
|
+
@reason_phrase = response.reason_phrase
|
32
|
+
@headers = response.headers
|
35
33
|
end
|
36
34
|
|
37
35
|
def parse_objects
|
data/lib/pyr/response_object.rb
CHANGED
@@ -14,7 +14,7 @@ module PYR
|
|
14
14
|
# Pass in an options hash to instantiate the object and set the data
|
15
15
|
# attributes. First iterates through the hash converting nested hashes
|
16
16
|
# and arrays of hashes into other PYR::ResponseObjects if possible.
|
17
|
-
# Hash keys that are not defined as attributes with `
|
17
|
+
# Hash keys that are not defined as attributes with `attr_accessor`
|
18
18
|
# or `lr_has_many` will not be accessible in the parent object.
|
19
19
|
def initialize(opts = {}, &block)
|
20
20
|
new_opts = opts.each_with_object({}) do |(key, val), memo|
|
@@ -28,6 +28,11 @@ module PYR
|
|
28
28
|
@controller ||= self.class.to_s.split('::').last.tableize
|
29
29
|
end
|
30
30
|
|
31
|
+
#
|
32
|
+
def call
|
33
|
+
PYR.call self
|
34
|
+
end
|
35
|
+
|
31
36
|
private
|
32
37
|
|
33
38
|
# If the attribute key is a valid plural or singular resource,
|
@@ -10,26 +10,26 @@ module PYR
|
|
10
10
|
lr_scope :district, -> { where office_type: 'district' }
|
11
11
|
|
12
12
|
attr_accessor :self,
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
13
|
+
:city,
|
14
|
+
:rep,
|
15
|
+
:active,
|
16
|
+
:office_id,
|
17
|
+
:bioguide_id,
|
18
|
+
:office_type,
|
19
|
+
:distance,
|
20
|
+
:building,
|
21
|
+
:address,
|
22
|
+
:suite,
|
23
|
+
:city,
|
24
|
+
:state,
|
25
|
+
:zip,
|
26
|
+
:phone,
|
27
|
+
:fax,
|
28
|
+
:hours,
|
29
|
+
:latitude,
|
30
|
+
:longitude,
|
31
|
+
:v_card_link,
|
32
|
+
:downloads,
|
33
|
+
:qr_code_link
|
34
34
|
end
|
35
35
|
end
|
@@ -13,30 +13,30 @@ module PYR
|
|
13
13
|
lr_scope :representatives, -> { where role: 'United States Representative' }
|
14
14
|
|
15
15
|
attr_accessor :self,
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
16
|
+
:state,
|
17
|
+
:district,
|
18
|
+
:active,
|
19
|
+
:bioguide_id,
|
20
|
+
:official_full,
|
21
|
+
:role,
|
22
|
+
:party,
|
23
|
+
:senate_class,
|
24
|
+
:last,
|
25
|
+
:first,
|
26
|
+
:middle,
|
27
|
+
:nickname,
|
28
|
+
:suffix,
|
29
|
+
:contact_form,
|
30
|
+
:url,
|
31
|
+
:photo,
|
32
|
+
:twitter,
|
33
|
+
:facebook,
|
34
|
+
:youtube,
|
35
|
+
:instagram,
|
36
|
+
:googleplus,
|
37
|
+
:twitter_id,
|
38
|
+
:facebook_id,
|
39
|
+
:youtube_id,
|
40
|
+
:instagram_id
|
41
41
|
end
|
42
42
|
end
|
data/lib/pyr/version.rb
CHANGED
data/pyr.gemspec
CHANGED
@@ -34,7 +34,8 @@ Gem::Specification.new do |spec|
|
|
34
34
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
35
35
|
spec.require_paths = ['lib']
|
36
36
|
|
37
|
-
spec.add_dependency '
|
37
|
+
spec.add_dependency 'faraday'
|
38
|
+
spec.add_dependency 'faraday_middleware'
|
38
39
|
spec.add_dependency 'lazy_record', '~> 0.4.0'
|
39
40
|
spec.add_development_dependency 'bundler', '~> 1.14'
|
40
41
|
end
|
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pyr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- M. Simon Borg
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: faraday
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: lazy_record
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|