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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 976f951e641281e557062b61caeb09e4ba007b13
4
- data.tar.gz: 60ffb83eaf42200bb494b0b1d42d8f4c50dfc6aa
3
+ metadata.gz: 118839e5f2a9f5725eebc66f603f338383ff8e82
4
+ data.tar.gz: 3879eb7384f392f3e5be09b7fe4161e7e21dd2ca
5
5
  SHA512:
6
- metadata.gz: 75fbacdad26adb045bf28ef18a7ed9d0709ce9fa2d66c336b083ec3829f76d15d885bc877534d726dd7257c3a84ecd76042f3074bb55edd4fda959b5016ed160
7
- data.tar.gz: ceb9b557dd6d88acd537f54be7efadcc4217092c755674c2e302f0dc48b7850c73f84a27232691c1158294f7b5bda60f3c20d1f573d5679ac68e2e165eb9dae3
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 = { base_url: API_BASE_URI, resource: resource }
162
+ request_object = { resource: resource }
153
163
  end
154
164
  Response.new request_object
155
165
  end
@@ -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, :message, :headers, :objects, :controller
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 base_url && resource
15
+ if resource
18
16
  @controller = resource.controller
19
- @uri = "#{base_url}#{resource}"
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
- payload = HTTParty.get uri
31
- @body = payload.parsed_response
32
- @code = payload.code
33
- @message = payload.message
34
- @headers = payload.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
@@ -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 `lr_attr_accessor`
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
- :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
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
- :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
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PYR
4
- VERSION = '0.2.4' # :nodoc:
4
+ VERSION = '0.2.5' # :nodoc:
5
5
  end
@@ -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 'httparty', '~> 0.14.0'
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
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-14 00:00:00.000000000 Z
11
+ date: 2017-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: httparty
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.14.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.14.0
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: lazy_record
29
43
  requirement: !ruby/object:Gem::Requirement