weary 0.2.3 → 0.3.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.
- data/VERSION +1 -1
- data/examples/status.rb +2 -2
- data/lib/weary.rb +28 -9
- data/lib/weary/resource.rb +16 -6
- data/spec/weary/request_spec.rb +1 -1
- data/spec/weary/resource_spec.rb +10 -0
- data/spec/weary_spec.rb +18 -5
- data/weary.gemspec +2 -2
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/examples/status.rb
CHANGED
@@ -13,5 +13,5 @@ class Status
|
|
13
13
|
end
|
14
14
|
|
15
15
|
toots = Status.new
|
16
|
-
recent_toot = toots.user_timeline(:id => "markwunsch"
|
17
|
-
puts "@" + recent_toot[
|
16
|
+
recent_toot = toots.user_timeline(:id => "markwunsch")[0]
|
17
|
+
puts "@" + recent_toot["user"]["screen_name"] + ": " + "\"#{recent_toot['text']}\""
|
data/lib/weary.rb
CHANGED
@@ -76,8 +76,8 @@ module Weary
|
|
76
76
|
#
|
77
77
|
# Methods that are understood are:
|
78
78
|
# [<tt>via</tt>] Get, Post, etc. Defaults to a GET request
|
79
|
-
# [<tt>with</tt>] An array of parameters that will be passed to the body or query of the request.
|
80
|
-
# [<tt>requires</tt>]
|
79
|
+
# [<tt>with</tt>] An array of parameters that will be passed to the body or query of the request. If you pass a hash, it will define default <tt>values</tt> for params <tt>keys</tt>
|
80
|
+
# [<tt>requires</tt>] Array of members of <tt>:with</tt> that are required by the resource.
|
81
81
|
# [<tt>authenticates</tt>] Boolean value; does the resource require authentication?
|
82
82
|
# [<tt>url</tt>] The url of the resource. You can use the same flags as #construct_url
|
83
83
|
# [<tt>format</tt>] The format you would like to request. Defaults to json
|
@@ -127,7 +127,7 @@ module Weary
|
|
127
127
|
if resource.authenticates?
|
128
128
|
raise StandardError, "Can not authenticate unless username and password are defined" unless (@username && @password)
|
129
129
|
end
|
130
|
-
@resources ||= []
|
130
|
+
@resources ||= []
|
131
131
|
@resources << resource.to_hash
|
132
132
|
craft_methods(resource)
|
133
133
|
return resource.to_hash
|
@@ -139,15 +139,34 @@ module Weary
|
|
139
139
|
options ||= {}
|
140
140
|
url = "#{resource.url}"
|
141
141
|
}
|
142
|
-
|
143
|
-
|
144
|
-
|
142
|
+
if resource.with.is_a?(Hash)
|
143
|
+
hash_string = ""
|
144
|
+
resource.with.each_pair {|k,v| hash_string << ":#{k} => '#{v}',"}
|
145
|
+
code << %Q{
|
146
|
+
params = {#{hash_string.chop}}.delete_if {|key,value| value.empty? }.merge(params)
|
147
|
+
}
|
148
|
+
end
|
149
|
+
unless resource.requires.nil?
|
150
|
+
if resource.requires.is_a?(Array)
|
151
|
+
resource.requires.each do |required|
|
152
|
+
code << %Q{ raise ArgumentError, "This resource requires parameter: ':#{required}'" unless params.has_key?(:#{required}) \n}
|
153
|
+
end
|
154
|
+
else
|
155
|
+
resource.requires.each_key do |required|
|
156
|
+
code << %Q{ raise ArgumentError, "This resource requires parameter: ':#{required}'" unless params.has_key?(:#{required}) \n}
|
157
|
+
end
|
145
158
|
end
|
146
159
|
end
|
147
160
|
unless resource.with.empty?
|
148
|
-
|
149
|
-
|
150
|
-
|
161
|
+
if resource.with.is_a?(Array)
|
162
|
+
with = %Q{[#{resource.with.collect {|x| ":#{x}"}.join(',')}]}
|
163
|
+
else
|
164
|
+
with = %Q{[#{resource.with.keys.collect {|x| ":#{x}"}.join(',')}]}
|
165
|
+
end
|
166
|
+
code << %Q{
|
167
|
+
unnecessary = params.keys - #{with}
|
168
|
+
unnecessary.each { |x| params.delete(x) }
|
169
|
+
}
|
151
170
|
end
|
152
171
|
if resource.via == (:post || :put)
|
153
172
|
code << %Q{options[:body] = params unless params.empty? \n}
|
data/lib/weary/resource.rb
CHANGED
@@ -50,16 +50,26 @@ module Weary
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def with=(params)
|
53
|
-
|
54
|
-
@
|
53
|
+
if params.is_a?(Hash)
|
54
|
+
@requires.each { |key| params[key] = nil unless params.has_key?(key) }
|
55
|
+
@with = params
|
55
56
|
else
|
56
|
-
@
|
57
|
+
unless @requires.nil?
|
58
|
+
@with = params.collect {|x| x.to_sym} | @requires
|
59
|
+
else
|
60
|
+
@with = params.collect {|x| x.to_sym}
|
61
|
+
end
|
57
62
|
end
|
58
63
|
end
|
59
64
|
|
60
|
-
def requires=(params)
|
61
|
-
|
62
|
-
|
65
|
+
def requires=(params)
|
66
|
+
if @with.is_a?(Hash)
|
67
|
+
params.each { |key| @with[key] = nil unless @with.has_key?(key) }
|
68
|
+
@requires = params.collect {|x| x.to_sym}
|
69
|
+
else
|
70
|
+
@with = @with | params.collect {|x| x.to_sym}
|
71
|
+
@requires = params.collect {|x| x.to_sym}
|
72
|
+
end
|
63
73
|
end
|
64
74
|
|
65
75
|
def url=(pattern)
|
data/spec/weary/request_spec.rb
CHANGED
@@ -19,7 +19,7 @@ describe Weary::Request do
|
|
19
19
|
|
20
20
|
it "should perform the request and retrieve a response" do
|
21
21
|
test = Weary::Request.new("http://foo.bar")
|
22
|
-
method = test.method
|
22
|
+
method = test.method
|
23
23
|
response = Weary::Response.new(mock_response(method, 301, {'Location' => 'http://bar.foo'}), method)
|
24
24
|
test.stub!(:perform).and_return(response)
|
25
25
|
test.perform.code.should == 301
|
data/spec/weary/resource_spec.rb
CHANGED
@@ -8,6 +8,7 @@ describe Weary::Resource do
|
|
8
8
|
it 'defaults to a GET request' do
|
9
9
|
@test.via.should == :get
|
10
10
|
end
|
11
|
+
|
11
12
|
it 'should add requirements to the "with" array' do
|
12
13
|
@test.requires = [:foo, :bar]
|
13
14
|
@test.with.should == [:foo, :bar]
|
@@ -18,6 +19,15 @@ describe Weary::Resource do
|
|
18
19
|
@test.with.should == [:foo, :bar]
|
19
20
|
end
|
20
21
|
|
22
|
+
it 'with params could be a hash' do
|
23
|
+
@test.with = {:foo => "Foo", :bar => "Bar"}
|
24
|
+
@test.with.should == {:foo => "Foo", :bar => "Bar"}
|
25
|
+
@test.requires = [:id, :user]
|
26
|
+
@test.with.should == {:bar=>"Bar", :user => nil, :foo => "Foo", :id => nil}
|
27
|
+
@test.with = [:foo, :bar]
|
28
|
+
@test.with.should == [:foo, :bar, :id, :user]
|
29
|
+
end
|
30
|
+
|
21
31
|
it 'authenticates? should be boolean' do
|
22
32
|
@test.authenticates = "foobar"
|
23
33
|
@test.authenticates?.should == true
|
data/spec/weary_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe Weary do
|
|
6
6
|
@test.instance_eval { extend Weary }
|
7
7
|
end
|
8
8
|
|
9
|
-
describe '
|
9
|
+
describe 'Default Domain' do
|
10
10
|
it 'should be set with a url' do
|
11
11
|
@test.on_domain 'http://twitter.com/'
|
12
12
|
@test.domain.should == 'http://twitter.com/'
|
@@ -22,7 +22,7 @@ describe Weary do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
describe '
|
25
|
+
describe 'Default Format' do
|
26
26
|
it 'can be set' do
|
27
27
|
@test.as_format(:xml)
|
28
28
|
@test.instance_variable_get(:@default_format).should == :xml
|
@@ -35,7 +35,7 @@ describe Weary do
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
describe '
|
38
|
+
describe 'Default URL Pattern' do
|
39
39
|
it 'can be set' do
|
40
40
|
@test.construct_url("<domain><resource>.<format>")
|
41
41
|
@test.instance_variable_defined?(:@url_pattern).should == true
|
@@ -47,7 +47,7 @@ describe Weary do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
describe "
|
50
|
+
describe "Basic Authentication Credentials" do
|
51
51
|
# i want to change this to be more specific about it being
|
52
52
|
# basic authentication
|
53
53
|
it "should accept a username and password" do
|
@@ -57,7 +57,20 @@ describe Weary do
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
describe
|
60
|
+
describe "Set Headers" do
|
61
|
+
it "should be a hash of values to pass in the Request head"
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "Common Request Paramaters" do
|
65
|
+
it "should define with params that every resource inherits"
|
66
|
+
# #always_with && #always_requires methods will set with/requires in
|
67
|
+
# the prepare_resource method of Weary
|
68
|
+
|
69
|
+
it "should be able to be a hash"
|
70
|
+
# new feature of Resources
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'Resource Declaration' do
|
61
74
|
before do
|
62
75
|
@test.on_domain "http://foobar.com/"
|
63
76
|
end
|
data/weary.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{weary}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.3.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Mark Wunsch"]
|
9
|
-
s.date = %q{2009-06-
|
9
|
+
s.date = %q{2009-06-18}
|
10
10
|
s.description = %q{The Weary need REST: a tiny DSL that makes the consumption of RESTful web services simple.}
|
11
11
|
s.email = %q{mark@markwunsch.com}
|
12
12
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Wunsch
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-18 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|