rapidash 0.0.1 → 0.0.2
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.
- data/.gitignore +1 -0
- data/.travis.yml +6 -0
- data/README.md +30 -30
- data/Rakefile +5 -0
- data/lib/rapidash.rb +2 -0
- data/lib/rapidash/base.rb +23 -2
- data/lib/rapidash/client.rb +12 -3
- data/lib/rapidash/clientable.rb +4 -20
- data/lib/rapidash/http_client.rb +2 -9
- data/lib/rapidash/oauth_client.rb +0 -4
- data/lib/rapidash/resourceable.rb +46 -0
- data/lib/rapidash/response.rb +1 -0
- data/lib/rapidash/test_client.rb +14 -0
- data/lib/rapidash/urlable.rb +3 -10
- data/lib/rapidash/version.rb +1 -1
- data/rapidash.gemspec +2 -1
- data/spec/rapidash/base_spec.rb +22 -9
- data/spec/rapidash/client_spec.rb +22 -0
- data/spec/rapidash/clientable_spec.rb +12 -54
- data/spec/rapidash/http_client_spec.rb +0 -15
- data/spec/rapidash/integration/test_client_spec.rb +48 -0
- data/spec/rapidash/oauth_client_spec.rb +3 -10
- data/spec/rapidash/resourceable_spec.rb +95 -0
- data/spec/rapidash/test_client_spec.rb +26 -0
- data/spec/rapidash/urlable_spec.rb +18 -7
- data/spec/spec_helper.rb +1 -0
- metadata +56 -11
- checksums.yaml +0 -7
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Rapidash
|
1
|
+
# Rapidash [](https://travis-ci.org/Gazler/rapidash)
|
2
2
|
|
3
3
|
Rapidash is an opinionated core for you to build a client for your API on. The goal is to define a standard way that developers can quickly write a client for the consumption of their RESTful API.
|
4
4
|
|
@@ -23,7 +23,21 @@ Or install it yourself as:
|
|
23
23
|
Resources can be defined as follows:
|
24
24
|
|
25
25
|
class Users < Rapidash::Base
|
26
|
-
|
26
|
+
end
|
27
|
+
|
28
|
+
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
|
+
|
30
|
+
class Users < Rapidash::Base
|
31
|
+
url :members # or url "members" is also supported
|
32
|
+
end
|
33
|
+
|
34
|
+
Resources can exist inside other resources. For example, on Github, a user has repositories. The following could be how you build the resources:
|
35
|
+
|
36
|
+
class Repos < Rapidash::Base
|
37
|
+
end
|
38
|
+
|
39
|
+
class Users < Rapidash::Base
|
40
|
+
resource :repos
|
27
41
|
end
|
28
42
|
|
29
43
|
### Client
|
@@ -55,7 +69,6 @@ Currently when using the HTTP method, you will need to define your own initializ
|
|
55
69
|
require 'rapidash'
|
56
70
|
|
57
71
|
class Me < Rapidash::Base
|
58
|
-
url "me"
|
59
72
|
end
|
60
73
|
|
61
74
|
class Facebook < Rapidash::Client
|
@@ -63,7 +76,7 @@ Currently when using the HTTP method, you will need to define your own initializ
|
|
63
76
|
resource :me
|
64
77
|
end
|
65
78
|
|
66
|
-
client = Facebook
|
79
|
+
client = Facebook.new({
|
67
80
|
:site => "https://graph.facebook.com",
|
68
81
|
:uid => "YOUR_ID",
|
69
82
|
:secret => "YOUR_SECRET",
|
@@ -73,39 +86,26 @@ Currently when using the HTTP method, you will need to define your own initializ
|
|
73
86
|
|
74
87
|
### Github
|
75
88
|
|
76
|
-
|
77
|
-
|
78
|
-
class Users < Rapidash::Base
|
79
|
-
url :users
|
80
|
-
|
81
|
-
def user(name)
|
82
|
-
self.url += "/#{name}"
|
83
|
-
self
|
84
|
-
end
|
89
|
+
require 'rapidash'
|
85
90
|
|
86
|
-
|
87
|
-
self.url += "/repos"
|
88
|
-
call!
|
89
|
-
end
|
91
|
+
class Repos < Rapidash::Base
|
90
92
|
|
91
|
-
|
92
|
-
|
93
|
-
call!
|
93
|
+
class Users < Rapidash::Base
|
94
|
+
resource :repos
|
94
95
|
end
|
95
|
-
end
|
96
96
|
|
97
|
-
|
98
|
-
|
99
|
-
|
97
|
+
class Github < Rapidash::Client
|
98
|
+
method :http
|
99
|
+
resource :users
|
100
100
|
|
101
|
-
|
102
|
-
|
101
|
+
def initialize
|
102
|
+
@site = "https://api.github.com/"
|
103
|
+
end
|
103
104
|
end
|
104
|
-
end
|
105
105
|
|
106
|
-
|
107
|
-
|
108
|
-
|
106
|
+
client = Github.new
|
107
|
+
p client.users!("Gazler").name #Gary Rennie
|
108
|
+
p client.users("Gazler").repos![0].name #Githug
|
109
109
|
|
110
110
|
## Contributing
|
111
111
|
|
data/Rakefile
CHANGED
data/lib/rapidash.rb
CHANGED
@@ -5,6 +5,7 @@ require "rapidash/errors"
|
|
5
5
|
require "rapidash/response"
|
6
6
|
|
7
7
|
require "rapidash/clientable"
|
8
|
+
require "rapidash/resourceable"
|
8
9
|
require "rapidash/client"
|
9
10
|
|
10
11
|
require "rapidash/urlable"
|
@@ -12,6 +13,7 @@ require "rapidash/base"
|
|
12
13
|
|
13
14
|
require "rapidash/http_client"
|
14
15
|
require "rapidash/oauth_client"
|
16
|
+
require "rapidash/test_client"
|
15
17
|
|
16
18
|
module Rapidash
|
17
19
|
end
|
data/lib/rapidash/base.rb
CHANGED
@@ -2,19 +2,40 @@ module Rapidash
|
|
2
2
|
class Base
|
3
3
|
|
4
4
|
include Urlable
|
5
|
+
include Resourceable
|
5
6
|
attr_accessor :url, :options, :client
|
6
7
|
|
7
|
-
def initialize
|
8
|
-
|
8
|
+
def initialize(*args)
|
9
|
+
@client, @id, options = args
|
10
|
+
|
11
|
+
if @id.is_a?(Hash)
|
12
|
+
options = @id
|
13
|
+
@id = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
@options ||= {}
|
17
|
+
@options.merge!(options || {})
|
18
|
+
@url = "#{base_url}#{self.class.to_s.split("::")[-1].downcase}"
|
19
|
+
@url += "/#{@id}" if @id
|
9
20
|
end
|
10
21
|
|
11
22
|
|
12
23
|
def call!
|
13
24
|
self.options ||= {}
|
25
|
+
self.options.delete(:previous_url)
|
14
26
|
self.options[:header] ||= {}
|
15
27
|
self.options[:header]["content-type"] = "application/json"
|
16
28
|
method = self.options.delete(:method) || :get
|
17
29
|
client.send(method, url, self.options)
|
18
30
|
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def base_url
|
35
|
+
if old_url = self.options[:previous_url]
|
36
|
+
return "#{old_url}/"
|
37
|
+
end
|
38
|
+
""
|
39
|
+
end
|
19
40
|
end
|
20
41
|
end
|
data/lib/rapidash/client.rb
CHANGED
@@ -1,9 +1,18 @@
|
|
1
1
|
module Rapidash
|
2
2
|
class Client
|
3
3
|
include Clientable
|
4
|
+
include Resourceable
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def initialize
|
7
|
+
raise ConfigurationError.new "Missing Method, define using `method` on your client"
|
8
|
+
end
|
9
|
+
|
10
|
+
def get(url, options = {})
|
11
|
+
request(:get, url, options)
|
12
|
+
end
|
13
|
+
|
14
|
+
def post(url, options = {})
|
15
|
+
request(:post, url, options)
|
16
|
+
end
|
8
17
|
end
|
9
18
|
end
|
data/lib/rapidash/clientable.rb
CHANGED
@@ -8,30 +8,14 @@ module Rapidash
|
|
8
8
|
module ClassMethods
|
9
9
|
|
10
10
|
def method(method)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
case method
|
12
|
+
when :http then include HTTPClient
|
13
|
+
when :oauth then include OAuthClient
|
14
|
+
when :test then include TestClient
|
15
15
|
else
|
16
16
|
raise ConfigurationError.new "Invalid API Authentication Method"
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
20
|
-
def resource(name)
|
21
|
-
mod = self.to_s.split("::")[0...-1]
|
22
|
-
if mod.empty?
|
23
|
-
mod = Kernel
|
24
|
-
else
|
25
|
-
mod = Kernel.const_get(mod.join("::"))
|
26
|
-
end
|
27
|
-
klass = mod.const_get(name.capitalize)
|
28
|
-
define_method(name) do |*args|
|
29
|
-
klass.new(self, *args)
|
30
|
-
end
|
31
|
-
define_method("#{name}!".to_sym) do |*args|
|
32
|
-
klass.new(self, *args).call!
|
33
|
-
end
|
34
|
-
end
|
35
19
|
end
|
36
20
|
end
|
37
21
|
end
|
data/lib/rapidash/http_client.rb
CHANGED
@@ -15,19 +15,12 @@ module Rapidash
|
|
15
15
|
@connection ||= Faraday.new(site)
|
16
16
|
end
|
17
17
|
|
18
|
-
def get(url, options = {})
|
19
|
-
request(:get, url, options)
|
20
|
-
end
|
21
|
-
|
22
|
-
def post(url, options = {})
|
23
|
-
request(:post, url, options)
|
24
|
-
end
|
25
|
-
|
26
18
|
def request(verb, url, options = {})
|
27
19
|
url = connection.build_url(url, options[:params]).to_s
|
28
20
|
response = connection.run_request(verb, url, options[:body], options[:header])
|
29
21
|
|
30
|
-
|
22
|
+
# "foo"[0] does not work in 1.8.7, "foo"[0,1] is required
|
23
|
+
case response.status.to_s[0,1]
|
31
24
|
#Handle redirects
|
32
25
|
when "3"
|
33
26
|
request(verb, response.headers["location"], options)
|
@@ -17,10 +17,6 @@ module Rapidash
|
|
17
17
|
self.access_token = options[:access_token] if options[:access_token]
|
18
18
|
end
|
19
19
|
|
20
|
-
def get(url, params = {})
|
21
|
-
request(:get, url, :params => params)
|
22
|
-
end
|
23
|
-
|
24
20
|
def request(verb, url, options = {})
|
25
21
|
response = oauth_access_token.send(verb.to_sym, "#{site}/#{url}", options)
|
26
22
|
body = JSON.parse(response.body)
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Rapidash
|
2
|
+
module Resourceable
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def resource(name)
|
11
|
+
mod = self.to_s.split("::")[0...-1]
|
12
|
+
if mod.empty?
|
13
|
+
mod = Kernel
|
14
|
+
else
|
15
|
+
mod = Kernel.const_get(mod.join("::"))
|
16
|
+
end
|
17
|
+
klass = mod.const_get(name.to_s.capitalize)
|
18
|
+
|
19
|
+
def get_client(me)
|
20
|
+
client = me
|
21
|
+
if me.respond_to?(:client)
|
22
|
+
client = me.client
|
23
|
+
end
|
24
|
+
client
|
25
|
+
end
|
26
|
+
|
27
|
+
mod = self
|
28
|
+
|
29
|
+
define_method(name) do |*args|
|
30
|
+
if self.respond_to?(:url)
|
31
|
+
options = {:previous_url => self.url}
|
32
|
+
if args[args.length].is_a?(Hash)
|
33
|
+
args[args.length].merge!(options)
|
34
|
+
else
|
35
|
+
args << options
|
36
|
+
end
|
37
|
+
end
|
38
|
+
klass.new(mod.get_client(self), *args)
|
39
|
+
end
|
40
|
+
define_method("#{name}!".to_sym) do |*args|
|
41
|
+
self.send(name, *args).call!
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/rapidash/response.rb
CHANGED
data/lib/rapidash/urlable.rb
CHANGED
@@ -9,16 +9,9 @@ module Rapidash
|
|
9
9
|
def url(url)
|
10
10
|
self.class_eval do
|
11
11
|
define_method(:initialize) do |*args|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
id = nil
|
16
|
-
end
|
17
|
-
@options ||= {}
|
18
|
-
options ||= {}
|
19
|
-
@options.merge!(options)
|
20
|
-
@url = url.to_s
|
21
|
-
@url += "/#{id}" if id
|
12
|
+
super(*args)
|
13
|
+
@url = "#{base_url}#{url.to_s}"
|
14
|
+
@url += "/#{@id}" if @id
|
22
15
|
end
|
23
16
|
end
|
24
17
|
end
|
data/lib/rapidash/version.rb
CHANGED
data/rapidash.gemspec
CHANGED
@@ -18,10 +18,11 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_development_dependency "bundler", "~> 1.
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.0"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec", "~> 2.8"
|
24
24
|
|
25
|
+
spec.add_dependency 'json'
|
25
26
|
spec.add_dependency 'faraday', '~> 0.8'
|
26
27
|
spec.add_dependency "oauth2", "~>0.6"
|
27
28
|
spec.add_dependency "hashie", "~>1.2"
|
data/spec/rapidash/base_spec.rb
CHANGED
@@ -4,7 +4,10 @@ class BaseTester < Rapidash::Base
|
|
4
4
|
url "tester"
|
5
5
|
end
|
6
6
|
|
7
|
-
class
|
7
|
+
class Base < Rapidash::Base
|
8
|
+
end
|
9
|
+
|
10
|
+
class Rapidash::Resource < Rapidash::Base
|
8
11
|
end
|
9
12
|
|
10
13
|
|
@@ -12,18 +15,17 @@ describe Rapidash::Base do
|
|
12
15
|
|
13
16
|
describe ".initialize" do
|
14
17
|
|
15
|
-
it "should
|
16
|
-
|
17
|
-
InvalidApiTester.new
|
18
|
-
}.to raise_error(Rapidash::ConfigurationError)
|
18
|
+
it "should assume a default based on the class name" do
|
19
|
+
Base.new.instance_variable_get(:@url).should eql("base")
|
19
20
|
end
|
20
21
|
|
21
|
-
it "should
|
22
|
-
|
23
|
-
BaseTester.new.should be_valid
|
24
|
-
}.to_not raise_error(Rapidash::ConfigurationError)
|
22
|
+
it "should ignore any modules when infering the URL" do
|
23
|
+
Rapidash::Resource.new.instance_variable_get(:@url).should eql("resource")
|
25
24
|
end
|
26
25
|
|
26
|
+
it "should override the URL if set" do
|
27
|
+
BaseTester.new.instance_variable_get(:@url).should eql("tester")
|
28
|
+
end
|
27
29
|
end
|
28
30
|
|
29
31
|
let(:client) { mock }
|
@@ -46,4 +48,15 @@ describe Rapidash::Base do
|
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
51
|
+
describe ".base_url" do
|
52
|
+
it "should return an empty string if no previous url is set" do
|
53
|
+
subject.send(:base_url).should eql("")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return the previous url if set" do
|
57
|
+
subject.options = {:previous_url => "users/Gazler"}
|
58
|
+
subject.send(:base_url).should eql("users/Gazler/")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
49
62
|
end
|
@@ -1,9 +1,31 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
class Client < Rapidash::Client
|
4
|
+
method :test
|
5
|
+
end
|
6
|
+
|
3
7
|
describe Rapidash::Client do
|
8
|
+
|
9
|
+
let!(:subject) { Client.new }
|
10
|
+
|
4
11
|
it "should raise an error when instantiated" do
|
5
12
|
expect {
|
6
13
|
Rapidash::Client.new
|
7
14
|
}.to raise_error(Rapidash::ConfigurationError)
|
8
15
|
end
|
16
|
+
|
17
|
+
describe ".get" do
|
18
|
+
it "should call request" do
|
19
|
+
subject.should_receive(:request).with(:get, "foo", {})
|
20
|
+
subject.get("foo")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe ".post" do
|
25
|
+
it "should call request" do
|
26
|
+
subject.should_receive(:request).with(:post, "foo", {})
|
27
|
+
subject.post("foo")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
9
31
|
end
|
@@ -1,25 +1,5 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
class Rapidash::Users
|
4
|
-
def initialize(*args)
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
class Users
|
9
|
-
def initialize(*args)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class Rapidash::ClientTester
|
14
|
-
include Rapidash::Clientable
|
15
|
-
resource :users
|
16
|
-
end
|
17
|
-
|
18
|
-
class ClientTester
|
19
|
-
include Rapidash::Clientable
|
20
|
-
resource :users
|
21
|
-
end
|
22
|
-
|
23
3
|
class OAuthClientTester
|
24
4
|
include Rapidash::Clientable
|
25
5
|
method :oauth
|
@@ -30,27 +10,19 @@ class HTTPClientTester
|
|
30
10
|
method :http
|
31
11
|
end
|
32
12
|
|
13
|
+
class TestClientTester
|
14
|
+
include Rapidash::Clientable
|
15
|
+
method :test
|
16
|
+
end
|
17
|
+
|
18
|
+
|
33
19
|
|
34
20
|
|
35
21
|
describe Rapidash::Clientable do
|
36
22
|
|
37
23
|
describe "#included" do
|
38
|
-
it "should include the resource method" do
|
39
|
-
Rapidash::ClientTester.methods.should include(:resource)
|
40
|
-
end
|
41
|
-
|
42
24
|
it "should include the method method" do
|
43
|
-
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe "#resource" do
|
48
|
-
it "should add a method with the name of the argument" do
|
49
|
-
Rapidash::ClientTester.new.methods.should include(:users)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should add a bang method with the name of the argument" do
|
53
|
-
Rapidash::ClientTester.new.methods.should include(:users!)
|
25
|
+
HTTPClientTester.methods.map { |m| m.to_sym }.should include(:method)
|
54
26
|
end
|
55
27
|
end
|
56
28
|
|
@@ -66,6 +38,11 @@ describe Rapidash::Clientable do
|
|
66
38
|
client.class.ancestors.should include(Rapidash::OAuthClient)
|
67
39
|
end
|
68
40
|
|
41
|
+
it "should include the OAuthClient" do
|
42
|
+
client = TestClientTester.new
|
43
|
+
client.class.ancestors.should include(Rapidash::TestClient)
|
44
|
+
end
|
45
|
+
|
69
46
|
it "should raise an error on anything else" do
|
70
47
|
expect {
|
71
48
|
class InvalidClientTester
|
@@ -77,23 +54,4 @@ describe Rapidash::Clientable do
|
|
77
54
|
|
78
55
|
end
|
79
56
|
|
80
|
-
describe ".users" do
|
81
|
-
it "should return an instance of the resource" do
|
82
|
-
Rapidash::ClientTester.new.users.class.should eql(Rapidash::Users)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should not use a namespace if not in a module" do
|
86
|
-
ClientTester.new.users.class.should eql(Users)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe ".tickets!" do
|
91
|
-
it "should return an instance of the resource and call it" do
|
92
|
-
users = mock
|
93
|
-
Rapidash::Users.should_receive(:new).and_return(users)
|
94
|
-
users.should_receive(:call!)
|
95
|
-
Rapidash::ClientTester.new.users!
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
57
|
end
|
@@ -30,21 +30,6 @@ describe Rapidash::HTTPClient do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
describe ".get" do
|
34
|
-
it "should call request" do
|
35
|
-
subject.should_receive(:request).with(:get, "foo", {})
|
36
|
-
subject.get("foo")
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe ".get" do
|
41
|
-
it "should call request" do
|
42
|
-
subject.should_receive(:request).with(:post, "foo", {})
|
43
|
-
subject.post("foo")
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
|
48
33
|
describe ".request" do
|
49
34
|
|
50
35
|
let!(:valid_response) { OpenStruct.new(:status => "200")}
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Integration
|
4
|
+
|
5
|
+
class Repos < Rapidash::Base
|
6
|
+
end
|
7
|
+
|
8
|
+
class Users < Rapidash::Base
|
9
|
+
url "members"
|
10
|
+
resource :repos
|
11
|
+
end
|
12
|
+
|
13
|
+
class Client < Rapidash::Client
|
14
|
+
method :test
|
15
|
+
resource :users
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
gazler = OpenStruct.new({
|
21
|
+
:headers => { "content-type" => "application/json" },
|
22
|
+
:body => { :name => "Gary Rennie" }.to_json
|
23
|
+
})
|
24
|
+
|
25
|
+
repos = OpenStruct.new({
|
26
|
+
:headers => { "content-type" => "application/json" },
|
27
|
+
:body => [ { :name => "Githug" } ].to_json
|
28
|
+
})
|
29
|
+
|
30
|
+
responses = {
|
31
|
+
:get => {
|
32
|
+
"members/Gazler" => gazler,
|
33
|
+
"members/Gazler/repos" => repos,
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
describe "An actual Rapidash Client" do
|
38
|
+
|
39
|
+
let!(:client) { Integration::Client.new(:responses => responses) }
|
40
|
+
|
41
|
+
it "should get the user from the API" do
|
42
|
+
client.users!("Gazler").name.should eql("Gary Rennie")
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should get the repos from A user" do
|
46
|
+
client.users("Gazler").repos![0].name.should eql("Githug")
|
47
|
+
end
|
48
|
+
end
|
@@ -7,12 +7,12 @@ end
|
|
7
7
|
describe Rapidash::OAuthClient do
|
8
8
|
|
9
9
|
let(:options) do
|
10
|
-
|
10
|
+
{
|
11
11
|
:uid => "foo",
|
12
12
|
:secret => "bar",
|
13
13
|
:access_token => "baz",
|
14
14
|
:site => "http://example.com"
|
15
|
-
}
|
15
|
+
}
|
16
16
|
end
|
17
17
|
|
18
18
|
let(:subject) { OAuthTester.new(options) }
|
@@ -25,7 +25,7 @@ describe Rapidash::OAuthClient do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
describe ".access_token_from_code" do
|
28
|
-
it "should call
|
28
|
+
it "should call localhost for the access token" do
|
29
29
|
auth_code = mock
|
30
30
|
client = mock
|
31
31
|
subject.stub(:client).and_return(client)
|
@@ -47,13 +47,6 @@ describe Rapidash::OAuthClient do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
describe ".get" do
|
51
|
-
it "should call request with the parameters" do
|
52
|
-
subject.should_receive(:request).with(:get, "me", :params => {:page => 1})
|
53
|
-
subject.get("me", {:page => 1})
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
50
|
describe ".request" do
|
58
51
|
|
59
52
|
let(:request) { mock }
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
class Rapidash::Repos
|
4
|
+
attr_accessor :client, :args
|
5
|
+
def initialize(client, *args)
|
6
|
+
@client = client
|
7
|
+
@args = args
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Rapidash::Users
|
12
|
+
include Rapidash::Resourceable
|
13
|
+
attr_accessor :client, :url
|
14
|
+
resource :repos
|
15
|
+
def initialize(client, *args)
|
16
|
+
@client = client
|
17
|
+
self
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Users
|
22
|
+
def initialize(*args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Rapidash::ClientTester
|
27
|
+
include Rapidash::Resourceable
|
28
|
+
resource :users
|
29
|
+
end
|
30
|
+
|
31
|
+
class ClientTester
|
32
|
+
include Rapidash::Resourceable
|
33
|
+
resource :users
|
34
|
+
end
|
35
|
+
|
36
|
+
describe Rapidash::Resourceable do
|
37
|
+
|
38
|
+
describe "#included" do
|
39
|
+
it "should include the resource method" do
|
40
|
+
Rapidash::ClientTester.methods.map { |m| m.to_sym }.should include(:resource)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#resource" do
|
46
|
+
it "should add a method with the name of the argument" do
|
47
|
+
Rapidash::ClientTester.new.methods.map { |m| m.to_sym }.should include(:users)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should add a bang method with the name of the argument" do
|
51
|
+
Rapidash::ClientTester.new.methods.map { |m| m.to_sym }.should include(:users!)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe ".users" do
|
56
|
+
it "should return an instance of the resource" do
|
57
|
+
Rapidash::ClientTester.new.users.class.should eql(Rapidash::Users)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should not use a namespace if not in a module" do
|
61
|
+
ClientTester.new.users.class.should eql(Users)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe ".tickets!" do
|
66
|
+
it "should return an instance of the resource and call it" do
|
67
|
+
users = mock
|
68
|
+
Rapidash::Users.should_receive(:new).and_return(users)
|
69
|
+
users.should_receive(:call!)
|
70
|
+
Rapidash::ClientTester.new.users!
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "chaining resources" do
|
75
|
+
it "should allow resources to be nested" do
|
76
|
+
client = mock
|
77
|
+
users = Rapidash::Users.new(client)
|
78
|
+
users.methods.map { |m| m.to_sym }.should include(:repos)
|
79
|
+
users.methods.map { |m| m.to_sym }.should include(:repos!)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should maintain the client across resources " do
|
83
|
+
client = mock
|
84
|
+
users = Rapidash::Users.new(client)
|
85
|
+
users.repos.instance_variable_get(:@client).should eql(client)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should maintain the URL when chaining" do
|
89
|
+
client = mock
|
90
|
+
users = Rapidash::Users.new(client)
|
91
|
+
users.repos.instance_variable_get(:@args)[0].keys.should include(:previous_url)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
class TestClientTester
|
4
|
+
include Rapidash::TestClient
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Rapidash::HTTPClient do
|
8
|
+
|
9
|
+
let!(:responses) {
|
10
|
+
{
|
11
|
+
:get => {
|
12
|
+
"foo" => "response"
|
13
|
+
}
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
let!(:subject) { TestClientTester.new(:responses => responses) }
|
18
|
+
|
19
|
+
describe ".request" do
|
20
|
+
it "should do something" do
|
21
|
+
Rapidash::Response.should_receive(:new).with("response")
|
22
|
+
subject.request(:get, "foo")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -1,13 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class ApiTester
|
4
|
-
attr_accessor :url, :options, :client
|
5
|
-
include Rapidash::Urlable
|
3
|
+
class ApiTester < Rapidash::Base
|
6
4
|
url :foo
|
7
5
|
end
|
8
6
|
|
9
|
-
class
|
10
|
-
|
7
|
+
class BaseUrlTester < Rapidash::Base
|
8
|
+
url :foo
|
9
|
+
|
10
|
+
private
|
11
|
+
def base_url
|
12
|
+
"BASE_URL/"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ApiTesterNoUrl < Rapidash::Base
|
11
17
|
end
|
12
18
|
|
13
19
|
describe Rapidash::Urlable do
|
@@ -16,13 +22,13 @@ describe Rapidash::Urlable do
|
|
16
22
|
|
17
23
|
describe "#included" do
|
18
24
|
it "should add the url method" do
|
19
|
-
ApiTester.methods.should include(:url)
|
25
|
+
ApiTester.methods.map { |m| m.to_sym}.should include(:url)
|
20
26
|
end
|
21
27
|
end
|
22
28
|
|
23
29
|
describe "#url" do
|
24
30
|
it "should override the initialize to set a url" do
|
25
|
-
ApiTesterNoUrl.new.instance_variable_get(:@url).should eql(
|
31
|
+
ApiTesterNoUrl.new.instance_variable_get(:@url).should eql("apitesternourl")
|
26
32
|
ApiTester.new.instance_variable_get(:@url).should eql("foo")
|
27
33
|
end
|
28
34
|
|
@@ -37,6 +43,11 @@ describe Rapidash::Urlable do
|
|
37
43
|
api.instance_variable_get(:@options).should eql({:option1 => "foo"})
|
38
44
|
api.instance_variable_get(:@url).should eql("foo/1")
|
39
45
|
end
|
46
|
+
|
47
|
+
it "should call base_url on when constructing the url" do
|
48
|
+
api = BaseUrlTester.new(client, 1)
|
49
|
+
api.instance_variable_get(:@url).should eql("BASE_URL/foo/1")
|
50
|
+
end
|
40
51
|
end
|
41
52
|
|
42
53
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,46 +1,52 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rapidash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Gary 'Gazler' Rennie
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: bundler
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
19
|
- - ~>
|
18
20
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
21
|
+
version: '1.0'
|
20
22
|
type: :development
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
27
|
- - ~>
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
29
|
+
version: '1.0'
|
27
30
|
- !ruby/object:Gem::Dependency
|
28
31
|
name: rake
|
29
32
|
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
30
34
|
requirements:
|
31
|
-
- - '>='
|
35
|
+
- - ! '>='
|
32
36
|
- !ruby/object:Gem::Version
|
33
37
|
version: '0'
|
34
38
|
type: :development
|
35
39
|
prerelease: false
|
36
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
37
42
|
requirements:
|
38
|
-
- - '>='
|
43
|
+
- - ! '>='
|
39
44
|
- !ruby/object:Gem::Version
|
40
45
|
version: '0'
|
41
46
|
- !ruby/object:Gem::Dependency
|
42
47
|
name: rspec
|
43
48
|
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
44
50
|
requirements:
|
45
51
|
- - ~>
|
46
52
|
- !ruby/object:Gem::Version
|
@@ -48,13 +54,31 @@ dependencies:
|
|
48
54
|
type: :development
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
51
58
|
requirements:
|
52
59
|
- - ~>
|
53
60
|
- !ruby/object:Gem::Version
|
54
61
|
version: '2.8'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: json
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
55
78
|
- !ruby/object:Gem::Dependency
|
56
79
|
name: faraday
|
57
80
|
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
58
82
|
requirements:
|
59
83
|
- - ~>
|
60
84
|
- !ruby/object:Gem::Version
|
@@ -62,6 +86,7 @@ dependencies:
|
|
62
86
|
type: :runtime
|
63
87
|
prerelease: false
|
64
88
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
65
90
|
requirements:
|
66
91
|
- - ~>
|
67
92
|
- !ruby/object:Gem::Version
|
@@ -69,6 +94,7 @@ dependencies:
|
|
69
94
|
- !ruby/object:Gem::Dependency
|
70
95
|
name: oauth2
|
71
96
|
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
72
98
|
requirements:
|
73
99
|
- - ~>
|
74
100
|
- !ruby/object:Gem::Version
|
@@ -76,6 +102,7 @@ dependencies:
|
|
76
102
|
type: :runtime
|
77
103
|
prerelease: false
|
78
104
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
79
106
|
requirements:
|
80
107
|
- - ~>
|
81
108
|
- !ruby/object:Gem::Version
|
@@ -83,6 +110,7 @@ dependencies:
|
|
83
110
|
- !ruby/object:Gem::Dependency
|
84
111
|
name: hashie
|
85
112
|
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
86
114
|
requirements:
|
87
115
|
- - ~>
|
88
116
|
- !ruby/object:Gem::Version
|
@@ -90,6 +118,7 @@ dependencies:
|
|
90
118
|
type: :runtime
|
91
119
|
prerelease: false
|
92
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
93
122
|
requirements:
|
94
123
|
- - ~>
|
95
124
|
- !ruby/object:Gem::Version
|
@@ -103,6 +132,7 @@ extra_rdoc_files: []
|
|
103
132
|
files:
|
104
133
|
- .gitignore
|
105
134
|
- .rspec
|
135
|
+
- .travis.yml
|
106
136
|
- Gemfile
|
107
137
|
- LICENSE.txt
|
108
138
|
- README.md
|
@@ -114,7 +144,9 @@ files:
|
|
114
144
|
- lib/rapidash/errors.rb
|
115
145
|
- lib/rapidash/http_client.rb
|
116
146
|
- lib/rapidash/oauth_client.rb
|
147
|
+
- lib/rapidash/resourceable.rb
|
117
148
|
- lib/rapidash/response.rb
|
149
|
+
- lib/rapidash/test_client.rb
|
118
150
|
- lib/rapidash/urlable.rb
|
119
151
|
- lib/rapidash/version.rb
|
120
152
|
- rapidash.gemspec
|
@@ -122,40 +154,53 @@ files:
|
|
122
154
|
- spec/rapidash/client_spec.rb
|
123
155
|
- spec/rapidash/clientable_spec.rb
|
124
156
|
- spec/rapidash/http_client_spec.rb
|
157
|
+
- spec/rapidash/integration/test_client_spec.rb
|
125
158
|
- spec/rapidash/oauth_client_spec.rb
|
159
|
+
- spec/rapidash/resourceable_spec.rb
|
126
160
|
- spec/rapidash/response_spec.rb
|
161
|
+
- spec/rapidash/test_client_spec.rb
|
127
162
|
- spec/rapidash/urlable_spec.rb
|
128
163
|
- spec/spec_helper.rb
|
129
164
|
homepage: ''
|
130
165
|
licenses:
|
131
166
|
- MIT
|
132
|
-
metadata: {}
|
133
167
|
post_install_message:
|
134
168
|
rdoc_options: []
|
135
169
|
require_paths:
|
136
170
|
- lib
|
137
171
|
required_ruby_version: !ruby/object:Gem::Requirement
|
172
|
+
none: false
|
138
173
|
requirements:
|
139
|
-
- - '>='
|
174
|
+
- - ! '>='
|
140
175
|
- !ruby/object:Gem::Version
|
141
176
|
version: '0'
|
177
|
+
segments:
|
178
|
+
- 0
|
179
|
+
hash: -4317292123252369380
|
142
180
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
181
|
+
none: false
|
143
182
|
requirements:
|
144
|
-
- - '>='
|
183
|
+
- - ! '>='
|
145
184
|
- !ruby/object:Gem::Version
|
146
185
|
version: '0'
|
186
|
+
segments:
|
187
|
+
- 0
|
188
|
+
hash: -4317292123252369380
|
147
189
|
requirements: []
|
148
190
|
rubyforge_project:
|
149
|
-
rubygems_version:
|
191
|
+
rubygems_version: 1.8.24
|
150
192
|
signing_key:
|
151
|
-
specification_version:
|
193
|
+
specification_version: 3
|
152
194
|
summary: An opinionated core for creating clients for RESTful APIs quickly
|
153
195
|
test_files:
|
154
196
|
- spec/rapidash/base_spec.rb
|
155
197
|
- spec/rapidash/client_spec.rb
|
156
198
|
- spec/rapidash/clientable_spec.rb
|
157
199
|
- spec/rapidash/http_client_spec.rb
|
200
|
+
- spec/rapidash/integration/test_client_spec.rb
|
158
201
|
- spec/rapidash/oauth_client_spec.rb
|
202
|
+
- spec/rapidash/resourceable_spec.rb
|
159
203
|
- spec/rapidash/response_spec.rb
|
204
|
+
- spec/rapidash/test_client_spec.rb
|
160
205
|
- spec/rapidash/urlable_spec.rb
|
161
206
|
- spec/spec_helper.rb
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: eb726775f243e6328acdfa4a53fba2ecbb81727d
|
4
|
-
data.tar.gz: fe0fdea46f6798d999095c5acf7d337c291cba14
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 16a571aab676ca4d1f8f4209abbe3104184920c78f65349dbf50f12f6abf089f4347fefc66f57b154a55fd0752a3a548432a29fdd783748293de30f9d9ac0b74
|
7
|
-
data.tar.gz: fd8f336dc1436a0ca683c16a692006851dd6f3e9e5eddf0afc7d558740b1006dbbb66b4b92bf47a5656f04d91e1891c06952b3a15d7714788accc4a6c96f289f
|