sawyer 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/lib/sawyer.rb +1 -1
- data/lib/sawyer/agent.rb +21 -3
- data/lib/sawyer/response.rb +9 -0
- data/sawyer.gemspec +2 -2
- data/test/agent_test.rb +24 -0
- data/test/response_test.rb +7 -1
- metadata +2 -2
data/Gemfile
CHANGED
data/lib/sawyer.rb
CHANGED
data/lib/sawyer/agent.rb
CHANGED
@@ -9,12 +9,26 @@ module Sawyer
|
|
9
9
|
# Sawyer::Response.
|
10
10
|
#
|
11
11
|
# endpoint - String URI of the API entry point.
|
12
|
-
def initialize(endpoint)
|
12
|
+
def initialize(endpoint, conn = nil)
|
13
13
|
@endpoint = endpoint
|
14
|
-
@conn = Faraday.new
|
14
|
+
@conn = conn || Faraday.new(endpoint)
|
15
15
|
yield @conn if block_given?
|
16
16
|
end
|
17
17
|
|
18
|
+
# Public: Retains a reference to the root relations of the API.
|
19
|
+
#
|
20
|
+
# Returns a Sawyer::Relation::Map.
|
21
|
+
def rels
|
22
|
+
@rels ||= root.data.rels
|
23
|
+
end
|
24
|
+
|
25
|
+
# Public: Retains a reference to the root response of the API.
|
26
|
+
#
|
27
|
+
# Returns a Sawyer::Response.
|
28
|
+
def root
|
29
|
+
@root ||= start
|
30
|
+
end
|
31
|
+
|
18
32
|
# Public: Hits the root of the API to get the initial actions.
|
19
33
|
#
|
20
34
|
# Returns a Sawyer::Response.
|
@@ -43,15 +57,19 @@ module Sawyer
|
|
43
57
|
|
44
58
|
options ||= {}
|
45
59
|
url = expand_url(url, options[:uri])
|
60
|
+
started = nil
|
46
61
|
res = @conn.send method, url do |req|
|
47
|
-
req.body = encode_body(data) if data
|
62
|
+
req.body = encode_body(data) if data
|
48
63
|
if params = options[:query]
|
49
64
|
req.params.update params
|
50
65
|
end
|
51
66
|
if headers = options[:headers]
|
52
67
|
req.headers.update headers
|
53
68
|
end
|
69
|
+
started = Time.now
|
54
70
|
end
|
71
|
+
res.env[:sawyer_started] = started
|
72
|
+
res.env[:sawyer_ended] = Time.now
|
55
73
|
|
56
74
|
Response.new self, res
|
57
75
|
end
|
data/lib/sawyer/response.rb
CHANGED
@@ -16,6 +16,7 @@ module Sawyer
|
|
16
16
|
@agent = agent
|
17
17
|
@status = res.status
|
18
18
|
@headers = res.headers
|
19
|
+
@env = res.env
|
19
20
|
@data = process_data(@agent.decode_body(res.body))
|
20
21
|
end
|
21
22
|
|
@@ -35,6 +36,14 @@ module Sawyer
|
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
39
|
+
def timing
|
40
|
+
@timing ||= @env[:sawyer_ended] - @env[:sawyer_started]
|
41
|
+
end
|
42
|
+
|
43
|
+
def time
|
44
|
+
@env[:sawyer_ended]
|
45
|
+
end
|
46
|
+
|
38
47
|
def inspect
|
39
48
|
%(#<#{self.class}: #{@status} @rels=#{@rels.inspect} @data=#{@data.inspect}>)
|
40
49
|
end
|
data/sawyer.gemspec
CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
## If your rubyforge_project name is different, then edit it and comment out
|
14
14
|
## the sub! line in the Rakefile
|
15
15
|
s.name = 'sawyer'
|
16
|
-
s.version = '0.0.
|
17
|
-
s.date = '2012-09-
|
16
|
+
s.version = '0.0.3'
|
17
|
+
s.date = '2012-09-26'
|
18
18
|
s.rubyforge_project = 'sawyer'
|
19
19
|
|
20
20
|
## Make sure your summary is short. The description may be as long
|
data/test/agent_test.rb
CHANGED
@@ -10,6 +10,30 @@ module Sawyer
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
def test_accesses_root_relations
|
14
|
+
@stubs.get '/a/' do |env|
|
15
|
+
assert_equal 'foo.com', env[:url].host
|
16
|
+
|
17
|
+
[200, {}, Yajl.dump(
|
18
|
+
:_links => {
|
19
|
+
:users => {:href => '/users'}})]
|
20
|
+
end
|
21
|
+
|
22
|
+
assert_equal 200, @agent.root.status
|
23
|
+
|
24
|
+
assert_equal '/users', @agent.rels[:users].href
|
25
|
+
assert_equal :get, @agent.rels[:users].method
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_saves_root_endpoint
|
29
|
+
@stubs.get '/a/' do |env|
|
30
|
+
[200, {}, '{}']
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_kind_of Sawyer::Response, @agent.root
|
34
|
+
assert_not_equal @agent.root.time, @agent.start.time
|
35
|
+
end
|
36
|
+
|
13
37
|
def test_starts_a_session
|
14
38
|
@stubs.get '/a/' do |env|
|
15
39
|
assert_equal 'foo.com', env[:url].host
|
data/test/response_test.rb
CHANGED
@@ -3,6 +3,7 @@ require File.expand_path("../helper", __FILE__)
|
|
3
3
|
module Sawyer
|
4
4
|
class ResponseTest < TestCase
|
5
5
|
def setup
|
6
|
+
@now = Time.now
|
6
7
|
@stubs = Faraday::Adapter::Test::Stubs.new
|
7
8
|
@agent = Sawyer::Agent.new "http://foo.com" do |conn|
|
8
9
|
conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
|
@@ -17,7 +18,7 @@ module Sawyer
|
|
17
18
|
end
|
18
19
|
end
|
19
20
|
end
|
20
|
-
|
21
|
+
|
21
22
|
@res = @agent.start
|
22
23
|
assert_kind_of Sawyer::Response, @res
|
23
24
|
end
|
@@ -40,6 +41,11 @@ module Sawyer
|
|
40
41
|
assert_equal :post, @res.data.rels[:self].method
|
41
42
|
end
|
42
43
|
|
44
|
+
def test_gets_response_timing
|
45
|
+
assert @res.timing > 0
|
46
|
+
assert @res.time >= @now
|
47
|
+
end
|
48
|
+
|
43
49
|
def test_makes_request_from_relation
|
44
50
|
@stubs.post '/a' do
|
45
51
|
[201, {}, ""]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sawyer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|