sawyer 0.8.1 → 0.9.1

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
- SHA1:
3
- metadata.gz: f6cbbe41c12f00d598889cbb8aed1d64097bd8dc
4
- data.tar.gz: 938a4dd63ecc154994228964b2777ebd2c157563
2
+ SHA256:
3
+ metadata.gz: 64487d4cb58ab69521a75070aedc4630968e138ca03add5e10f0a7933f007004
4
+ data.tar.gz: ecc9fb91e96931cc9e275ec976effbd88850a55a5f54c3ea521488b465d5b8bf
5
5
  SHA512:
6
- metadata.gz: cfcca0af2c72b15c6a6a3d13d327e4ca1c60fb99ac292f19a81b695a5ef56d9d49cf5ce15a87434144566713e6a87343d52cde9f4e43a191552b8767a23d89cb
7
- data.tar.gz: e8476f19b01abf5595ceb699e317fb5d5a68c590f91022778ff2e2b04b8350c98427cc5015c7132309b2856c881c90f578f2957932ef0d9297fc502467b16b43
6
+ metadata.gz: 7cdaadda1829426240d8f839c1065fa94b05014ebfede30367179c4e3d0add95484b1d6f3660e6bd810714beb7d6e1a3a814ce908539bd35b57e51a0b6a1e7ef
7
+ data.tar.gz: 3c4a5da79f2321361d0f6e9a36fa7d177eb0a087fb3514c66d1de3de0b932aa1b0fda2954e9df5815c5603367c8d1799031d16fd717299cccb73d1c80778ca27
data/Gemfile CHANGED
@@ -1,8 +1,13 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
- gem 'rake'
4
+
5
+ gem "rake"
5
6
 
6
7
  group :test do
7
8
  gem "minitest"
8
9
  end
10
+
11
+ install_if -> { ENV["FARADAY_VERSION"] } do
12
+ gem "faraday", ENV["FARADAY_VERSION"]
13
+ end
data/README.md CHANGED
@@ -1,19 +1,62 @@
1
1
  # Sawyer
2
2
 
3
- Sawyer is an experimental secret user agent built on top of
4
- [Faraday][faraday].
3
+ Sawyer is an experimental hypermedia agent for Ruby built on top of [Faraday][faraday].
5
4
 
6
5
  [faraday]: https://github.com/lostisland/faraday
7
6
 
