api-model 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/Gemfile.lock +7 -5
- data/README.md +3 -0
- data/api-model.gemspec +2 -1
- data/lib/api-model.rb +28 -22
- data/lib/api_model/configuration.rb +39 -0
- data/lib/api_model/http_request.rb +20 -7
- data/lib/api_model/initializer.rb +4 -3
- data/lib/api_model/response.rb +34 -9
- data/lib/api_model/rest_methods.rb +21 -0
- data/spec/api-model/api_model_spec.rb +130 -0
- data/spec/api-model/configuration_spec.rb +78 -0
- data/spec/api-model/http_request_spec.rb +74 -0
- data/spec/{lib → api-model}/initializer_spec.rb +5 -5
- data/spec/api-model/response_spec.rb +186 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/fixtures/cars.yml +84 -0
- data/spec/support/fixtures/errors.yml +57 -0
- data/spec/support/fixtures/posts.yml +56 -0
- data/spec/support/fixtures/users.yml +57 -0
- data/spec/support/mock_models/banana.rb +3 -6
- data/spec/support/mock_models/blog_post.rb +2 -1
- data/spec/support/mock_models/car.rb +11 -0
- data/spec/support/mock_models/multiple_hosts.rb +6 -2
- data/spec/support/mock_models/user.rb +4 -0
- metadata +39 -12
- data/spec/lib/api_host_spec.rb +0 -20
- data/spec/lib/api_model_spec.rb +0 -34
- data/spec/lib/http_request_spec.rb +0 -43
- data/spec/lib/response_spec.rb +0 -89
@@ -0,0 +1,57 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api-model-specs.com/needs_auth
|
6
|
+
headers:
|
7
|
+
User-Agent:
|
8
|
+
- Typhoeus - https://github.com/typhoeus/typhoeus
|
9
|
+
response:
|
10
|
+
status:
|
11
|
+
code: 401
|
12
|
+
message: OK
|
13
|
+
headers:
|
14
|
+
Server:
|
15
|
+
- nginx/1.4.1
|
16
|
+
Date:
|
17
|
+
- Thu, 28 Nov 2013 16:02:56 GMT
|
18
|
+
Content-Type:
|
19
|
+
- text/plain; charset=utf-8
|
20
|
+
Content-Length:
|
21
|
+
- '248'
|
22
|
+
Connection:
|
23
|
+
- keep-alive
|
24
|
+
body:
|
25
|
+
encoding: UTF-8
|
26
|
+
string: "{}"
|
27
|
+
http_version:
|
28
|
+
recorded_at: Thu, 28 Nov 2013 16:02:20 GMT
|
29
|
+
|
30
|
+
- request:
|
31
|
+
method: get
|
32
|
+
uri: http://api-model-specs.com/not_found
|
33
|
+
headers:
|
34
|
+
User-Agent:
|
35
|
+
- Typhoeus - https://github.com/typhoeus/typhoeus
|
36
|
+
response:
|
37
|
+
status:
|
38
|
+
code: 404
|
39
|
+
message: OK
|
40
|
+
headers:
|
41
|
+
Server:
|
42
|
+
- nginx/1.4.1
|
43
|
+
Date:
|
44
|
+
- Thu, 28 Nov 2013 16:02:56 GMT
|
45
|
+
Content-Type:
|
46
|
+
- text/plain; charset=utf-8
|
47
|
+
Content-Length:
|
48
|
+
- '248'
|
49
|
+
Connection:
|
50
|
+
- keep-alive
|
51
|
+
body:
|
52
|
+
encoding: UTF-8
|
53
|
+
string: "{}"
|
54
|
+
http_version:
|
55
|
+
recorded_at: Thu, 28 Nov 2013 16:02:20 GMT
|
56
|
+
|
57
|
+
recorded_with: VCR 2.8.0
|
@@ -27,6 +27,33 @@ http_interactions:
|
|
27
27
|
http_version:
|
28
28
|
recorded_at: Thu, 28 Nov 2013 16:02:20 GMT
|
29
29
|
|
30
|
+
- request:
|
31
|
+
method: post
|
32
|
+
uri: http://api-model-specs.com/posts
|
33
|
+
headers:
|
34
|
+
User-Agent:
|
35
|
+
- Typhoeus - https://github.com/typhoeus/typhoeus
|
36
|
+
response:
|
37
|
+
status:
|
38
|
+
code: 200
|
39
|
+
message: OK
|
40
|
+
headers:
|
41
|
+
Server:
|
42
|
+
- nginx/1.4.1
|
43
|
+
Date:
|
44
|
+
- Thu, 28 Nov 2013 16:02:56 GMT
|
45
|
+
Content-Type:
|
46
|
+
- text/plain; charset=utf-8
|
47
|
+
Content-Length:
|
48
|
+
- '248'
|
49
|
+
Connection:
|
50
|
+
- keep-alive
|
51
|
+
body:
|
52
|
+
encoding: UTF-8
|
53
|
+
string: "[{\"name\":\"foo\"},{\"name\":\"bar\"}]"
|
54
|
+
http_version:
|
55
|
+
recorded_at: Thu, 28 Nov 2013 16:02:20 GMT
|
56
|
+
|
30
57
|
- request:
|
31
58
|
method: get
|
32
59
|
uri: http://api-model-specs.com/single_post
|
@@ -54,4 +81,33 @@ http_interactions:
|
|
54
81
|
http_version:
|
55
82
|
recorded_at: Thu, 28 Nov 2013 16:02:20 GMT
|
56
83
|
|
84
|
+
- request:
|
85
|
+
method: post
|
86
|
+
uri: http://api-model-specs.com/create_with_json
|
87
|
+
headers:
|
88
|
+
User-Agent:
|
89
|
+
- Typhoeus - https://github.com/typhoeus/typhoeus
|
90
|
+
body:
|
91
|
+
string: "{\"name\":\"foobarbaz\"}"
|
92
|
+
response:
|
93
|
+
status:
|
94
|
+
code: 200
|
95
|
+
message: OK
|
96
|
+
headers:
|
97
|
+
Server:
|
98
|
+
- nginx/1.4.1
|
99
|
+
Date:
|
100
|
+
- Thu, 28 Nov 2013 16:02:56 GMT
|
101
|
+
Content-Type:
|
102
|
+
- text/plain; charset=utf-8
|
103
|
+
Content-Length:
|
104
|
+
- '248'
|
105
|
+
Connection:
|
106
|
+
- keep-alive
|
107
|
+
body:
|
108
|
+
encoding: UTF-8
|
109
|
+
string: "{\"name\":\"foobarbaz\"}"
|
110
|
+
http_version:
|
111
|
+
recorded_at: Thu, 28 Nov 2013 16:02:20 GMT
|
112
|
+
|
57
113
|
recorded_with: VCR 2.8.0
|
@@ -0,0 +1,57 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api-model-specs.com/users
|
6
|
+
headers:
|
7
|
+
User-Agent:
|
8
|
+
- Typhoeus - https://github.com/typhoeus/typhoeus
|
9
|
+
response:
|
10
|
+
status:
|
11
|
+
code: 200
|
12
|
+
message: OK
|
13
|
+
headers:
|
14
|
+
Server:
|
15
|
+
- nginx/1.4.1
|
16
|
+
Date:
|
17
|
+
- Thu, 28 Nov 2013 16:02:56 GMT
|
18
|
+
Content-Type:
|
19
|
+
- text/plain; charset=utf-8
|
20
|
+
Content-Length:
|
21
|
+
- '248'
|
22
|
+
Connection:
|
23
|
+
- keep-alive
|
24
|
+
body:
|
25
|
+
encoding: UTF-8
|
26
|
+
string: "{\"users\":[{\"name\":\"foo\"}, {\"name\":\"bar\"}, {\"name\":\"baz\"}]}"
|
27
|
+
http_version:
|
28
|
+
recorded_at: Thu, 28 Nov 2013 16:02:20 GMT
|
29
|
+
|
30
|
+
- request:
|
31
|
+
method: get
|
32
|
+
uri: http://api-model-specs.com/search
|
33
|
+
headers:
|
34
|
+
User-Agent:
|
35
|
+
- Typhoeus - https://github.com/typhoeus/typhoeus
|
36
|
+
response:
|
37
|
+
status:
|
38
|
+
code: 200
|
39
|
+
message: OK
|
40
|
+
headers:
|
41
|
+
Server:
|
42
|
+
- nginx/1.4.1
|
43
|
+
Date:
|
44
|
+
- Thu, 28 Nov 2013 16:02:56 GMT
|
45
|
+
Content-Type:
|
46
|
+
- text/plain; charset=utf-8
|
47
|
+
Content-Length:
|
48
|
+
- '248'
|
49
|
+
Connection:
|
50
|
+
- keep-alive
|
51
|
+
body:
|
52
|
+
encoding: UTF-8
|
53
|
+
string: "{\"search\":{\"results\":{\"users\":[{\"name\":\"foo\"}, {\"name\":\"bar\"}, {\"name\":\"baz\"}]}}}"
|
54
|
+
http_version:
|
55
|
+
recorded_at: Thu, 28 Nov 2013 16:02:20 GMT
|
56
|
+
|
57
|
+
recorded_with: VCR 2.8.0
|
@@ -1,9 +1,13 @@
|
|
1
1
|
class MultipleHostsFoo < ApiModel::Base
|
2
|
-
|
2
|
+
api_config do |config|
|
3
|
+
config.host = "http://foo.com"
|
4
|
+
end
|
3
5
|
end
|
4
6
|
|
5
7
|
class MultipleHostsBar < ApiModel::Base
|
6
|
-
|
8
|
+
api_config do |config|
|
9
|
+
config.host = "http://bar.com"
|
10
|
+
end
|
7
11
|
end
|
8
12
|
|
9
13
|
class MultipleHostsNone < ApiModel::Base
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Damien Timewell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: hashie
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -117,24 +131,32 @@ extra_rdoc_files: []
|
|
117
131
|
files:
|
118
132
|
- .gitignore
|
119
133
|
- .rspec
|
134
|
+
- .travis.yml
|
120
135
|
- Gemfile
|
121
136
|
- Gemfile.lock
|
122
137
|
- README.md
|
123
138
|
- api-model.gemspec
|
124
139
|
- lib/api-model.rb
|
140
|
+
- lib/api_model/configuration.rb
|
125
141
|
- lib/api_model/http_request.rb
|
126
142
|
- lib/api_model/initializer.rb
|
127
143
|
- lib/api_model/response.rb
|
128
|
-
-
|
129
|
-
- spec/
|
130
|
-
- spec/
|
131
|
-
- spec/
|
132
|
-
- spec/
|
144
|
+
- lib/api_model/rest_methods.rb
|
145
|
+
- spec/api-model/api_model_spec.rb
|
146
|
+
- spec/api-model/configuration_spec.rb
|
147
|
+
- spec/api-model/http_request_spec.rb
|
148
|
+
- spec/api-model/initializer_spec.rb
|
149
|
+
- spec/api-model/response_spec.rb
|
133
150
|
- spec/spec_helper.rb
|
151
|
+
- spec/support/fixtures/cars.yml
|
152
|
+
- spec/support/fixtures/errors.yml
|
134
153
|
- spec/support/fixtures/posts.yml
|
154
|
+
- spec/support/fixtures/users.yml
|
135
155
|
- spec/support/mock_models/banana.rb
|
136
156
|
- spec/support/mock_models/blog_post.rb
|
157
|
+
- spec/support/mock_models/car.rb
|
137
158
|
- spec/support/mock_models/multiple_hosts.rb
|
159
|
+
- spec/support/mock_models/user.rb
|
138
160
|
homepage: https://github.com/iZettle/api-model
|
139
161
|
licenses: []
|
140
162
|
metadata: {}
|
@@ -159,14 +181,19 @@ signing_key:
|
|
159
181
|
specification_version: 4
|
160
182
|
summary: A simple way of interacting with rest APIs
|
161
183
|
test_files:
|
162
|
-
- spec/
|
163
|
-
- spec/
|
164
|
-
- spec/
|
165
|
-
- spec/
|
166
|
-
- spec/
|
184
|
+
- spec/api-model/api_model_spec.rb
|
185
|
+
- spec/api-model/configuration_spec.rb
|
186
|
+
- spec/api-model/http_request_spec.rb
|
187
|
+
- spec/api-model/initializer_spec.rb
|
188
|
+
- spec/api-model/response_spec.rb
|
167
189
|
- spec/spec_helper.rb
|
190
|
+
- spec/support/fixtures/cars.yml
|
191
|
+
- spec/support/fixtures/errors.yml
|
168
192
|
- spec/support/fixtures/posts.yml
|
193
|
+
- spec/support/fixtures/users.yml
|
169
194
|
- spec/support/mock_models/banana.rb
|
170
195
|
- spec/support/mock_models/blog_post.rb
|
196
|
+
- spec/support/mock_models/car.rb
|
171
197
|
- spec/support/mock_models/multiple_hosts.rb
|
198
|
+
- spec/support/mock_models/user.rb
|
172
199
|
has_rdoc:
|
data/spec/lib/api_host_spec.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'support/mock_models/banana'
|
3
|
-
require 'support/mock_models/multiple_hosts'
|
4
|
-
|
5
|
-
describe ApiModel do
|
6
|
-
describe "api_host" do
|
7
|
-
it "should be possible to set the base api_host" do
|
8
|
-
Banana.api_host = "http://api-model-specs.com"
|
9
|
-
Banana.api_host.should eq "http://api-model-specs.com"
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "with combinations of setting different hosts" do
|
13
|
-
it "should no override each other" do
|
14
|
-
MultipleHostsFoo.api_host.should eq("http://foo.com")
|
15
|
-
MultipleHostsBar.api_host.should eq("http://bar.com")
|
16
|
-
MultipleHostsNone.api_host.should eq("")
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/spec/lib/api_model_spec.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'support/mock_models/blog_post'
|
3
|
-
|
4
|
-
describe ApiModel do
|
5
|
-
|
6
|
-
describe "retrieving a single object" do
|
7
|
-
describe "with the default builder" do
|
8
|
-
let(:blog_post) do
|
9
|
-
VCR.use_cassette('posts') do
|
10
|
-
BlogPost.get_json "http://api-model-specs.com/single_post"
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
it "should run the request and objectify the response hash" do
|
15
|
-
blog_post.should be_a(BlogPost)
|
16
|
-
blog_post.name.should eq "foo"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "with a custom builder" do
|
21
|
-
let(:custom_built_blog_post) do
|
22
|
-
VCR.use_cassette('posts') do
|
23
|
-
BlogPost.get_json "http://api-model-specs.com/single_post", builder: BlogPost::CustomBuilder.new
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should be possible to use a custom builder class when objectifing" do
|
28
|
-
custom_built_blog_post.should be_a(BlogPost)
|
29
|
-
custom_built_blog_post.title.should eq "FOOBAR"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'support/mock_models/blog_post'
|
3
|
-
|
4
|
-
describe ApiModel::HttpRequest do
|
5
|
-
|
6
|
-
describe "default attributes" do
|
7
|
-
subject { ApiModel::HttpRequest.new }
|
8
|
-
|
9
|
-
it "should default #method to :get" do
|
10
|
-
subject.method.should eq :get
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should default #options to a blank hash" do
|
14
|
-
subject.options.should eq Hash.new
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "using api_host" do
|
19
|
-
let(:blog_post) do
|
20
|
-
BlogPost.api_host = "http://api-model-specs.com"
|
21
|
-
|
22
|
-
VCR.use_cassette('posts') do
|
23
|
-
BlogPost.get_json "/single_post"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should be used with #path to generate a #full_path" do
|
28
|
-
blog_post.http_response.request.url.should eq "http://api-model-specs.com/single_post"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "sending a GET request" do
|
33
|
-
let(:request) { ApiModel::HttpRequest.new path: "http://api-model-specs.com/posts", method: :get }
|
34
|
-
|
35
|
-
it "should use typhoeus to send a request" do
|
36
|
-
VCR.use_cassette('posts') do
|
37
|
-
request.run
|
38
|
-
end
|
39
|
-
|
40
|
-
request.api_call.success?.should eq true
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
data/spec/lib/response_spec.rb
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'support/mock_models/blog_post'
|
3
|
-
|
4
|
-
describe ApiModel::Response do
|
5
|
-
|
6
|
-
let(:valid_response) do
|
7
|
-
VCR.use_cassette('posts') do
|
8
|
-
ApiModel::HttpRequest.new(path: "http://api-model-specs.com/single_post", method: :get).run
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "parsing the json body" do
|
13
|
-
it "should produce a hash given valid json" do
|
14
|
-
valid_response.json_response_body.should be_a(Hash)
|
15
|
-
valid_response.json_response_body["name"].should eq "foo"
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should catch errors from parsing invalid json" do
|
19
|
-
valid_response.stub_chain(:http_response, :body).and_return "blah"
|
20
|
-
ApiModel::Log.should_receive(:info).with "Could not parse JSON response: blah"
|
21
|
-
|
22
|
-
expect {
|
23
|
-
valid_response.json_response_body
|
24
|
-
}.to_not raise_error
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe "#build" do
|
29
|
-
it "should use the builder.build method if present" do
|
30
|
-
builder = double
|
31
|
-
builder.should_receive(:build).with something: "foo"
|
32
|
-
|
33
|
-
valid_response.build builder, something: "foo"
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should use builder.new if there's no builder.build method" do
|
37
|
-
builder = double
|
38
|
-
builder.should_receive(:new).with something_else: "hi"
|
39
|
-
|
40
|
-
valid_response.build builder, something_else: "hi"
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe "#build_objects" do
|
45
|
-
let(:single_object) do
|
46
|
-
valid_response.stub(:json_response_body).and_return name: "foo"
|
47
|
-
valid_response.build_objects BlogPost
|
48
|
-
end
|
49
|
-
|
50
|
-
let(:array_of_objects) do
|
51
|
-
valid_response.stub(:json_response_body).and_return [{name: "foo"}, {name: "bar"}]
|
52
|
-
valid_response.build_objects BlogPost
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should build a single object" do
|
56
|
-
single_object.should be_a(BlogPost)
|
57
|
-
single_object.name.should eq "foo"
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should build an array of objects" do
|
61
|
-
array_of_objects[0].should be_a(BlogPost)
|
62
|
-
array_of_objects[0].name.should eq "foo"
|
63
|
-
|
64
|
-
array_of_objects[1].should be_a(BlogPost)
|
65
|
-
array_of_objects[1].name.should eq "bar"
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should include the Typhoeus::Response object" do
|
69
|
-
single_object.http_response.should be_a(Typhoeus::Response)
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should include the #json_response_body" do
|
73
|
-
single_object.json_response_body.should eq name: "foo"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
describe "passing core methods down to the built class" do
|
78
|
-
|
79
|
-
ApiModel::Response::FALL_THROUGH_METHODS.each do |fall_trhough_method|
|
80
|
-
it "should pass ##{fall_trhough_method} on the built object class" do
|
81
|
-
allow_message_expectations_on_nil
|
82
|
-
valid_response.objects.should_receive(fall_trhough_method)
|
83
|
-
valid_response.send fall_trhough_method
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|