rapidash 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +89 -50
- data/lib/rapidash/base.rb +27 -10
- data/lib/rapidash/client.rb +1 -0
- data/lib/rapidash/clientable.rb +5 -1
- data/lib/rapidash/http_client.rb +26 -5
- data/lib/rapidash/oauth_client.rb +5 -1
- data/lib/rapidash/response.rb +0 -1
- data/lib/rapidash/version.rb +1 -1
- data/spec/rapidash/base_spec.rb +31 -3
- data/spec/rapidash/clientable_spec.rb +20 -0
- data/spec/rapidash/http_client_spec.rb +30 -0
- data/spec/rapidash/oauth_client_spec.rb +15 -0
- data/spec/spec_helper.rb +0 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7cf2002fd2e632f1c5150d3d3a5d858d58ec09c
|
4
|
+
data.tar.gz: 6de1e547b1243cce13a8440a41ac28113e191843
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b64aa96d960b5a4bb1e2e50bd7c16c262e40fe57548745d1410385c3ce25f8b3374dc2673da012c0d9c5038c21e0e3746eb3fe4405a8c30b1a8b308d2215cd64
|
7
|
+
data.tar.gz: 9e1d5838e548568064e63bfe8ff2c3cc173d308c7a4f5c9c79615cb7fdf29fedeb837c51c352ec0d5813643bba8c4598b9e1e2e6eb467ba9dc9c3b253798c777
|
data/README.md
CHANGED
@@ -22,32 +22,65 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
Resources can be defined as follows:
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
```ruby
|
26
|
+
class Users < Rapidash::Base
|
27
|
+
end
|
28
|
+
```
|
27
29
|
|
28
30
|
The URL of the resource will be inferred from the class name. In this case Users. If you want to override that, you can with the url method.
|
29
31
|
|
30
|
-
|
31
|
-
|
32
|
-
|
32
|
+
```ruby
|
33
|
+
class Users < Rapidash::Base
|
34
|
+
url :members # or url "members" is also supported
|
35
|
+
end
|
36
|
+
```
|
33
37
|
|
34
38
|
Resources can exist inside other resources. For example, on Github, a user has repositories. The following could be how you build the resources:
|
35
39
|
|
36
|
-
|
37
|
-
|
40
|
+
```ruby
|
41
|
+
class Repos < Rapidash::Base
|
42
|
+
end
|
38
43
|
|
39
|
-
|
40
|
-
|
41
|
-
|
44
|
+
class Users < Rapidash::Base
|
45
|
+
resource :repos
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
#### Root elements
|
50
|
+
|
51
|
+
A root element can be set for create and post actions
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class Posts < Rapidash::Base
|
55
|
+
end
|
56
|
+
|
57
|
+
client.posts.create!({:post => {:name => "a post"}})
|
58
|
+
```
|
59
|
+
|
60
|
+
With a root element, the code would look like this:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class Posts < Rapidash::Base
|
64
|
+
root :post
|
65
|
+
end
|
66
|
+
|
67
|
+
client.posts.create!(:name => "a post")
|
68
|
+
```
|
42
69
|
|
43
70
|
### Client
|
44
71
|
|
45
72
|
The main thing a client must do is define a method, `oauth` and `http` are currently supported. You can also define resources which links a resource as defined above to the client.
|
46
73
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
74
|
+
```ruby
|
75
|
+
class Client < Rapidash::Client
|
76
|
+
method :oauth
|
77
|
+
resource :users
|
78
|
+
use_patch # This will use PATCH when updating instead of POST
|
79
|
+
extension :json #Append the extension fo the urls
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
|
51
84
|
|
52
85
|
OAuth provides an initialize method which you can see in the Facebook client example.
|
53
86
|
|
@@ -55,57 +88,63 @@ Currently when using the HTTP method, you will need to define your own initializ
|
|
55
88
|
|
56
89
|
### Making calls
|
57
90
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
91
|
+
```ruby
|
92
|
+
client = Client.new
|
93
|
+
client.site = "http://example.com/"
|
94
|
+
client.users #Returns an instance of Users
|
95
|
+
client.users! #Will make a call to "http://example.com/users.json
|
96
|
+
client.users!(1) #Will make a call to http://example.com/users/1.json
|
97
|
+
client.users!(params => {:page => 1}}) #Will make a call to http://example.com/users.json?page=1
|
98
|
+
client.users.create!({:user => {:name => "Gazler"}}) #POST requst to /users.json
|
99
|
+
client.users(1).update!({:user => {:name => "Gazler"}}) #PUT or PATCH requst to /users.json
|
100
|
+
client.users(1).delete! #DELETE requst to /users.json
|
101
|
+
```
|
64
102
|
|
65
103
|
## Example Clients
|
66
104
|
|
67
105
|
### Facebook
|
68
106
|
|
69
|
-
|
107
|
+
```ruby
|
108
|
+
require 'rapidash'
|
70
109
|
|
71
|
-
|
72
|
-
|
110
|
+
class Me < Rapidash::Base
|
111
|
+
end
|
73
112
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
113
|
+
class Facebook < Rapidash::Client
|
114
|
+
method :oauth
|
115
|
+
resource :me
|
116
|
+
end
|
78
117
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
118
|
+
client = Facebook.new({
|
119
|
+
:site => "https://graph.facebook.com",
|
120
|
+
:uid => "YOUR_ID",
|
121
|
+
:secret => "YOUR_SECRET",
|
122
|
+
:access_token => "YOUR_TOKEN"
|
123
|
+
})
|
124
|
+
p client.me!.first_name #Gary
|
125
|
+
```
|
86
126
|
|
87
127
|
### Github
|
88
128
|
|
89
|
-
|
90
|
-
|
91
|
-
class Repos < Rapidash::Base
|
129
|
+
```ruby
|
130
|
+
require 'rapidash'
|
92
131
|
|
93
|
-
|
94
|
-
resource :repos
|
95
|
-
end
|
132
|
+
class Repos < Rapidash::Base
|
96
133
|
|
97
|
-
|
98
|
-
|
99
|
-
|
134
|
+
class Users < Rapidash::Base
|
135
|
+
resource :repos
|
136
|
+
end
|
100
137
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
138
|
+
class Github < Rapidash::Client
|
139
|
+
method :http
|
140
|
+
resource :users
|
141
|
+
site "https://api.github.com/"
|
142
|
+
end
|
105
143
|
|
106
|
-
|
107
|
-
|
108
|
-
|
144
|
+
client = Github.new
|
145
|
+
p client.users!("Gazler").name #Gary Rennie
|
146
|
+
p client.users("Gazler").repos![0].name #Githug
|
147
|
+
```
|
109
148
|
|
110
149
|
## Contributing
|
111
150
|
|
data/lib/rapidash/base.rb
CHANGED
@@ -3,8 +3,17 @@ module Rapidash
|
|
3
3
|
|
4
4
|
include Urlable
|
5
5
|
include Resourceable
|
6
|
+
|
6
7
|
attr_accessor :url, :options, :client
|
7
8
|
|
9
|
+
class << self
|
10
|
+
attr_accessor :root_element
|
11
|
+
|
12
|
+
def root(name)
|
13
|
+
@root_element = name.to_sym
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
8
17
|
def initialize(*args)
|
9
18
|
@client, @id, options = args
|
10
19
|
|
@@ -20,34 +29,42 @@ module Rapidash
|
|
20
29
|
end
|
21
30
|
|
22
31
|
def create!(params)
|
23
|
-
|
24
|
-
|
32
|
+
options[:method] = :post
|
33
|
+
set_body!(params)
|
25
34
|
call!
|
26
35
|
end
|
27
36
|
|
28
37
|
def update!(params)
|
29
|
-
|
30
|
-
|
38
|
+
options[:method] = client.class.patch ? :patch : :put
|
39
|
+
set_body!(params)
|
31
40
|
call!
|
32
41
|
end
|
33
42
|
|
34
43
|
def delete!
|
35
|
-
|
44
|
+
options[:method] = :delete
|
36
45
|
call!
|
37
46
|
end
|
38
47
|
|
39
48
|
|
40
49
|
def call!
|
41
50
|
self.options ||= {}
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
method =
|
46
|
-
client.send(method, url,
|
51
|
+
options.delete(:previous_url)
|
52
|
+
options[:header] ||= {}
|
53
|
+
options[:header]["content-type"] = "application/json"
|
54
|
+
method = options.delete(:method) || :get
|
55
|
+
client.send(method, url, options)
|
47
56
|
end
|
48
57
|
|
49
58
|
private
|
50
59
|
|
60
|
+
def set_body!(params)
|
61
|
+
if self.class.root_element
|
62
|
+
options[:body] = {self.class.root_element => params}.to_json
|
63
|
+
else
|
64
|
+
options[:body] = params.to_json
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
51
68
|
def base_url
|
52
69
|
if old_url = self.options[:previous_url]
|
53
70
|
return "#{old_url}/"
|
data/lib/rapidash/client.rb
CHANGED
data/lib/rapidash/clientable.rb
CHANGED
@@ -7,7 +7,7 @@ module Rapidash
|
|
7
7
|
|
8
8
|
module ClassMethods
|
9
9
|
|
10
|
-
attr_accessor :patch
|
10
|
+
attr_accessor :patch, :url_extension
|
11
11
|
|
12
12
|
def method(method)
|
13
13
|
case method
|
@@ -22,6 +22,10 @@ module Rapidash
|
|
22
22
|
def use_patch
|
23
23
|
@patch = true
|
24
24
|
end
|
25
|
+
|
26
|
+
def extension(extension)
|
27
|
+
@url_extension = extension
|
28
|
+
end
|
25
29
|
end
|
26
30
|
end
|
27
31
|
end
|
data/lib/rapidash/http_client.rb
CHANGED
@@ -3,20 +3,31 @@ require 'faraday'
|
|
3
3
|
module Rapidash
|
4
4
|
module HTTPClient
|
5
5
|
|
6
|
-
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_accessor :extension, :site
|
7
11
|
attr_writer :connection
|
8
12
|
|
9
|
-
def
|
13
|
+
def initialize
|
14
|
+
end
|
15
|
+
|
16
|
+
def site=(site)
|
17
|
+
@site = site
|
10
18
|
@connection = nil
|
11
|
-
@site = value
|
12
19
|
end
|
13
20
|
|
14
21
|
def connection
|
15
|
-
@connection ||= Faraday.new(site)
|
22
|
+
@connection ||= Faraday.new(site || self.class.site_url)
|
16
23
|
end
|
17
24
|
|
18
25
|
def request(verb, url, options = {})
|
19
|
-
|
26
|
+
if extension
|
27
|
+
url = "#{url}.#{(extension)}"
|
28
|
+
elsif self.class.respond_to?(:url_extension) && self.class.url_extension
|
29
|
+
url = "#{url}.#{(self.class.url_extension)}"
|
30
|
+
end
|
20
31
|
url = connection.build_url(url, options[:params]).to_s
|
21
32
|
response = connection.run_request(verb, url, options[:body], options[:header])
|
22
33
|
|
@@ -29,5 +40,15 @@ module Rapidash
|
|
29
40
|
return Response.new(response)
|
30
41
|
end
|
31
42
|
end
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
module ClassMethods
|
47
|
+
attr_accessor :site_url
|
48
|
+
|
49
|
+
def site(site)
|
50
|
+
@site_url = site
|
51
|
+
end
|
52
|
+
end
|
32
53
|
end
|
33
54
|
end
|
@@ -18,7 +18,11 @@ module Rapidash
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def request(verb, url, options = {})
|
21
|
-
|
21
|
+
if extension
|
22
|
+
url = "#{url}.#{(extension)}"
|
23
|
+
elsif self.class.respond_to?(:url_extension) && self.class.url_extension
|
24
|
+
url = "#{url}.#{(self.class.url_extension)}"
|
25
|
+
end
|
22
26
|
response = oauth_access_token.send(verb.to_sym, "#{site}/#{url}", options)
|
23
27
|
body = JSON.parse(response.body)
|
24
28
|
if body.kind_of?(Hash)
|
data/lib/rapidash/response.rb
CHANGED
data/lib/rapidash/version.rb
CHANGED
data/spec/rapidash/base_spec.rb
CHANGED
@@ -16,6 +16,10 @@ class BaseTesterClient
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
+
class RootTester < Rapidash::Base
|
20
|
+
root :post
|
21
|
+
end
|
22
|
+
|
19
23
|
|
20
24
|
describe Rapidash::Base do
|
21
25
|
|
@@ -38,11 +42,15 @@ describe Rapidash::Base do
|
|
38
42
|
let(:headers) { {"content-type" => "application/json"} }
|
39
43
|
let(:subject) { BaseTester.new(client) }
|
40
44
|
|
45
|
+
let(:no_root) {
|
46
|
+
{
|
47
|
+
:title => "A test post"
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
41
51
|
let(:post) {
|
42
52
|
{
|
43
|
-
:post =>
|
44
|
-
:title => "A test post"
|
45
|
-
}
|
53
|
+
:post => no_root
|
46
54
|
}
|
47
55
|
}
|
48
56
|
|
@@ -55,6 +63,16 @@ describe Rapidash::Base do
|
|
55
63
|
:body => post.to_json
|
56
64
|
})
|
57
65
|
end
|
66
|
+
|
67
|
+
it "should use the root element if one is defined" do
|
68
|
+
subject = RootTester.new
|
69
|
+
subject.should_receive(:call!)
|
70
|
+
subject.create!(no_root)
|
71
|
+
subject.instance_variable_get(:@options).should eql({
|
72
|
+
:method => :post,
|
73
|
+
:body => post.to_json
|
74
|
+
})
|
75
|
+
end
|
58
76
|
end
|
59
77
|
|
60
78
|
describe ".update!" do
|
@@ -76,6 +94,16 @@ describe Rapidash::Base do
|
|
76
94
|
:body => post.to_json
|
77
95
|
})
|
78
96
|
end
|
97
|
+
|
98
|
+
it "should use the root element if one is defined" do
|
99
|
+
subject = RootTester.new(client)
|
100
|
+
subject.should_receive(:call!)
|
101
|
+
subject.update!(no_root)
|
102
|
+
subject.instance_variable_get(:@options).should eql({
|
103
|
+
:method => :patch,
|
104
|
+
:body => post.to_json
|
105
|
+
})
|
106
|
+
end
|
79
107
|
end
|
80
108
|
|
81
109
|
describe ".delete!" do
|
@@ -10,6 +10,14 @@ class HTTPClientTester
|
|
10
10
|
method :http
|
11
11
|
end
|
12
12
|
|
13
|
+
class HTTPClientPatchTester < HTTPClientTester
|
14
|
+
use_patch
|
15
|
+
end
|
16
|
+
|
17
|
+
class HTTPClientExtensionTester < HTTPClientTester
|
18
|
+
extension :json
|
19
|
+
end
|
20
|
+
|
13
21
|
class TestClientTester
|
14
22
|
include Rapidash::Clientable
|
15
23
|
method :test
|
@@ -54,4 +62,16 @@ describe Rapidash::Clientable do
|
|
54
62
|
|
55
63
|
end
|
56
64
|
|
65
|
+
describe "#use_patch" do
|
66
|
+
it "should set the patch variable to true" do
|
67
|
+
HTTPClientPatchTester.new.class.instance_variable_get(:@patch).should eql(true)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "#extension" do
|
72
|
+
it "should set the url_extension variable" do
|
73
|
+
HTTPClientExtensionTester.new.class.instance_variable_get(:@url_extension).should eql(:json)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
57
77
|
end
|
@@ -4,6 +4,17 @@ class HTTPTester
|
|
4
4
|
include Rapidash::HTTPClient
|
5
5
|
end
|
6
6
|
|
7
|
+
class HTTPSiteTester < HTTPTester
|
8
|
+
site "http://mysite.com/"
|
9
|
+
end
|
10
|
+
|
11
|
+
class HTTPExtensionTester < HTTPTester
|
12
|
+
site "http://mysite.com/"
|
13
|
+
def self.url_extension
|
14
|
+
:json
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
7
18
|
describe Rapidash::HTTPClient do
|
8
19
|
|
9
20
|
let!(:subject) { HTTPTester.new }
|
@@ -28,6 +39,18 @@ describe Rapidash::HTTPClient do
|
|
28
39
|
it "should create a Faraday object" do
|
29
40
|
subject.connection.class.should eql(Faraday::Connection)
|
30
41
|
end
|
42
|
+
|
43
|
+
it "should use the site variable if set" do
|
44
|
+
Faraday.should_receive(:new).with("http://example.com/")
|
45
|
+
subject.site = "http://example.com/"
|
46
|
+
subject.connection
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should use the class URL if one is defined" do
|
50
|
+
subject = HTTPSiteTester.new
|
51
|
+
Faraday.should_receive(:new).with("http://mysite.com/")
|
52
|
+
subject.connection
|
53
|
+
end
|
31
54
|
end
|
32
55
|
|
33
56
|
describe ".request" do
|
@@ -46,6 +69,13 @@ describe Rapidash::HTTPClient do
|
|
46
69
|
subject.request(:get, "foo")
|
47
70
|
end
|
48
71
|
|
72
|
+
it "should use the class extension if one is set" do
|
73
|
+
subject = HTTPExtensionTester.new
|
74
|
+
subject.connection.should_receive(:run_request).with(:get, "http://mysite.com/foo.json", nil, nil).and_return(valid_response)
|
75
|
+
subject.request(:get, "foo")
|
76
|
+
end
|
77
|
+
|
78
|
+
|
49
79
|
it "should return a response object" do
|
50
80
|
subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return(valid_response)
|
51
81
|
response = subject.request(:get, "foo")
|
@@ -4,6 +4,12 @@ class OAuthTester
|
|
4
4
|
include Rapidash::OAuthClient
|
5
5
|
end
|
6
6
|
|
7
|
+
class OAuthExtensionTester < OAuthTester
|
8
|
+
def self.url_extension
|
9
|
+
:json
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
7
13
|
describe Rapidash::OAuthClient do
|
8
14
|
|
9
15
|
let(:options) do
|
@@ -67,6 +73,15 @@ describe Rapidash::OAuthClient do
|
|
67
73
|
subject.request(:get, "me")
|
68
74
|
end
|
69
75
|
|
76
|
+
it "should use the class extension if one is set" do
|
77
|
+
subject = OAuthExtensionTester.new(options)
|
78
|
+
subject.stub(:oauth_access_token).and_return(request)
|
79
|
+
request.stub(:get).and_return(response)
|
80
|
+
request.should_receive(:get).with("http://example.com/me.json", anything)
|
81
|
+
subject.request(:get, "me")
|
82
|
+
end
|
83
|
+
|
84
|
+
|
70
85
|
it "should return a Hashie::Mash" do
|
71
86
|
subject.request(:get, "me").class.should eql(Hashie::Mash)
|
72
87
|
end
|
data/spec/spec_helper.rb
CHANGED