8
- ![](http://techno-weenie.net/sawyer/images/faraday.jpeg)
7
+ ## Installation
9
8
 
10
- Think of Faraday as the nerdy scientist behind an HTTP API. Sure, he
11
- knows the technical details of how to communicate with an application.
12
- But he also gets overly obsessive about alternate timelines to be of
13
- much use.
9
+ Add this line to your application's Gemfile:
14
10
 
15
- ![](http://techno-weenie.net/sawyer/images/sawyer.jpeg)
11
+ ```ruby
12
+ gem 'sawyer'
13
+ ```
16
14
 
17
- Sawyer knows what needs to be done. He gets in there, assesses the
18
- situation, and figures out the next action.
15
+ And then execute:
19
16
 
17
+ ```sh
18
+ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```sh
24
+ gem install sawyer
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require "sawyer"
31
+
32
+ # Create a Sawyer agent
33
+ agent = Sawyer::Agent.new("https://api.github.com",
34
+ links_parser: Sawyer::LinkParsers::Simple.new)
35
+
36
+ # Fetch the root of the API
37
+ root = agent.root.data
38
+
39
+ # Access a resource directly
40
+ contributors = agent.call(:get, "repos/lostisland/sawyer/contributors").data
41
+
42
+ # Load a hypermedia relation
43
+ top_contributor = contributors.first
44
+ followers = top_contributor.rels[:followers].get.data
45
+ ```
46
+
47
+ For more information, check out the [documentation](http://www.rubydoc.info/gems/sawyer/).
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `script/test` to bootstrap the project and run the tests.
52
+ You can also run `script/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To package the gem, run `script/package`. To release a new version, update the version number in [`lib/sawyer.rb`](lib/sawyer.rb), and then run `script/release`, which will create a git tag for the version, push git commits and tags, and push the .gem file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Check out the [contributing guide](CONTRIBUTING.md) for more information on contributing.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/lib/sawyer/agent.rb CHANGED
@@ -48,6 +48,11 @@ module Sawyer
48
48
  yield @conn if block_given?
49
49
  end
50
50
 
51
+ # Public: Close the underlying connection.
52
+ def close
53
+ @conn.close if @conn.respond_to?(:close)
54
+ end
55
+
51
56
  # Public: Retains a reference to the root relations of the API.
52
57
  #
53
58
  # Returns a Sawyer::Relation::Map.
@@ -38,6 +38,7 @@ module Sawyer
38
38
  def keys
39
39
  @map.keys
40
40
  end
41
+
41
42
  def to_hash
42
43
  pairs = @map.map do |k, v|
43
44
  [(k.to_s + "_url").to_sym, v.href]
@@ -3,7 +3,8 @@ module Sawyer
3
3
  attr_reader :agent,
4
4
  :status,
5
5
  :headers,
6
- :data,
6
+ :env,
7
+ :body,
7
8
  :rels
8
9
 
9
10
  # Builds a Response after a completed request.
@@ -15,12 +16,19 @@ module Sawyer
15
16
  @status = res.status
16
17
  @headers = res.headers
17
18
  @env = res.env
18
- @data = @headers[:content_type] =~ /json|msgpack/ ? process_data(@agent.decode_body(res.body)) : res.body
19
+ @body = res.body
19
20
  @rels = process_rels
20
21
  @started = options[:sawyer_started]
21
22
  @ended = options[:sawyer_ended]
22
23
  end
23
24
 
25
+ def data
26
+ @data ||= begin
27
+ return(body) unless (headers[:content_type] =~ /json|msgpack/)
28
+ process_data(agent.decode_body(body))
29
+ end
30
+ end
31
+
24
32
  # Turns parsed contents from an API response into a Resource or
25
33
  # collection of Resources.
26
34
  #
@@ -58,7 +66,7 @@ module Sawyer
58
66
  end
59
67
 
60
68
  def inspect
61
- %(#<#{self.class}: #{@status} @rels=#{@rels.inspect} @data=#{@data.inspect}>)
69
+ %(#<#{self.class}: #{@status} @rels=#{@rels.inspect} @data=#{data.inspect}>)
62
70
  end
63
71
  end
64
72
  end
@@ -4,7 +4,9 @@ require 'time'
4
4
  module Sawyer
5
5
  class Serializer
6
6
  def self.any_json
7
- yajl || multi_json || json
7
+ yajl || multi_json || json || begin
8
+ raise RuntimeError, "Sawyer requires a JSON gem: yajl, multi_json, or json"
9
+ end
8
10
  end
9
11
 
10
12
  def self.yajl
data/lib/sawyer.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Sawyer
2
- VERSION = "0.8.1"
2
+ VERSION = "0.9.1"
3
3
 
4
4
  class Error < StandardError; end
5
5
  end
data/sawyer.gemspec CHANGED
@@ -17,13 +17,12 @@ Gem::Specification.new do |spec|
17
17
  spec.homepage = 'https://github.com/lostisland/sawyer'
18
18
  spec.licenses = ['MIT']
19
19
 
20
- spec.add_dependency 'faraday', ['~> 0.8', '< 1.0']
21
- spec.add_dependency 'addressable', ['>= 2.3.5', '< 2.6']
20
+ spec.add_dependency 'faraday', '>= 0.17.3', '< 3'
21
+ spec.add_dependency 'addressable', ['>= 2.3.5']
22
22
 
23
23
  spec.files = %w(Gemfile LICENSE.md README.md Rakefile)
24
24
  spec.files << "#{lib}.gemspec"
25
25
  spec.files += Dir.glob("lib/**/*.rb")
26
- spec.files += Dir.glob("test/**/*.rb")
27
26
  spec.files += Dir.glob("script/*")
28
27
 
29
28
  dev_null = File.exist?('/dev/null') ? '/dev/null' : 'NUL'
metadata CHANGED
@@ -1,36 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sawyer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Olson
8
8
  - Wynn Netherland
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-18 00:00:00.000000000 Z
12
+ date: 2022-04-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '0.8'
20
+ version: 0.17.3
21
21
  - - "<"
22
22
  - !ruby/object:Gem::Version
23
- version: '1.0'
23
+ version: '3'
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
- - - "~>"
28
+ - - ">="
29
29
  - !ruby/object:Gem::Version
30
- version: '0.8'
30
+ version: 0.17.3
31
31
  - - "<"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.0'
33
+ version: '3'
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: addressable
36
36
  requirement: !ruby/object:Gem::Requirement
@@ -38,9 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 2.3.5
41
- - - "<"
42
- - !ruby/object:Gem::Version
43
- version: '2.6'
44
41
  type: :runtime
45
42
  prerelease: false
46
43
  version_requirements: !ruby/object:Gem::Requirement
@@ -48,10 +45,7 @@ dependencies:
48
45
  - - ">="
49
46
  - !ruby/object:Gem::Version
50
47
  version: 2.3.5
51
- - - "<"
52
- - !ruby/object:Gem::Version
53
- version: '2.6'
54
- description:
48
+ description:
55
49
  email: technoweenie@gmail.com
56
50
  executables: []
57
51
  extensions: []
@@ -75,16 +69,11 @@ files:
75
69
  - script/package
76
70
  - script/release
77
71
  - script/test
78
- - test/agent_test.rb
79
- - test/helper.rb
80
- - test/relation_test.rb
81
- - test/resource_test.rb
82
- - test/response_test.rb
83
72
  homepage: https://github.com/lostisland/sawyer
84
73
  licenses:
85
74
  - MIT
86
75
  metadata: {}
87
- post_install_message:
76
+ post_install_message:
88
77
  rdoc_options: []
89
78
  require_paths:
90
79
  - lib
@@ -99,10 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
88
  - !ruby/object:Gem::Version
100
89
  version: 1.3.5
101
90
  requirements: []
102
- rubyforge_project:
103
- rubygems_version: 2.4.5.1
104
- signing_key:
91
+ rubygems_version: 3.0.9
92
+ signing_key:
105
93
  specification_version: 2
106
94
  summary: Secret User Agent of HTTP
107
95
  test_files: []
108
- has_rdoc:
data/test/agent_test.rb DELETED
@@ -1,195 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- require 'faraday/adapter/test'
4
-
5
- module Sawyer
6
- class AgentTest < TestCase
7
-
8
- class InlineRelsParser
9
- def parse(data)
10
- links = {}
11
- data.keys.select {|k| k[/_url$/] }.each {|k| links[k.to_s.gsub(/_url$/, '')] = data.delete(k) }
12
-
13
- return data, links
14
- end
15
- end
16
-
17
- def setup
18
- @stubs = Faraday::Adapter::Test::Stubs.new
19
- @agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
20
- conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
21
- conn.adapter :test, @stubs
22
- end
23
- end
24
-
25
- def test_accesses_root_relations
26
- @stubs.get '/a/' do |env|
27
- assert_equal 'foo.com', env[:url].host
28
-
29
- [200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
30
- :_links => {
31
- :users => {:href => '/users'}})]
32
- end
33
-
34
- assert_equal 200, @agent.root.status
35
-
36
- assert_equal '/users', @agent.rels[:users].href
37
- assert_equal :get, @agent.rels[:users].method
38
- end
39
-
40
- def test_allows_custom_rel_parsing
41
- @stubs.get '/a/' do |env|
42
- assert_equal 'foo.com', env[:url].host
43
-
44
- [200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
45
- :url => '/',
46
- :users_url => '/users',
47
- :repos_url => '/repos')]
48
- end
49
-
50
- agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
51
- conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
52
- conn.adapter :test, @stubs
53
- end
54
- agent.links_parser = InlineRelsParser.new
55
-
56
- assert_equal 200, agent.root.status
57
-
58
- assert_equal '/users', agent.rels[:users].href
59
- assert_equal :get, agent.rels[:users].method
60
- assert_equal '/repos', agent.rels[:repos].href
61
- assert_equal :get, agent.rels[:repos].method
62
-
63
- end
64
-
65
- def test_saves_root_endpoint
66
- @stubs.get '/a/' do |env|
67
- [200, {}, '{}']
68
- end
69
-
70
- assert_kind_of Sawyer::Response, @agent.root
71
- refute_equal @agent.root.time, @agent.start.time
72
- end
73
-
74
- def test_starts_a_session
75
- @stubs.get '/a/' do |env|
76
- assert_equal 'foo.com', env[:url].host
77
-
78
- [200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
79
- :_links => {
80
- :users => {:href => '/users'}})]
81
- end
82
-
83
- res = @agent.start
84
-
85
- assert_equal 200, res.status
86
- assert_kind_of Sawyer::Resource, resource = res.data
87
-
88
- assert_equal '/users', resource.rels[:users].href
89
- assert_equal :get, resource.rels[:users].method
90
- end
91
-
92
- def test_requests_with_body_and_options
93
- @stubs.post '/a/b/c' do |env|
94
- assert_equal '{"a":1}', env[:body]
95
- assert_equal 'abc', env[:request_headers]['x-test']
96
- assert_equal 'foo=bar', env[:url].query
97
- [200, {}, "{}"]
98
- end
99
-
100
- res = @agent.call :post, 'b/c' , {:a => 1},
101
- :headers => {"X-Test" => "abc"},
102
- :query => {:foo => 'bar'}
103
- assert_equal 200, res.status
104
- end
105
-
106
- def test_requests_with_body_and_options_to_get
107
- @stubs.get '/a/b/c' do |env|
108
- assert_nil env[:body]
109
- assert_equal 'abc', env[:request_headers]['x-test']
110
- assert_equal 'foo=bar', env[:url].query
111
- [200, {}, "{}"]
112
- end
113
-
114
- res = @agent.call :get, 'b/c' , {:a => 1},
115
- :headers => {"X-Test" => "abc"},
116
- :query => {:foo => 'bar'}
117
- assert_equal 200, res.status
118
- end
119
-
120
- def test_encodes_and_decodes_times
121
- time = Time.at(Time.now.to_i)
122
- data = {
123
- :a => 1,
124
- :b => true,
125
- :c => 'c',
126
- :created_at => time,
127
- :published_at => nil,
128
- :updated_at => "An invalid date",
129
- :pub_date => time,
130
- :subscribed_at => time.to_i,
131
- :lost_at => time.to_f,
132
- :first_date => false,
133
- :validate => true
134
- }
135
- data = [data.merge(:foo => [data])]
136
- encoded = Sawyer::Agent.encode(data)
137
- decoded = Sawyer::Agent.decode(encoded)
138
-
139
- 2.times do
140
- assert_equal 1, decoded.size
141
- decoded = decoded.shift
142
-
143
- assert_equal 1, decoded[:a]
144
- assert_equal true, decoded[:b]
145
- assert_equal 'c', decoded[:c]
146
- assert_equal time, decoded[:created_at], "Did not parse created_at as Time"
147
- assert_nil decoded[:published_at]
148
- assert_equal "An invalid date", decoded[:updated_at]
149
- assert_equal time, decoded[:pub_date], "Did not parse pub_date as Time"
150
- assert_equal true, decoded[:validate]
151
- assert_equal time, decoded[:subscribed_at], "Did not parse subscribed_at as Time"
152
- assert_equal time, decoded[:lost_at], "Did not parse lost_at as Time"
153
- assert_equal false, decoded[:first_date], "Parsed first_date"
154
- decoded = decoded[:foo]
155
- end
156
- end
157
-
158
- def test_does_not_encode_non_json_content_types
159
- @stubs.get '/a/' do |env|
160
- assert_equal 'foo.com', env[:url].host
161
-
162
- [200, {'Content-Type' => 'text/plain'}, "This is plain text"]
163
- end
164
- res = @agent.call :get, '/a/',
165
- :headers => {"Accept" => "text/plain"}
166
- assert_equal 200, res.status
167
-
168
- assert_equal "This is plain text", res.data
169
- end
170
-
171
- def test_handle_yaml_dump_and_load
172
- require 'yaml'
173
- res = Agent.new 'http://example.com', :a => 1
174
- YAML.load(YAML.dump(res))
175
- end
176
-
177
- def test_handle_marshal_dump_and_load
178
- res = Agent.new 'http://example.com', :a => 1
179
- Marshal.load(Marshal.dump(res))
180
- end
181
-
182
- def test_blank_response_doesnt_raise
183
- @stubs.get "/a/" do |env|
184
- assert_equal "foo.com", env[:url].host
185
- [200, { "Content-Type" => "application/json" }, " "]
186
- end
187
-
188
- agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
189
- conn.adapter :test, @stubs
190
- end
191
-
192
- assert_equal 200, agent.root.status
193
- end
194
- end
195
- end
data/test/helper.rb DELETED
@@ -1,7 +0,0 @@
1
- require "minitest/autorun"
2
- require File.expand_path('../../lib/sawyer', __FILE__)
3
-
4
- class Sawyer::TestCase < Minitest::Test
5
- def default_test
6
- end
7
- end
@@ -1,174 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- module Sawyer
4
- class RelationTest < TestCase
5
- def test_builds_relation_from_hash
6
- hash = {:href => '/users/1', :method => 'post'}
7
- rel = Sawyer::Relation.from_link(nil, :self, hash)
8
-
9
- assert_equal :self, rel.name
10
- assert_equal '/users/1', rel.href
11
- assert_equal :post, rel.method
12
- assert_equal [:post], rel.available_methods.to_a
13
- end
14
-
15
- def test_builds_multiple_rels_from_multiple_methods
16
- index = {
17
- 'comments' => {:href => '/comments', :method => 'get,post'}
18
- }
19
-
20
- rels = Sawyer::Relation.from_links(nil, index)
21
- assert_equal 1, rels.size
22
- assert_equal [:comments], rels.keys
23
-
24
- assert rel = rels[:comments]
25
- assert_equal '/comments', rel.href
26
- assert_equal :get, rel.method
27
- assert_equal [:get, :post], rel.available_methods.to_a
28
- assert_kind_of Addressable::Template, rel.href_template
29
- end
30
-
31
- def test_builds_rels_from_hash
32
- index = {
33
- 'self' => '/users/1'
34
- }
35
-
36
- rels = Sawyer::Relation.from_links(nil, index)
37
-
38
- assert_equal 1, rels.size
39
- assert_equal [:self], rels.keys
40
- assert rel = rels[:self]
41
- assert_equal :self, rel.name
42
- assert_equal '/users/1', rel.href
43
- assert_equal :get, rel.method
44
- assert_equal [:get], rel.available_methods.to_a
45
- assert_kind_of Addressable::Template, rel.href_template
46
- end
47
-
48
- def test_builds_rels_from_hash_index
49
- index = {
50
- 'self' => {:href => '/users/1'}
51
- }
52
-
53
- rels = Sawyer::Relation.from_links(nil, index)
54
-
55
- assert_equal 1, rels.size
56
- assert_equal [:self], rels.keys
57
- assert rel = rels[:self]
58
- assert_equal :self, rel.name
59
- assert_equal '/users/1', rel.href
60
- assert_equal :get, rel.method
61
- assert_equal [:get], rel.available_methods.to_a
62
- assert_kind_of Addressable::Template, rel.href_template
63
- end
64
-
65
- def test_builds_rels_from_nil
66
- rels = Sawyer::Relation.from_links nil, nil
67
- assert_equal 0, rels.size
68
- assert_equal [], rels.keys
69
- end
70
-
71
- def test_relation_api_calls
72
- agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
73
- conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
74
- conn.adapter :test do |stubs|
75
- stubs.get '/a/1' do
76
- [200, {}, '{}']
77
- end
78
- stubs.delete '/a/1' do
79
- [204, {}, '{}']
80
- end
81
- end
82
- end
83
-
84
- rel = Sawyer::Relation.new agent, :self, "/a/1", "get,put,delete"
85
- assert_equal :get, rel.method
86
- [:get, :put, :delete].each do |m|
87
- assert rel.available_methods.include?(m), "#{m.inspect} is not available: #{rel.available_methods.inspect}"
88
- end
89
-
90
- assert_equal 200, rel.call.status
91
- assert_equal 200, rel.call(:method => :head).status
92
- assert_equal 204, rel.call(nil, :method => :delete).status
93
- assert_raises ArgumentError do
94
- rel.call nil, :method => :post
95
- end
96
-
97
- assert_equal 200, rel.head.status
98
- assert_equal 200, rel.get.status
99
- assert_equal 204, rel.delete.status
100
-
101
- assert_raises ArgumentError do
102
- rel.post
103
- end
104
- end
105
-
106
- def test_relation_api_calls_with_uri_tempate
107
- agent = Sawyer::Agent.new "http://foo.com/a" do |conn|
108
- conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
109
- conn.adapter :test do |stubs|
110
- stubs.get '/octocat/hello' do |env|
111
- assert_equal "a=1&b=2", env[:url].query
112
- [200, {}, '{}']
113
- end
114
-
115
- stubs.get '/a' do
116
- [404, {}, '{}']
117
- end
118
- end
119
- end
120
-
121
- rel = Sawyer::Relation.new agent, :repo, "{/user,repo}{?a,b}"
122
-
123
- assert_equal '', rel.href
124
- assert_equal '/octocat', rel.href(:user => :octocat)
125
-
126
- assert_equal 404, rel.get.status
127
- assert_equal 200, rel.get(:uri => {'user' => 'octocat', 'repo' => 'hello', 'a' => 1, 'b' => 2}).status
128
- end
129
-
130
- def test_handles_invalid_uri
131
- hash = {:href => '/this has spaces', :method => 'post'}
132
- rel = Sawyer::Relation.from_link(nil, :self, hash)
133
-
134
- assert_equal :self, rel.name
135
- assert_equal '/this has spaces', rel.href
136
- end
137
-
138
- def test_allows_all_methods_when_not_in_strict_mode
139
-
140
- agent = Sawyer::Agent.new "http://foo.com/a/", :allow_undefined_methods => true do |conn|
141
- conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
142
- conn.adapter :test do |stubs|
143
- stubs.get '/a/1' do
144
- [200, {}, '{}']
145
- end
146
- stubs.delete '/a/1' do
147
- [204, {}, '{}']
148
- end
149
- stubs.post '/a/1' do
150
- [200, {}, '{}']
151
- end
152
- stubs.put '/a/1' do
153
- [204, {}, '{}']
154
- end
155
- end
156
- end
157
-
158
- rel = Sawyer::Relation.new agent, :self, "/a/1"
159
- assert_equal 200, rel.get.status
160
- assert_equal 200, rel.post.status
161
- assert_equal 204, rel.put.status
162
- assert_equal 204, rel.delete.status
163
- end
164
-
165
- def test_map_inspect
166
- map = Sawyer::Relation::Map.new
167
- hash = {:href => '/users/1', :method => 'post'}
168
- rel = Sawyer::Relation.from_link(nil, :self, hash)
169
- map << rel
170
-
171
- assert_equal "{:self_url=>\"/users/1\"}", map.inspect
172
- end
173
- end
174
- end
@@ -1,199 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- module Sawyer
4
- class ResourceTest < TestCase
5
-
6
- def setup
7
- @stubs = Faraday::Adapter::Test::Stubs.new
8
- @agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
9
- conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
10
- conn.adapter :test, @stubs
11
- end
12
- end
13
-
14
- def test_accessible_keys
15
- res = Resource.new @agent, :a => 1,
16
- :_links => {:self => {:href => '/'}}
17
-
18
- assert_equal 1, res.a
19
- assert res.rels[:self]
20
- assert_equal @agent, res.agent
21
- assert_equal 1, res.fields.size
22
- assert res.fields.include?(:a)
23
- end
24
-
25
- def test_clashing_keys
26
- res = Resource.new @agent, :agent => 1, :rels => 2, :fields => 3,
27
- :_links => {:self => {:href => '/'}}
28
-
29
- assert_equal 1, res.agent
30
- assert_equal 2, res.rels
31
- assert_equal 3, res.fields
32
-
33
- assert res._rels[:self]
34
- assert_equal @agent, res._agent
35
- assert_equal 3, res._fields.size
36
- [:agent, :rels, :fields].each do |f|
37
- assert res._fields.include?(f)
38
- end
39
- end
40
-
41
- def test_nested_object
42
- res = Resource.new @agent,
43
- :user => {:id => 1, :_links => {:self => {:href => '/users/1'}}},
44
- :_links => {:self => {:href => '/'}}
45
-
46
- assert_equal '/', res.rels[:self].href
47
- assert_kind_of Resource, res.user
48
- assert_equal 1, res.user.id
49
- assert_equal '/users/1', res.user.rels[:self].href
50
- end
51
-
52
- def test_nested_collection
53
- res = Resource.new @agent,
54
- :users => [{:id => 1, :_links => {:self => {:href => '/users/1'}}}],
55
- :_links => {:self => {:href => '/'}}
56
-
57
- assert_equal '/', res.rels[:self].href
58
- assert_kind_of Array, res.users
59
-
60
- assert user = res.users.first
61
- assert_kind_of Resource, user
62
- assert_equal 1, user.id
63
- assert_equal '/users/1', user.rels[:self].href
64
- end
65
-
66
- def test_attribute_predicates
67
- res = Resource.new @agent, :a => 1, :b => true, :c => nil, :d => false
68
-
69
- assert res.a?
70
- assert res.b?
71
- assert !res.c?
72
- assert !res.d?
73
- end
74
-
75
- def test_attribute_setter
76
- res = Resource.new @agent, :a => 1
77
- assert_equal 1, res.a
78
- assert !res.key?(:b)
79
-
80
- res.b = 2
81
- assert_equal 2, res.b
82
- assert res.key?(:b)
83
- end
84
-
85
- def test_dynamic_attribute_methods_from_getter
86
- res = Resource.new @agent, :a => 1
87
- assert res.key?(:a)
88
- assert res.respond_to?(:a)
89
- assert res.respond_to?(:a=)
90
-
91
- assert_equal 1, res.a
92
- assert res.respond_to?(:a)
93
- assert res.respond_to?(:a=)
94
- end
95
-
96
- def test_nillable_attribute_getters
97
- res = Resource.new @agent, :a => 1
98
- assert !res.key?(:b)
99
- assert !res.respond_to?(:b)
100
- assert !res.respond_to?(:b=)
101
- assert_nil res.b
102
- res.b
103
- end
104
-
105
- def test_dynamic_attribute_methods_from_setter
106
- res = Resource.new @agent, :a => 1
107
- assert !res.key?(:b)
108
- assert !res.respond_to?(:b)
109
- assert !res.respond_to?(:b=)
110
-
111
- res.b = 1
112
- assert res.key?(:b)
113
- assert res.respond_to?(:b)
114
- assert res.respond_to?(:b=)
115
- end
116
-
117
- def test_attrs
118
- res = Resource.new @agent, :a => 1
119
- hash = {:a => 1 }
120
- assert_equal hash, res.attrs
121
- end
122
-
123
- def test_to_h
124
- res = Resource.new @agent, :a => 1
125
- hash = {:a => 1 }
126
- assert_equal hash, res.to_h
127
- end
128
-
129
- def test_to_h_with_nesting
130
- res = Resource.new @agent, :a => {:b => 1}
131
- hash = {:a => {:b => 1}}
132
- assert_equal hash, res.to_h
133
- end
134
-
135
- def test_to_attrs_for_sawyer_resource_arrays
136
- res = Resource.new @agent, :a => 1, :b => [Resource.new(@agent, :a => 2)]
137
- hash = {:a => 1, :b => [{:a => 2}]}
138
- assert_equal hash, res.to_attrs
139
- end
140
-
141
- def test_handle_hash_notation_with_string_key
142
- res = Resource.new @agent, :a => 1
143
- assert_equal 1, res['a']
144
-
145
- res[:b] = 2
146
- assert_equal 2, res.b
147
- end
148
-
149
- def test_simple_rel_parsing
150
- @agent.links_parser = Sawyer::LinkParsers::Simple.new
151
- res = Resource.new @agent,
152
- :url => '/',
153
- :user => {
154
- :id => 1,
155
- :url => '/users/1',
156
- :followers_url => '/users/1/followers'
157
- }
158
-
159
- assert_equal '/', res.rels[:self].href
160
- assert_kind_of Resource, res.user
161
- assert_equal '/', res.url
162
- assert_equal 1, res.user.id
163
- assert_equal '/users/1', res.user.rels[:self].href
164
- assert_equal '/users/1', res.user.url
165
- assert_equal '/users/1/followers', res.user.rels[:followers].href
166
- assert_equal '/users/1/followers', res.user.followers_url
167
- end
168
-
169
- def test_handle_yaml_dump
170
- require 'yaml'
171
- res = Resource.new @agent, :a => 1
172
- YAML.dump(res)
173
- end
174
-
175
- def test_handle_marshal_dump
176
- dump = Marshal.dump(Resource.new(@agent, :a => 1))
177
- resource = Marshal.load(dump)
178
- assert_equal 1, resource.a
179
- end
180
-
181
- def test_inspect
182
- resource = Resource.new @agent, :a => 1
183
- assert_equal "{:a=>1}", resource.inspect
184
- end
185
-
186
- def test_each
187
- resource = Resource.new @agent, { :a => 1, :b => 2 }
188
- output = []
189
- resource.each { |k,v| output << [k,v] }
190
- assert_equal [[:a, 1], [:b, 2]], output
191
- end
192
-
193
- def test_enumerable
194
- resource = Resource.new @agent, { :a => 1, :b => 2 }
195
- enum = resource.map
196
- assert_equal Enumerator, enum.class
197
- end
198
- end
199
- end
@@ -1,77 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- module Sawyer
4
- class ResponseTest < TestCase
5
- def setup
6
- @now = Time.now
7
- @stubs = Faraday::Adapter::Test::Stubs.new
8
- @agent = Sawyer::Agent.new "http://foo.com" do |conn|
9
- conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
10
- conn.adapter :test, @stubs do |stub|
11
- stub.get '/' do
12
- [200, {
13
- 'Content-Type' => 'application/json',
14
- 'Link' => '</starred?page=2>; rel="next", </starred?page=19>; rel="last"'
15
- }, Sawyer::Agent.encode(
16
- :a => 1,
17
- :_links => {
18
- :self => {:href => '/a', :method => 'POST'}
19
- }
20
- )]
21
- end
22
-
23
- stub.get '/emails' do
24
- emails = %w(rick@example.com technoweenie@example.com)
25
- [200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(emails)]
26
- end
27
- end
28
- end
29
-
30
- @res = @agent.start
31
- assert_kind_of Sawyer::Response, @res
32
- end
33
-
34
- def test_gets_status
35
- assert_equal 200, @res.status
36
- end
37
-
38
- def test_gets_headers
39
- assert_equal 'application/json', @res.headers['content-type']
40
- end
41
-
42
- def test_gets_body
43
- assert_equal 1, @res.data.a
44
- assert_equal [:a], @res.data.fields.to_a
45
- end
46
-
47
- def test_gets_rels
48
- assert_equal '/starred?page=2', @res.rels[:next].href
49
- assert_equal :get, @res.rels[:next].method
50
- assert_equal '/starred?page=19', @res.rels[:last].href
51
- assert_equal :get, @res.rels[:next].method
52
- assert_equal '/a', @res.data.rels[:self].href
53
- assert_equal :post, @res.data.rels[:self].method
54
- end
55
-
56
- def test_gets_response_timing
57
- assert @res.timing > 0
58
- assert @res.time >= @now
59
- end
60
-
61
- def test_makes_request_from_relation
62
- @stubs.post '/a' do
63
- [201, {'Content-Type' => 'application/json'}, ""]
64
- end
65
-
66
- res = @res.data.rels[:self].call
67
- assert_equal 201, res.status
68
- assert_nil res.data
69
- end
70
-
71
- def test_handles_arrays_of_strings
72
- res = @agent.call(:get, '/emails')
73
- assert_equal 'rick@example.com', res.data.first
74
- end
75
- end
76
- end
77
-