smoodit 0.0.1

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.
@@ -0,0 +1,25 @@
1
+ module Smoodit
2
+ # Defines HTTP request methods
3
+ module Request
4
+ # Perform an HTTP request
5
+ def request(method, path, options={}, raw=false)
6
+ response = connection(raw).send(method) do |request|
7
+ case method
8
+ when :get, :delete
9
+ request.url(formatted_path(path), options)
10
+ when :post, :put
11
+ request.path = formatted_path(path)
12
+ request.body = options unless options.empty?
13
+ end
14
+ end
15
+
16
+ return raw ? response : response.body, response.status
17
+ end
18
+
19
+ private
20
+
21
+ def formatted_path(path)
22
+ path
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module Smoodit
2
+ # The version of the gem
3
+ VERSION = '0.0.1'.freeze unless defined?(::Smoodit::VERSION)
4
+ end
data/lib/smoodit.rb ADDED
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../smoodit/error', __FILE__)
2
+ require File.expand_path('../smoodit/configuration', __FILE__)
3
+ require File.expand_path('../smoodit/api', __FILE__)
4
+ require File.expand_path('../smoodit/client', __FILE__)
5
+
6
+ module Smoodit
7
+ extend Configuration
8
+
9
+ # Alias for Smoodit::Client.new
10
+ #
11
+ # @return [Smoodit::Client]
12
+ def self.client(options={})
13
+ Smoodit::Client.new(options)
14
+ end
15
+
16
+ # Delegate to Smoodit::Client
17
+ def self.method_missing(method, *args, &block)
18
+ #return super unless client.respond_to?(method)
19
+ client.send(method, *args, &block)
20
+ end
21
+ end
data/smoodit.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/smoodit/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.add_development_dependency('bundler', '~> 1.0')
6
+ s.add_development_dependency('json', '~> 1.4')
7
+ s.add_development_dependency('nokogiri', '~> 1.4')
8
+ s.add_development_dependency('maruku', '~> 0.6')
9
+ s.add_development_dependency('rake', '~> 0.8')
10
+ s.add_development_dependency('rspec', '~> 2.4')
11
+ s.add_development_dependency('simplecov', '~> 0.3')
12
+ s.add_development_dependency('webmock', '~> 1.6')
13
+ s.add_development_dependency('yard', '~> 0.6')
14
+ s.add_development_dependency('ZenTest', '~> 4.4')
15
+ s.add_runtime_dependency('hashie', '~> 1.0.0')
16
+ s.add_runtime_dependency('faraday', '~> 0.5.4')
17
+ s.add_runtime_dependency('faraday_middleware', '~> 0.3.1')
18
+ s.add_runtime_dependency('jruby-openssl', '~> 0.7.2') if RUBY_PLATFORM == 'java'
19
+ s.add_runtime_dependency('multi_json', '~> 0.0.5')
20
+ s.add_runtime_dependency('multi_xml', '~> 0.2.0')
21
+ s.add_runtime_dependency('simple_oauth', '~> 0.1.3')
22
+ s.authors = ["Giovanni Cappellotto"]
23
+ s.description = %q{A Ruby wrapper for the Smood it REST API}
24
+ s.post_install_message =<<eos
25
+ ********************************************************************************
26
+
27
+ Follow @smoodit on Twitter for announcements, updates, and news.
28
+ http://twitter.com/smoodit
29
+
30
+ ********************************************************************************
31
+ eos
32
+ s.email = ['gcappellotto@smood.it']
33
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
34
+ s.files = `git ls-files`.split("\n")
35
+ s.homepage = 'https://github.com/potomak/smoodit'
36
+ s.name = 'smoodit'
37
+ s.platform = Gem::Platform::RUBY
38
+ s.require_paths = ['lib']
39
+ s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6') if s.respond_to? :required_rubygems_version=
40
+ s.rubyforge_project = s.name
41
+ s.summary = %q{Ruby wrapper for the Smood it API}
42
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
43
+ s.version = Smoodit::VERSION.dup
44
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Faraday::Response do
4
+ before do
5
+ @client = Smoodit::Client.new
6
+ end
7
+
8
+ {
9
+ 400 => Smoodit::BadRequest,
10
+ 401 => Smoodit::Unauthorized,
11
+ 403 => Smoodit::Forbidden,
12
+ 404 => Smoodit::NotFound,
13
+ 406 => Smoodit::NotAcceptable,
14
+ 500 => Smoodit::InternalServerError,
15
+ 502 => Smoodit::BadGateway,
16
+ 503 => Smoodit::ServiceUnavailable,
17
+ }.each do |status, exception|
18
+ context "when HTTP status is #{status}" do
19
+
20
+ before do
21
+ stub_get('users/171').to_return(:status => status)
22
+ end
23
+
24
+ it "should raise #{exception.name} error" do
25
+ lambda do
26
+ @client.users(171) {|user| user}
27
+ end.should raise_error(exception)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ {
2
+ "avatar": "http://example.com/avatar.png",
3
+ "web": "http://example.com",
4
+ "username": "john",
5
+ "name": "John Doe",
6
+ "id": 1
7
+ }
@@ -0,0 +1,68 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Smoodit::API do
4
+ before do
5
+ @keys = Smoodit::Configuration::VALID_OPTIONS_KEYS
6
+ end
7
+
8
+ context "with module configuration" do
9
+
10
+ before do
11
+ Smoodit.configure do |config|
12
+ @keys.each do |key|
13
+ config.send("#{key}=", key)
14
+ end
15
+ end
16
+ end
17
+
18
+ after do
19
+ Smoodit.reset
20
+ end
21
+
22
+ it "should inherit module configuration" do
23
+ api = Smoodit::API.new
24
+ @keys.each do |key|
25
+ api.send(key).should == key
26
+ end
27
+ end
28
+
29
+ context "with class configuration" do
30
+
31
+ before do
32
+ @configuration = {
33
+ :consumer_key => 'CK',
34
+ :consumer_secret => 'CS',
35
+ :oauth_token => 'OT',
36
+ :oauth_token_secret => 'OS',
37
+ :adapter => :typhoeus,
38
+ :endpoint => 'http://tumblr.com/',
39
+ :format => :xml,
40
+ :proxy => 'http://giovanni:secret@proxy.example.com:8080',
41
+ :user_agent => 'Custom User Agent',
42
+ }
43
+ end
44
+
45
+ context "during initialization"
46
+
47
+ it "should override module configuration" do
48
+ api = Smoodit::API.new(@configuration)
49
+ @keys.each do |key|
50
+ api.send(key).should == @configuration[key]
51
+ end
52
+ end
53
+
54
+ context "after initilization" do
55
+
56
+ it "should override module configuration after initialization" do
57
+ api = Smoodit::API.new
58
+ @configuration.each do |key, value|
59
+ api.send("#{key}=", value)
60
+ end
61
+ @keys.each do |key|
62
+ api.send(key).should == @configuration[key]
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Smoodit::Client do
4
+ it "should connect using the endpoint configuration" do
5
+ client = Smoodit::Client.new
6
+ endpoint = URI.parse(client.api_endpoint)
7
+ connection = client.send(:connection).build_url(nil).to_s
8
+ connection.should == endpoint.to_s
9
+ end
10
+ end
@@ -0,0 +1,228 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe Smoodit do
4
+ after do
5
+ Smoodit.reset
6
+ end
7
+
8
+ context "when delegating to a client" do
9
+ before do
10
+ stub_get("john").
11
+ to_return(:body => fixture("user_john.js"), :headers => {:content_type => "application/json; charset=utf-8"})
12
+ end
13
+
14
+ it "should get the correct resource" do
15
+ Smoodit.john {|user| user}
16
+ a_get("john").
17
+ should have_been_made
18
+ end
19
+
20
+ it "should return the same results as a client" do
21
+ class_obj = nil
22
+ client_obj = nil
23
+
24
+ Smoodit.john {|user| class_obj = user}
25
+ Smoodit::Client.new.john {|user| client_obj = user}
26
+
27
+ class_obj.should == client_obj
28
+ end
29
+ end
30
+
31
+ context "when making a request to" do
32
+ # https://github.com/smoodit/api/wiki/User-profile
33
+ # https://github.com/smoodit/api/wiki/Your-profile
34
+ ["john", "profile"].each do |member|
35
+ describe "GET #{member}" do
36
+ before do
37
+ stub_get(member)
38
+
39
+ @request = Smoodit.send(member) {|r| r}
40
+ end
41
+
42
+ it "should use '#{member}' request path" do
43
+ @request.proxy.path.should == member
44
+ end
45
+
46
+ it "should use GET request verb" do
47
+ @request.proxy.verb.should == :get
48
+ end
49
+ end
50
+ end
51
+
52
+ # https://github.com/smoodit/api/wiki/Followers
53
+ # https://github.com/smoodit/api/wiki/Following
54
+ # https://github.com/smoodit/api/wiki/User-smoods
55
+ ["followers", "following", "smoods"].each do |member|
56
+ describe "GET john/#{member}" do
57
+ before do
58
+ stub_get("john/#{member}")
59
+
60
+ @request = Smoodit.john.send(member) {|r| r}
61
+ end
62
+
63
+ it "should use 'john/#{member}' request path" do
64
+ @request.proxy.path.should == "john/#{member}"
65
+ end
66
+
67
+ it "should use GET request verb" do
68
+ @request.proxy.verb.should == :get
69
+ end
70
+ end
71
+ end
72
+
73
+ # https://github.com/smoodit/api/wiki/Your-smoods
74
+ describe "GET profile/smoods" do
75
+ before do
76
+ stub_get("profile/smoods")
77
+
78
+ @request = Smoodit.profile.smoods {|r| r}
79
+ end
80
+
81
+ it "should use 'profile/smoods' request path" do
82
+ @request.proxy.path.should == "profile/smoods"
83
+ end
84
+
85
+ it "should use GET request verb" do
86
+ @request.proxy.verb.should == :get
87
+ end
88
+ end
89
+
90
+ # https://github.com/smoodit/api/wiki/Follow-user
91
+ # https://github.com/smoodit/api/wiki/Unfollow-user
92
+ ["follow", "unfollow"].each do |member|
93
+ describe "POST users/123/#{member}" do
94
+ before do
95
+ stub_post("users/123/#{member}")
96
+
97
+ @request = Smoodit.users(123).send(member).post {|r| r}
98
+ end
99
+
100
+ it "should use 'users/123/#{member}' request path" do
101
+ @request.proxy.path.should == "users/123/#{member}"
102
+ end
103
+
104
+ it "should use POST request verb" do
105
+ @request.proxy.verb.should == :post
106
+ end
107
+ end
108
+ end
109
+
110
+ # https://github.com/smoodit/api/wiki/Create-new-smood
111
+ describe "POST smoods" do
112
+ before do
113
+ stub_post("smoods")
114
+
115
+ Smoodit.configure do |config|
116
+ config.format = :test
117
+ end
118
+
119
+ @options = {
120
+ :smood => {
121
+ :mood => :anger
122
+ }
123
+ }
124
+
125
+ @request = Smoodit.smoods.post(@options) {|r| r}
126
+ end
127
+
128
+ it "should use 'smoods' request path" do
129
+ @request.proxy.path.should == "smoods"
130
+ end
131
+
132
+ it "should use POST request verb" do
133
+ @request.proxy.verb.should == :post
134
+ end
135
+
136
+ it "should include new smood parameters into request" do
137
+ @request.proxy.options.should == @options
138
+ end
139
+ end
140
+
141
+ # https://github.com/smoodit/api/wiki/Delete-smood
142
+ describe "DELETE smoods/123" do
143
+ before do
144
+ stub_delete("smoods/123")
145
+
146
+ @request = Smoodit.smoods(123).delete {|r| r}
147
+ end
148
+
149
+ it "should use 'smoods/123' request path" do
150
+ @request.proxy.path.should == "smoods/123"
151
+ end
152
+
153
+ it "should use DELETE request verb" do
154
+ @request.proxy.verb.should == :delete
155
+ end
156
+ end
157
+ end
158
+
159
+ describe ".client" do
160
+ it "should be a Smoodit::Client" do
161
+ Smoodit.client.should be_a Smoodit::Client
162
+ end
163
+ end
164
+
165
+ describe ".adapter" do
166
+ it "should return the default adapter" do
167
+ Smoodit.adapter.should == Smoodit::Configuration::DEFAULT_ADAPTER
168
+ end
169
+ end
170
+
171
+ describe ".adapter=" do
172
+ it "should set the adapter" do
173
+ Smoodit.adapter = :typhoeus
174
+ Smoodit.adapter.should == :typhoeus
175
+ end
176
+ end
177
+
178
+ describe ".endpoint" do
179
+ it "should return the default endpoint" do
180
+ Smoodit.endpoint.should == Smoodit::Configuration::DEFAULT_ENDPOINT
181
+ end
182
+ end
183
+
184
+ describe ".endpoint=" do
185
+ it "should set the endpoint" do
186
+ Smoodit.endpoint = 'http://tumblr.com/'
187
+ Smoodit.endpoint.should == 'http://tumblr.com/'
188
+ end
189
+ end
190
+
191
+ describe ".format" do
192
+ it "should return the default format" do
193
+ Smoodit.format.should == Smoodit::Configuration::DEFAULT_FORMAT
194
+ end
195
+ end
196
+
197
+ describe ".format=" do
198
+ it "should set the format" do
199
+ Smoodit.format = 'xml'
200
+ Smoodit.format.should == 'xml'
201
+ end
202
+ end
203
+
204
+ describe ".user_agent" do
205
+ it "should return the default user agent" do
206
+ Smoodit.user_agent.should == Smoodit::Configuration::DEFAULT_USER_AGENT
207
+ end
208
+ end
209
+
210
+ describe ".user_agent=" do
211
+ it "should set the user_agent" do
212
+ Smoodit.user_agent = 'Custom User Agent'
213
+ Smoodit.user_agent.should == 'Custom User Agent'
214
+ end
215
+ end
216
+
217
+ describe ".configure" do
218
+ Smoodit::Configuration::VALID_OPTIONS_KEYS.each do |key|
219
+
220
+ it "should set the #{key}" do
221
+ Smoodit.configure do |config|
222
+ config.send("#{key}=", key)
223
+ Smoodit.send(key).should == key
224
+ end
225
+ end
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,54 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_group 'Smoodit', 'lib/smoodit'
4
+ add_group 'Faraday Middleware', 'lib/faraday'
5
+ add_group 'Specs', 'spec'
6
+ end
7
+
8
+ require File.expand_path('../../lib/smoodit', __FILE__)
9
+
10
+ require 'rspec'
11
+ require 'webmock/rspec'
12
+ RSpec.configure do |config|
13
+ config.include WebMock::API
14
+ end
15
+
16
+ def a_delete(path)
17
+ a_request(:delete, Smoodit.endpoint + path)
18
+ end
19
+
20
+ def a_get(path)
21
+ a_request(:get, Smoodit.endpoint + path)
22
+ end
23
+
24
+ def a_post(path)
25
+ a_request(:post, Smoodit.endpoint + path)
26
+ end
27
+
28
+ def a_put(path)
29
+ a_request(:put, Smoodit.endpoint + path)
30
+ end
31
+
32
+ def stub_delete(path)
33
+ stub_request(:delete, Smoodit.endpoint + path)
34
+ end
35
+
36
+ def stub_get(path)
37
+ stub_request(:get, Smoodit.endpoint + path)
38
+ end
39
+
40
+ def stub_post(path)
41
+ stub_request(:post, Smoodit.endpoint + path)
42
+ end
43
+
44
+ def stub_put(path)
45
+ stub_request(:put, Smoodit.endpoint + path)
46
+ end
47
+
48
+ def fixture_path
49
+ File.expand_path("../fixtures", __FILE__)
50
+ end
51
+
52
+ def fixture(file)
53
+ File.new(fixture_path + '/' + file)
54
+ end