rapidash 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +46 -9
- data/examples/github.rb +6 -6
- data/lib/rapidash.rb +3 -1
- data/lib/rapidash/base.rb +10 -8
- data/lib/rapidash/client.rb +54 -2
- data/lib/rapidash/http_client.rb +15 -30
- data/lib/rapidash/oauth_client.rb +10 -10
- data/lib/rapidash/resourceable.rb +32 -14
- data/lib/rapidash/response.rb +12 -11
- data/lib/rapidash/test_client.rb +0 -1
- data/lib/rapidash/urlable.rb +0 -2
- data/lib/rapidash/version.rb +1 -1
- data/rapidash.gemspec +2 -0
- data/spec/rapidash/base_spec.rb +19 -7
- data/spec/rapidash/client_spec.rb +99 -0
- data/spec/rapidash/http_client_spec.rb +41 -56
- data/spec/rapidash/integration/test_client_spec.rb +2 -2
- data/spec/rapidash/oauth_client_spec.rb +27 -27
- data/spec/rapidash/resourceable_spec.rb +140 -9
- data/spec/rapidash/response_spec.rb +31 -0
- data/spec/rapidash/urlable_spec.rb +1 -1
- data/spec/spec_helper.rb +4 -1
- metadata +31 -6
- data/lib/rapidash/clientable.rb +0 -35
- data/spec/rapidash/clientable_spec.rb +0 -88
@@ -1,6 +1,6 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
class Rapidash::
|
3
|
+
class Rapidash::Repo
|
4
4
|
attr_accessor :client, :args
|
5
5
|
def initialize(client, *args)
|
6
6
|
@client = client
|
@@ -8,7 +8,7 @@ class Rapidash::Repos
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
class Rapidash::
|
11
|
+
class Rapidash::User
|
12
12
|
include Rapidash::Resourceable
|
13
13
|
attr_accessor :client, :url
|
14
14
|
resource :repos
|
@@ -18,11 +18,22 @@ class Rapidash::Users
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
class
|
21
|
+
class User
|
22
22
|
def initialize(*args)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
class AdminUser
|
27
|
+
def initialize(*args)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class CoreMembers
|
32
|
+
def initialize(*args)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
26
37
|
class Rapidash::ClientTester
|
27
38
|
include Rapidash::Resourceable
|
28
39
|
resource :users
|
@@ -47,11 +58,82 @@ describe Rapidash::Resourceable do
|
|
47
58
|
|
48
59
|
end
|
49
60
|
|
61
|
+
|
62
|
+
describe "instance methods" do
|
63
|
+
let(:client) { ClientTester.new }
|
64
|
+
|
65
|
+
describe ".resource" do
|
66
|
+
|
67
|
+
|
68
|
+
it "should create a Rapidash::Base" do
|
69
|
+
client.resource(:users, 1).class.should eql(Rapidash::Base)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should set the url to the resource name" do
|
73
|
+
resource = client.resource(:users)
|
74
|
+
resource.url.should eql("users")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should pass the id through if specified" do
|
78
|
+
resource = client.resource(:users, 1)
|
79
|
+
resource.url.should eql("users/1")
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should pass the previous url through" do
|
83
|
+
def client.url
|
84
|
+
"base"
|
85
|
+
end
|
86
|
+
resource = client.resource(:users, 1)
|
87
|
+
resource.url.should eql("base/users/1")
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should pass the client through" do
|
91
|
+
resource = client.resource(:users, 1)
|
92
|
+
resource.client.should eql(client)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should allow an explicit url to be sent" do
|
96
|
+
resource = client.resource(:users, 1, :url => "people")
|
97
|
+
resource.url.should eql("people/1")
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should be chainable" do
|
101
|
+
resource = client.resource(:users, 1).resource(:comments, 2)
|
102
|
+
resource.url.should eql("users/1/comments/2")
|
103
|
+
resource.client.should eql(client)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe ".resource!" do
|
108
|
+
it "should call the call! method on a resource" do
|
109
|
+
resource = mock
|
110
|
+
Rapidash::Base.stub(:new).and_return(resource)
|
111
|
+
resource.should_receive(:call!)
|
112
|
+
client.resource!(:users, 1)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
50
117
|
describe "#resource" do
|
51
118
|
it "should add a method with the name of the argument" do
|
52
119
|
Rapidash::ClientTester.new.methods.map { |m| m.to_sym }.should include(:users)
|
53
120
|
end
|
54
121
|
|
122
|
+
it "should not fail when presented with a multi-word resource" do
|
123
|
+
expect {
|
124
|
+
class ClientTester
|
125
|
+
resource :admin_users
|
126
|
+
end
|
127
|
+
}.to_not raise_error(NameError)
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should load the plural class with a warning if the singular is not defined" do
|
131
|
+
Kernel.should_receive(:warn).with("[DEPRECATED] - RAPIDASH WARNING using CoreMembers instead of CoreMember - please either use `CoreMember` or set the class name with `resource core_members, :class_name => CoreMembers` implicit plural naming will be deprecated in Rapidash 1.0")
|
132
|
+
class ClientTester
|
133
|
+
resource :core_members
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
55
137
|
it "should add a bang method with the name of the argument" do
|
56
138
|
Rapidash::ClientTester.new.methods.map { |m| m.to_sym }.should include(:users!)
|
57
139
|
end
|
@@ -64,18 +146,18 @@ describe Rapidash::Resourceable do
|
|
64
146
|
|
65
147
|
describe ".users" do
|
66
148
|
it "should return an instance of the resource" do
|
67
|
-
Rapidash::ClientTester.new.users.class.should eql(Rapidash::
|
149
|
+
Rapidash::ClientTester.new.users.class.should eql(Rapidash::User)
|
68
150
|
end
|
69
151
|
|
70
152
|
it "should not use a namespace if not in a module" do
|
71
|
-
ClientTester.new.users.class.should eql(
|
153
|
+
ClientTester.new.users.class.should eql(User)
|
72
154
|
end
|
73
155
|
end
|
74
156
|
|
75
157
|
describe ".tickets!" do
|
76
158
|
it "should return an instance of the resource and call it" do
|
77
159
|
users = mock
|
78
|
-
Rapidash::
|
160
|
+
Rapidash::User.should_receive(:new).and_return(users)
|
79
161
|
users.should_receive(:call!)
|
80
162
|
Rapidash::ClientTester.new.users!
|
81
163
|
end
|
@@ -84,22 +166,71 @@ describe Rapidash::Resourceable do
|
|
84
166
|
describe "chaining resources" do
|
85
167
|
it "should allow resources to be nested" do
|
86
168
|
client = mock
|
87
|
-
users = Rapidash::
|
169
|
+
users = Rapidash::User.new(client)
|
88
170
|
users.methods.map { |m| m.to_sym }.should include(:repos)
|
89
171
|
users.methods.map { |m| m.to_sym }.should include(:repos!)
|
90
172
|
end
|
91
173
|
|
92
174
|
it "should maintain the client across resources " do
|
93
175
|
client = mock
|
94
|
-
users = Rapidash::
|
176
|
+
users = Rapidash::User.new(client)
|
95
177
|
users.repos.instance_variable_get(:@client).should eql(client)
|
96
178
|
end
|
97
179
|
|
98
180
|
it "should maintain the URL when chaining" do
|
99
181
|
client = mock
|
100
|
-
users = Rapidash::
|
182
|
+
users = Rapidash::User.new(client)
|
101
183
|
users.repos.instance_variable_get(:@args)[0].keys.should include(:previous_url)
|
102
184
|
end
|
185
|
+
|
186
|
+
it "should maintain the URL as well as the options when chaining" do
|
187
|
+
client = mock
|
188
|
+
users = Rapidash::User.new(client)
|
189
|
+
repos = users.repos(:params => {:foo => :bar})
|
190
|
+
repos.instance_variable_get(:@args)[0].should include(:params)
|
191
|
+
repos.instance_variable_get(:@args)[0].should include(:previous_url)
|
192
|
+
end
|
103
193
|
end
|
104
194
|
|
195
|
+
describe "resource with module" do
|
196
|
+
module Facebook
|
197
|
+
class User
|
198
|
+
def initialize(*args)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
class Posts
|
203
|
+
def initialize(*args)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
module Its
|
209
|
+
module A
|
210
|
+
class DeepResource
|
211
|
+
def initialize(*args)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
class ModuleTester
|
218
|
+
include Rapidash::Resourceable
|
219
|
+
resource :users, :class_name => "Facebook::User"
|
220
|
+
resource :posts, :class_name => Facebook::Posts
|
221
|
+
resource :deep_resources, :class_name => Its::A::DeepResource
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should find user in another module" do
|
225
|
+
ModuleTester.new.users.class.should eql(Facebook::User)
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should allow a plural class name" do
|
229
|
+
ModuleTester.new.posts.class.should eql(Facebook::Posts)
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should find deep_resource in a nested module" do
|
233
|
+
ModuleTester.new.deep_resources.class.should eql(Its::A::DeepResource)
|
234
|
+
end
|
235
|
+
end
|
105
236
|
end
|
@@ -31,6 +31,26 @@ def valid_js_response_object
|
|
31
31
|
})
|
32
32
|
end
|
33
33
|
|
34
|
+
def valid_null_response_object
|
35
|
+
body = nil.to_json
|
36
|
+
OpenStruct.new({
|
37
|
+
:headers => {
|
38
|
+
"content-type" => "application/json"
|
39
|
+
},
|
40
|
+
:body => body
|
41
|
+
})
|
42
|
+
end
|
43
|
+
|
44
|
+
def valid_empty_response_object
|
45
|
+
body = ""
|
46
|
+
OpenStruct.new({
|
47
|
+
:headers => {
|
48
|
+
"content-type" => "application/json"
|
49
|
+
},
|
50
|
+
:body => body
|
51
|
+
})
|
52
|
+
end
|
53
|
+
|
34
54
|
|
35
55
|
def valid_response_array
|
36
56
|
body = [{"foo" => "bar" }, {"baz" => "bra"}].to_json
|
@@ -85,6 +105,17 @@ describe Rapidash::Response do
|
|
85
105
|
response.foo.should eql("bar")
|
86
106
|
end
|
87
107
|
|
108
|
+
it "should parse a JSON null" do
|
109
|
+
response = Rapidash::Response.new(valid_null_response_object)
|
110
|
+
response.should eql(nil)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should parse an empty body" do
|
114
|
+
response = Rapidash::Response.new(valid_empty_response_object)
|
115
|
+
response.should eql(nil)
|
116
|
+
end
|
117
|
+
|
118
|
+
|
88
119
|
it "should parse JSON Arrays" do
|
89
120
|
response = Rapidash::Response.new(valid_response_array)
|
90
121
|
response[0].foo.should eql("bar")
|
@@ -28,7 +28,7 @@ describe Rapidash::Urlable do
|
|
28
28
|
|
29
29
|
describe "#url" do
|
30
30
|
it "should override the initialize to set a url" do
|
31
|
-
ApiTesterNoUrl.new.instance_variable_get(:@url).should eql("
|
31
|
+
ApiTesterNoUrl.new.instance_variable_get(:@url).should eql("apitesternourls")
|
32
32
|
ApiTester.new.instance_variable_get(:@url).should eql("foo")
|
33
33
|
end
|
34
34
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rapidash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gary 'Gazler' Rennie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '2.8'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.7'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: json
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,6 +122,20 @@ dependencies:
|
|
108
122
|
- - ~>
|
109
123
|
- !ruby/object:Gem::Version
|
110
124
|
version: '1.2'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: activesupport
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
111
139
|
description: Evolve your API
|
112
140
|
email:
|
113
141
|
- gazler@gmail.com
|
@@ -126,7 +154,6 @@ files:
|
|
126
154
|
- lib/rapidash.rb
|
127
155
|
- lib/rapidash/base.rb
|
128
156
|
- lib/rapidash/client.rb
|
129
|
-
- lib/rapidash/clientable.rb
|
130
157
|
- lib/rapidash/errors.rb
|
131
158
|
- lib/rapidash/http_client.rb
|
132
159
|
- lib/rapidash/oauth_client.rb
|
@@ -138,7 +165,6 @@ files:
|
|
138
165
|
- rapidash.gemspec
|
139
166
|
- spec/rapidash/base_spec.rb
|
140
167
|
- spec/rapidash/client_spec.rb
|
141
|
-
- spec/rapidash/clientable_spec.rb
|
142
168
|
- spec/rapidash/http_client_spec.rb
|
143
169
|
- spec/rapidash/integration/test_client_spec.rb
|
144
170
|
- spec/rapidash/oauth_client_spec.rb
|
@@ -167,14 +193,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
193
|
version: '0'
|
168
194
|
requirements: []
|
169
195
|
rubyforge_project:
|
170
|
-
rubygems_version: 2.0.
|
196
|
+
rubygems_version: 2.0.2
|
171
197
|
signing_key:
|
172
198
|
specification_version: 4
|
173
199
|
summary: An opinionated core for creating clients for RESTful APIs quickly
|
174
200
|
test_files:
|
175
201
|
- spec/rapidash/base_spec.rb
|
176
202
|
- spec/rapidash/client_spec.rb
|
177
|
-
- spec/rapidash/clientable_spec.rb
|
178
203
|
- spec/rapidash/http_client_spec.rb
|
179
204
|
- spec/rapidash/integration/test_client_spec.rb
|
180
205
|
- spec/rapidash/oauth_client_spec.rb
|
data/lib/rapidash/clientable.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module Rapidash
|
2
|
-
module Clientable
|
3
|
-
|
4
|
-
def self.included(base)
|
5
|
-
base.extend ClassMethods
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
|
10
|
-
attr_accessor :patch, :url_extension, :raise_error
|
11
|
-
|
12
|
-
def method(method)
|
13
|
-
case method
|
14
|
-
when :http then include HTTPClient
|
15
|
-
when :oauth then include OAuthClient
|
16
|
-
when :test then include TestClient
|
17
|
-
else
|
18
|
-
raise ConfigurationError.new "Invalid API Authentication Method"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def use_patch
|
23
|
-
@patch = true
|
24
|
-
end
|
25
|
-
|
26
|
-
def extension(extension)
|
27
|
-
@url_extension = extension
|
28
|
-
end
|
29
|
-
|
30
|
-
def raise_errors
|
31
|
-
@raise_error = true
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,88 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
class OAuthClientTester
|
4
|
-
include Rapidash::Clientable
|
5
|
-
method :oauth
|
6
|
-
end
|
7
|
-
|
8
|
-
class HTTPClientTester
|
9
|
-
include Rapidash::Clientable
|
10
|
-
method :http
|
11
|
-
end
|
12
|
-
|
13
|
-
class HTTPClientPatchTester < HTTPClientTester
|
14
|
-
use_patch
|
15
|
-
end
|
16
|
-
|
17
|
-
class HTTPClientExtensionTester < HTTPClientTester
|
18
|
-
extension :json
|
19
|
-
end
|
20
|
-
|
21
|
-
class HTTPClientErrorTester < HTTPClientTester
|
22
|
-
raise_errors
|
23
|
-
end
|
24
|
-
|
25
|
-
|
26
|
-
class TestClientTester
|
27
|
-
include Rapidash::Clientable
|
28
|
-
method :test
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
describe Rapidash::Clientable do
|
35
|
-
|
36
|
-
describe "#included" do
|
37
|
-
it "should include the method method" do
|
38
|
-
HTTPClientTester.methods.map { |m| m.to_sym }.should include(:method)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe "#method" do
|
43
|
-
|
44
|
-
it "should include the HTTPClient" do
|
45
|
-
client = HTTPClientTester.new
|
46
|
-
client.class.ancestors.should include(Rapidash::HTTPClient)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should include the OAuthClient" do
|
50
|
-
client = OAuthClientTester.new({:uid => "foo", :secret => "bar", :site => "baz"})
|
51
|
-
client.class.ancestors.should include(Rapidash::OAuthClient)
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should include the OAuthClient" do
|
55
|
-
client = TestClientTester.new
|
56
|
-
client.class.ancestors.should include(Rapidash::TestClient)
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should raise an error on anything else" do
|
60
|
-
expect {
|
61
|
-
class InvalidClientTester
|
62
|
-
include Rapidash::Clientable
|
63
|
-
method :invalid
|
64
|
-
end
|
65
|
-
}.to raise_error(Rapidash::ConfigurationError)
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
describe "#use_patch" do
|
71
|
-
it "should set the patch variable to true" do
|
72
|
-
HTTPClientPatchTester.new.class.instance_variable_get(:@patch).should eql(true)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe "#extension" do
|
77
|
-
it "should set the url_extension variable" do
|
78
|
-
HTTPClientExtensionTester.new.class.instance_variable_get(:@url_extension).should eql(:json)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe "#raise_errors" do
|
83
|
-
it "should set the raise_error variable" do
|
84
|
-
HTTPClientErrorTester.new.class.instance_variable_get(:@raise_error).should eql(true)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|