ey_api_hmac 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/ey_api_hmac/base_connection.rb +31 -9
- data/lib/ey_api_hmac/version.rb +1 -1
- data/spec/base_connection_spec.rb +36 -0
- metadata +6 -4
data/Gemfile.lock
CHANGED
@@ -11,14 +11,18 @@ module EY
|
|
11
11
|
@auth_id = auth_id
|
12
12
|
@auth_key = auth_key
|
13
13
|
@standard_headers = {
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
'CONTENT_TYPE' => 'application/json',
|
15
|
+
'Accept' => 'application/json',
|
16
|
+
'HTTP_DATE' => Time.now.httpdate,
|
17
|
+
'USER_AGENT' => user_agent || default_user_agent
|
18
18
|
}
|
19
19
|
@rack_builder_block = rack_builder_block
|
20
20
|
end
|
21
21
|
|
22
|
+
def default_user_agent
|
23
|
+
"ApiHMAC"
|
24
|
+
end
|
25
|
+
|
22
26
|
class NotFound < StandardError
|
23
27
|
def initialize(url)
|
24
28
|
super("#{url} not found")
|
@@ -65,7 +69,11 @@ module EY
|
|
65
69
|
request(:get, url, &block)
|
66
70
|
end
|
67
71
|
|
68
|
-
|
72
|
+
def handle_errors_with(error_handler)
|
73
|
+
@error_handler = error_handler
|
74
|
+
end
|
75
|
+
|
76
|
+
protected
|
69
77
|
|
70
78
|
def client
|
71
79
|
bak = self.backend
|
@@ -83,19 +91,33 @@ module EY
|
|
83
91
|
end
|
84
92
|
|
85
93
|
def request(method, url, body = nil, &block)
|
94
|
+
response = nil
|
95
|
+
request_headers = @standard_headers
|
86
96
|
if body
|
87
|
-
response = client.send(method, url,
|
97
|
+
response = client.send(method, url, request_headers, body.to_json)
|
88
98
|
else
|
89
|
-
response = client.send(method, url,
|
99
|
+
response = client.send(method, url, request_headers)
|
90
100
|
end
|
91
101
|
handle_response(url, response, &block)
|
102
|
+
rescue => e
|
103
|
+
request_hash = {:method => method, :url => url, :headers => request_headers, :body => body}
|
104
|
+
response_hash = {:status => response.status, :body => response.body} if response
|
105
|
+
handle_error(request_hash, (response_hash || {}), e) || (raise e)
|
106
|
+
end
|
107
|
+
|
108
|
+
def handle_error(request, response, exception)
|
109
|
+
if @error_handler
|
110
|
+
@error_handler.call(request, response, exception)
|
111
|
+
end
|
92
112
|
end
|
93
113
|
|
94
114
|
def handle_response(url, response)
|
95
115
|
case response.status
|
96
116
|
when 200, 201
|
97
|
-
|
98
|
-
|
117
|
+
if block_given?
|
118
|
+
json_body = JSON.parse(response.body)
|
119
|
+
yield json_body, response["Location"]
|
120
|
+
end
|
99
121
|
when 404
|
100
122
|
raise NotFound.new(url)
|
101
123
|
when 400
|
data/lib/ey_api_hmac/version.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require 'ey_api_hmac'
|
3
|
+
|
4
|
+
describe EY::ApiHMAC::BaseConnection do
|
5
|
+
before do
|
6
|
+
@connection = EY::ApiHMAC::BaseConnection.new("123", "456")
|
7
|
+
end
|
8
|
+
describe "handle_error" do
|
9
|
+
describe "on 500" do
|
10
|
+
before do
|
11
|
+
@connection.backend = lambda do |env|
|
12
|
+
["500", {}, ""]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
it "raises an error" do
|
16
|
+
lambda { @connection.post("/", "blah") }.should raise_exception(EY::ApiHMAC::BaseConnection::UnknownError)
|
17
|
+
end
|
18
|
+
it "calls the error handler if one is registered" do
|
19
|
+
errors = []
|
20
|
+
@connection.handle_errors_with(lambda {|*args| errors << [args]; "handled"})
|
21
|
+
@connection.post("/", "blah").should eq("handled")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
describe "on bad body" do
|
25
|
+
it "calls the error handler" do
|
26
|
+
@connection.backend = lambda do |env|
|
27
|
+
["200", {"Content-Type" => "application/json"}, "200 OK"]
|
28
|
+
end
|
29
|
+
errors = []
|
30
|
+
@connection.handle_errors_with(lambda {|*args| errors << [args]; false})
|
31
|
+
lambda { @connection.post("/", "blah") {} }.should raise_exception(JSON::ParserError)
|
32
|
+
errors.should_not be_empty
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ey_api_hmac
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 10
|
10
|
+
version: 0.0.10
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Jacob Burkhart & Thorben Schr\xC3\xB6der & David Calavera & others"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-09-
|
18
|
+
date: 2011-09-08 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rack-client
|
@@ -82,6 +82,7 @@ files:
|
|
82
82
|
- lib/ey_api_hmac/sso.rb
|
83
83
|
- lib/ey_api_hmac/version.rb
|
84
84
|
- spec/api_auth_spec.rb
|
85
|
+
- spec/base_connection_spec.rb
|
85
86
|
- spec/spec_helper.rb
|
86
87
|
- spec/sso_spec.rb
|
87
88
|
homepage: ""
|
@@ -119,5 +120,6 @@ specification_version: 3
|
|
119
120
|
summary: HMAC Rack basic implementation for Engine Yard services
|
120
121
|
test_files:
|
121
122
|
- spec/api_auth_spec.rb
|
123
|
+
- spec/base_connection_spec.rb
|
122
124
|
- spec/spec_helper.rb
|
123
125
|
- spec/sso_spec.rb
|