footrest 0.3.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/footrest.gemspec CHANGED
@@ -23,8 +23,12 @@ Gem::Specification.new do |gem|
23
23
  gem.add_development_dependency "bundler", ">= 1.0.0"
24
24
  gem.add_development_dependency "rspec", "~> 2.6"
25
25
  gem.add_development_dependency "webmock"
26
- gem.add_development_dependency "debugger"
27
26
  gem.add_development_dependency "pry"
27
+ if RUBY_VERSION < '2.0'
28
+ gem.add_development_dependency "debugger"
29
+ else
30
+ gem.add_development_dependency "byebug"
31
+ end
28
32
 
29
33
  gem.add_dependency "faraday", "~> 0.9.0"
30
34
  gem.add_dependency "activesupport", ">= 3.0.0"
data/lib/footrest.rb CHANGED
@@ -3,4 +3,19 @@ require 'footrest/client'
3
3
  require 'footrest/version'
4
4
 
5
5
  module Footrest
6
- end
6
+ end
7
+
8
+ module Faraday
9
+ class Response::Logger < Response::Middleware
10
+ private
11
+
12
+ SENSITIVE_HEADERS = %w{Authorization}
13
+ def dump_headers(headers)
14
+ return "empty headers" unless headers
15
+ headers.map { |k, v|
16
+ message = "#{k}: "
17
+ message << (SENSITIVE_HEADERS.include?(k) ? "[filtered]" : v.inspect)
18
+ }.join("\n")
19
+ end
20
+ end
21
+ end
@@ -10,10 +10,15 @@ module Footrest
10
10
  attr_reader :connection
11
11
 
12
12
  def set_connection(config)
13
+ config[:logger] = config[:logging] if config[:logging]
13
14
  @connection = Faraday.new(url: config[:prefix]) do |faraday|
14
15
  faraday.request :multipart
15
16
  faraday.request :url_encoded
16
- faraday.response :logger if config[:logging]
17
+ if config[:logger] == true
18
+ faraday.response :logger
19
+ elsif config[:logger]
20
+ faraday.use Faraday::Response::Logger, config[:logger]
21
+ end
17
22
  faraday.adapter Faraday.default_adapter
18
23
  faraday.use Footrest::FollowRedirects
19
24
  faraday.use Footrest::ParseJson, :content_type => /\bjson$/
@@ -13,7 +13,28 @@ module Footrest
13
13
  @body = @response[:body]
14
14
  @method = @response[:method]
15
15
 
16
- super("HTTP ERROR: #{@status} #{errors}")
16
+ super("HTTP ERROR: #{@status} #{method} #{url}\n#{errors}\n#{headers}")
17
+ end
18
+
19
+ def method
20
+ @method.to_s.upcase
21
+ rescue => e
22
+ "[Unable to show HTTP method: #{e}]"
23
+ end
24
+
25
+ def url
26
+ @response.url.to_s
27
+ rescue => e
28
+ "[Unable to show URL: #{e}]"
29
+ end
30
+
31
+ def headers
32
+ JSON::pretty_generate(
33
+ request: @response.request_headers,
34
+ response: @response.response_headers
35
+ )
36
+ rescue => e
37
+ "[Unable to show headers: #{e}]"
17
38
  end
18
39
 
19
40
  def errors
@@ -5,8 +5,9 @@ module Footrest
5
5
  def on_complete(response)
6
6
  if response[:response_headers]
7
7
  if link = response[:response_headers][:link]
8
- response[:next_page] = find_link(link, "next")
9
- response[:first_page] = find_link(link, "first")
8
+ %w(prev next first last current).each do |page|
9
+ response["#{page}_page".to_sym] = find_link(link, page)
10
+ end
10
11
  end
11
12
  end
12
13
  end
@@ -55,9 +55,14 @@ module Footrest
55
55
  end
56
56
 
57
57
  def response_type(env)
58
- type = env[:response_headers][CONTENT_TYPE].to_s
59
- type = type.split(';', 2).first if type.index(';')
60
- type
58
+ if env[:response_headers]
59
+ type = env[:response_headers][CONTENT_TYPE].to_s
60
+ type = type.split(';', 2).first if type.index(';')
61
+ type
62
+ else
63
+ warn("response_headers are nil. Did you use http when you meant https?")
64
+ nil
65
+ end
61
66
  end
62
67
 
63
68
  def process_response_type?(type)
@@ -1,3 +1,3 @@
1
1
  module Footrest
2
- VERSION = '0.3.1' unless defined?(Footrest::VERSION)
2
+ VERSION = '0.4.1' unless defined?(Footrest::VERSION)
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'helper'
1
+ require_relative '../spec_helper'
2
2
 
3
3
  describe Footrest::Client do
4
4
  it "sets the domain" do
@@ -1,4 +1,4 @@
1
- require 'helper'
1
+ require_relative '../spec_helper'
2
2
 
3
3
  describe Footrest::HttpError do
4
4
  before do
@@ -0,0 +1,20 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Footrest::Pagination do
4
+
5
+ context "on_complete" do
6
+ it 'captures prev, next, first, last, and current page links as response headers' do
7
+ response = {
8
+ response_headers: {
9
+ link: "<current_link>; rel=\"current\",<next_link>; rel=\"next\",<prev_link>; rel=\"prev\",<first_link>; rel=\"first\",<last_link>; rel=\"last\""
10
+ }
11
+ }
12
+
13
+ Footrest::Pagination.new.on_complete(response)
14
+
15
+ %w(prev next first last current).each do |page|
16
+ expect(response["#{page}_page".to_sym]).to eq("#{page}_link")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,4 +1,4 @@
1
- require 'helper'
1
+ require_relative '../spec_helper'
2
2
 
3
3
  class RequestHarness; include Footrest::Request; end
4
4
 
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: footrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-02-05 00:00:00.000000000 Z
13
+ date: 2014-12-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -77,7 +77,7 @@ dependencies:
77
77
  - !ruby/object:Gem::Version
78
78
  version: '0'
79
79
  - !ruby/object:Gem::Dependency
80
- name: debugger
80
+ name: pry
81
81
  requirement: !ruby/object:Gem::Requirement
82
82
  none: false
83
83
  requirements:
@@ -93,7 +93,7 @@ dependencies:
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  - !ruby/object:Gem::Dependency
96
- name: pry
96
+ name: debugger
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  none: false
99
99
  requirements:
@@ -178,8 +178,9 @@ files:
178
178
  - lib/footrest.rb
179
179
  - spec/footrest/client_spec.rb
180
180
  - spec/footrest/http_error_spec.rb
181
+ - spec/footrest/pagination_spec.rb
181
182
  - spec/footrest/request_spec.rb
182
- - spec/helper.rb
183
+ - spec/spec_helper.rb
183
184
  homepage: https://github.com/instructure/footrest
184
185
  licenses:
185
186
  - MIT
@@ -208,6 +209,7 @@ summary: REST APIs
208
209
  test_files:
209
210
  - spec/footrest/client_spec.rb
210
211
  - spec/footrest/http_error_spec.rb
212
+ - spec/footrest/pagination_spec.rb
211
213
  - spec/footrest/request_spec.rb
212
- - spec/helper.rb
214
+ - spec/spec_helper.rb
213
215
  has_rdoc: