footrest 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/footrest.gemspec CHANGED
@@ -29,4 +29,7 @@ Gem::Specification.new do |gem|
29
29
  gem.add_dependency "faraday", "~> 0.8.8"
30
30
  gem.add_dependency "faraday_middleware", "~> 0.9.0"
31
31
  gem.add_dependency "activesupport", ">= 3.0.0"
32
+
33
+ # Parses Link headers formatted according to RFC 5988 draft spec
34
+ gem.add_dependency "link_header", ">= 0.0.7"
32
35
  end
@@ -25,6 +25,7 @@ module Footrest
25
25
  end
26
26
 
27
27
  def fullpath(path)
28
+ return path if path =~ /^https?:/i
28
29
  prefix ? join(prefix, path) : path
29
30
  end
30
31
 
@@ -1,6 +1,7 @@
1
1
  require 'faraday'
2
2
  require 'faraday_middleware'
3
- require 'faraday/response/raise_footrest_http_error'
3
+ require 'footrest/http_error'
4
+ require 'footrest/pagination'
4
5
 
5
6
  module Footrest
6
7
  module Connection
@@ -15,7 +16,8 @@ module Footrest
15
16
  faraday.adapter Faraday.default_adapter
16
17
  faraday.use FaradayMiddleware::FollowRedirects
17
18
  faraday.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
18
- faraday.use Faraday::Response::RaiseFootrestHttpError
19
+ faraday.use Footrest::RaiseFootrestErrors
20
+ faraday.use Footrest::Pagination
19
21
  faraday.headers[:accept] = "application/json"
20
22
  faraday.headers[:authorization] = "Bearer #{config[:token]}" if config[:token]
21
23
  faraday.headers[:user_agent] = "Footrest"
@@ -1,3 +1,6 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
1
4
  module Footrest
2
5
  module HttpError
3
6
  class ErrorBase < StandardError
@@ -10,7 +13,13 @@ module Footrest
10
13
  @body = @response[:body]
11
14
  @method = @response[:method]
12
15
 
13
- super("HTTP ERROR: #{@status}")
16
+ super("HTTP ERROR: #{@status} #{errors}")
17
+ end
18
+
19
+ def errors
20
+ JSON::pretty_generate(JSON::parse(@body)["errors"])
21
+ rescue
22
+ @body
14
23
  end
15
24
  end
16
25
 
@@ -21,6 +30,25 @@ module Footrest
21
30
  ).each do |error|
22
31
  const_set error.to_sym, Class.new(ErrorBase)
23
32
  end
33
+ end
24
34
 
35
+ class RaiseFootrestErrors < Faraday::Response::Middleware
36
+ ERROR_MAP = {
37
+ 400 => Footrest::HttpError::BadRequest,
38
+ 401 => Footrest::HttpError::Unauthorized,
39
+ 403 => Footrest::HttpError::Forbidden,
40
+ 404 => Footrest::HttpError::NotFound,
41
+ 405 => Footrest::HttpError::MethodNotAllowed,
42
+ 500 => Footrest::HttpError::InternalServerError,
43
+ 501 => Footrest::HttpError::NotImplemented,
44
+ 502 => Footrest::HttpError::BadGateway,
45
+ 503 => Footrest::HttpError::ServiceUnavailable
46
+ }
47
+
48
+ def on_complete(response)
49
+ key = response[:status].to_i
50
+ raise ERROR_MAP[key].new(response) if ERROR_MAP.has_key? key
51
+ end
25
52
  end
53
+
26
54
  end
@@ -0,0 +1,20 @@
1
+ require 'link_header'
2
+
3
+ module Footrest
4
+ class Pagination < Faraday::Response::Middleware
5
+ def on_complete(response)
6
+ if response[:response_headers]
7
+ if link = response[:response_headers][:link]
8
+ response[:next_page] = find_link(link, "next")
9
+ response[:first_page] = find_link(link, "first")
10
+ end
11
+ end
12
+ end
13
+
14
+ def find_link(header, rel)
15
+ link = ::LinkHeader.parse(header).links.find{ |link| link['rel'] == rel }
16
+ link.to_a.first if link
17
+ end
18
+ end
19
+ end
20
+
@@ -1,3 +1,3 @@
1
1
  module Footrest
2
- VERSION = '0.2.0' unless defined?(Footrest::VERSION)
2
+ VERSION = '0.2.2' unless defined?(Footrest::VERSION)
3
3
  end
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.2.0
4
+ version: 0.2.2
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: 2013-09-13 00:00:00.000000000 Z
13
+ date: 2013-10-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -156,6 +156,22 @@ dependencies:
156
156
  - - ! '>='
157
157
  - !ruby/object:Gem::Version
158
158
  version: 3.0.0
159
+ - !ruby/object:Gem::Dependency
160
+ name: link_header
161
+ requirement: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: 0.0.7
167
+ type: :runtime
168
+ prerelease: false
169
+ version_requirements: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ! '>='
173
+ - !ruby/object:Gem::Version
174
+ version: 0.0.7
159
175
  description: Ruby interface for restful APIs
160
176
  email:
161
177
  - duane@instructure.com
@@ -166,10 +182,10 @@ extra_rdoc_files: []
166
182
  files:
167
183
  - Rakefile
168
184
  - footrest.gemspec
169
- - lib/faraday/response/raise_footrest_http_error.rb
170
185
  - lib/footrest/client.rb
171
186
  - lib/footrest/connection.rb
172
187
  - lib/footrest/http_error.rb
188
+ - lib/footrest/pagination.rb
173
189
  - lib/footrest/request.rb
174
190
  - lib/footrest/version.rb
175
191
  - lib/footrest.rb
@@ -1,23 +0,0 @@
1
- require 'faraday'
2
- require 'footrest/http_error'
3
-
4
- module Faraday
5
- class Response::RaiseFootrestHttpError < Response::Middleware
6
- ERROR_MAP = {
7
- 400 => Footrest::HttpError::BadRequest,
8
- 401 => Footrest::HttpError::Unauthorized,
9
- 403 => Footrest::HttpError::Forbidden,
10
- 404 => Footrest::HttpError::NotFound,
11
- 405 => Footrest::HttpError::MethodNotAllowed,
12
- 500 => Footrest::HttpError::InternalServerError,
13
- 501 => Footrest::HttpError::NotImplemented,
14
- 502 => Footrest::HttpError::BadGateway,
15
- 503 => Footrest::HttpError::ServiceUnavailable
16
- }
17
-
18
- def on_complete(response)
19
- key = response[:status].to_i
20
- raise ERROR_MAP[key].new(response) if ERROR_MAP.has_key? key
21
- end
22
- end
23
- end