weary 0.5.0 → 0.5.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.
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/weary.rb +44 -52
- data/lib/weary/httpverb.rb +32 -0
- data/lib/weary/request.rb +8 -26
- data/lib/weary/resource.rb +6 -12
- data/spec/weary/httpverb_spec.rb +20 -0
- data/spec/weary/resource_spec.rb +2 -1
- data/spec/weary_spec.rb +8 -0
- data/weary.gemspec +10 -5
- metadata +9 -4
data/Rakefile
CHANGED
@@ -24,6 +24,7 @@ begin
|
|
24
24
|
gemspec.authors = "Mark Wunsch"
|
25
25
|
gemspec.add_dependency('crack', '>= 0.1.2')
|
26
26
|
end
|
27
|
+
Jeweler::GemcutterTasks.new
|
27
28
|
rescue LoadError
|
28
29
|
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
29
30
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.1
|
data/lib/weary.rb
CHANGED
@@ -17,15 +17,12 @@ require 'weary/request'
|
|
17
17
|
require 'weary/response'
|
18
18
|
require 'weary/resource'
|
19
19
|
require 'weary/exceptions'
|
20
|
+
require 'weary/httpverb'
|
20
21
|
|
21
22
|
|
22
23
|
module Weary
|
23
24
|
|
24
|
-
Methods =
|
25
|
-
:post => [:post, :POST, /\bpost\b/i],
|
26
|
-
:put => [:put, :PUT, /\bput\b/i],
|
27
|
-
:delete => [:delete, :del, :DELETE, :DEL, /\bdelete\b/i],
|
28
|
-
:head => [:head, :HEAD, /\bhead\b/i] }
|
25
|
+
Methods = [:get, :post, :put, :delete, :head]
|
29
26
|
ContentTypes = { :json => [:json, 'json', 'application/json', 'text/json', 'application/javascript', 'text/javascript'],
|
30
27
|
:xml => [:xml, 'xml', 'text/xml', 'application/xml'],
|
31
28
|
:html => [:html, 'html', 'text/html'],
|
@@ -115,67 +112,62 @@ module Weary
|
|
115
112
|
# [<tt>follows</tt>] Boolean; Does this follow redirects? Defaults to true
|
116
113
|
# [<tt>domain</tt>] Sets the domain you would like this individual resource to be on (if you include the domain flag in <tt>url</tt>)
|
117
114
|
# [<tt>headers</tt>] Set headers for the HTTP Request
|
118
|
-
def declare(name)
|
119
|
-
|
120
|
-
yield resource if block_given?
|
121
|
-
form_resource(resource)
|
122
|
-
return resource
|
115
|
+
def declare(name,&block)
|
116
|
+
build_resource(name, :get, block)
|
123
117
|
end
|
124
118
|
alias get declare
|
125
119
|
|
126
|
-
def post(name)
|
127
|
-
|
128
|
-
yield resource if block_given?
|
129
|
-
form_resource(resource)
|
130
|
-
return resource
|
120
|
+
def post(name,&block)
|
121
|
+
build_resource(name, :post, block)
|
131
122
|
end
|
132
123
|
|
133
|
-
def put(name)
|
134
|
-
|
135
|
-
yield resource if block_given?
|
136
|
-
form_resource(resource)
|
137
|
-
return resource
|
124
|
+
def put(name,&block)
|
125
|
+
build_resource(name, :put, block)
|
138
126
|
end
|
139
127
|
|
140
|
-
def delete(name)
|
141
|
-
|
142
|
-
|
128
|
+
def delete(name,&block)
|
129
|
+
build_resource(name, :delete, block)
|
130
|
+
end
|
131
|
+
|
132
|
+
def build_resource(name,verb,block=nil)
|
133
|
+
resource = prepare_resource(name,verb)
|
134
|
+
block.call(resource) unless block.nil?
|
143
135
|
form_resource(resource)
|
144
136
|
return resource
|
145
137
|
end
|
146
|
-
|
147
|
-
private
|
148
138
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
end
|
161
|
-
return preparation
|
139
|
+
def prepare_resource(name,via)
|
140
|
+
preparation = Weary::Resource.new(name)
|
141
|
+
preparation.via = via
|
142
|
+
preparation.format = (@default_format || :json)
|
143
|
+
preparation.domain = @domain
|
144
|
+
preparation.url = (@url_pattern || "<domain><resource>.<format>")
|
145
|
+
preparation.with = @always_with unless @always_with.nil?
|
146
|
+
preparation.headers = @headers unless (@headers.nil? || @headers.empty?)
|
147
|
+
if !@oauth.nil?
|
148
|
+
preparation.oauth = true
|
149
|
+
preparation.access_token = @oauth
|
162
150
|
end
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
151
|
+
return preparation
|
152
|
+
end
|
153
|
+
|
154
|
+
def form_resource(resource)
|
155
|
+
if resource.authenticates?
|
156
|
+
raise StandardError, "Can not authenticate unless username and password are defined" unless (@username && @password)
|
157
|
+
end
|
158
|
+
if resource.oauth?
|
159
|
+
if resource.access_token.nil?
|
160
|
+
raise StandardError, "Access Token is not provided" if @oauth.nil?
|
161
|
+
resource.access_token = @oauth
|
173
162
|
end
|
174
|
-
@resources ||= []
|
175
|
-
@resources << resource.to_hash
|
176
|
-
craft_methods(resource)
|
177
|
-
return resource.to_hash
|
178
163
|
end
|
164
|
+
@resources ||= []
|
165
|
+
@resources << resource.to_hash
|
166
|
+
craft_methods(resource)
|
167
|
+
return resource.to_hash
|
168
|
+
end
|
169
|
+
|
170
|
+
private
|
179
171
|
|
180
172
|
def craft_methods(resource)
|
181
173
|
code = %Q{
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Weary
|
2
|
+
class HTTPVerb
|
3
|
+
|
4
|
+
attr_accessor :verb
|
5
|
+
|
6
|
+
def initialize(http_verb = :get)
|
7
|
+
self.verb = http_verb
|
8
|
+
end
|
9
|
+
|
10
|
+
def normalize
|
11
|
+
return verb.to_s.strip.downcase.intern
|
12
|
+
end
|
13
|
+
|
14
|
+
def request_class
|
15
|
+
case normalize
|
16
|
+
when :get
|
17
|
+
Net::HTTP::Get
|
18
|
+
when :post
|
19
|
+
Net::HTTP::Post
|
20
|
+
when :put
|
21
|
+
Net::HTTP::Put
|
22
|
+
when :delete
|
23
|
+
Net::HTTP::Delete
|
24
|
+
when :head
|
25
|
+
Net::HTTP::Head
|
26
|
+
else
|
27
|
+
Net::HTTP::Get
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/lib/weary/request.rb
CHANGED
@@ -15,19 +15,11 @@ module Weary
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def method=(http_verb)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
when *Methods[:put]
|
24
|
-
:put
|
25
|
-
when *Methods[:delete]
|
26
|
-
:delete
|
27
|
-
when *Methods[:head]
|
28
|
-
:head
|
29
|
-
else
|
30
|
-
raise ArgumentError, "Only GET, POST, PUT, DELETE, and HEAD methods are supported"
|
18
|
+
verb = HTTPVerb.new(http_verb).normalize
|
19
|
+
@http_verb = if Methods.include?(verb)
|
20
|
+
verb
|
21
|
+
else
|
22
|
+
:get
|
31
23
|
end
|
32
24
|
end
|
33
25
|
|
@@ -48,7 +40,6 @@ module Weary
|
|
48
40
|
response
|
49
41
|
end
|
50
42
|
end
|
51
|
-
alias make perform
|
52
43
|
|
53
44
|
private
|
54
45
|
def http
|
@@ -59,18 +50,9 @@ module Weary
|
|
59
50
|
end
|
60
51
|
|
61
52
|
def request
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
when :post
|
66
|
-
Net::HTTP::Post.new(@uri.request_uri)
|
67
|
-
when :put
|
68
|
-
Net::HTTP::Put.new(@uri.request_uri)
|
69
|
-
when :delete
|
70
|
-
Net::HTTP::Delete.new(@uri.request_uri)
|
71
|
-
when :head
|
72
|
-
Net::HTTP::Head.new(@uri.request_uri)
|
73
|
-
end
|
53
|
+
request_class = HTTPVerb.new(@http_verb).request_class
|
54
|
+
prepare = request_class.new(@uri.request_uri)
|
55
|
+
|
74
56
|
prepare.body = options[:body].is_a?(Hash) ? options[:body].to_params : options[:body] if options[:body]
|
75
57
|
prepare.basic_auth(options[:basic_auth][:username], options[:basic_auth][:password]) if options[:basic_auth]
|
76
58
|
if options[:headers]
|
data/lib/weary/resource.rb
CHANGED
@@ -18,17 +18,11 @@ module Weary
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def via=(http_verb)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
when *Methods[:put]
|
27
|
-
:put
|
28
|
-
when *Methods[:delete]
|
29
|
-
:delete
|
30
|
-
else
|
31
|
-
raise ArgumentError, "#{http_verb} is not a supported method"
|
21
|
+
verb = HTTPVerb.new(http_verb).normalize
|
22
|
+
@via = if Methods.include?(verb)
|
23
|
+
verb
|
24
|
+
else
|
25
|
+
:get
|
32
26
|
end
|
33
27
|
end
|
34
28
|
|
@@ -63,7 +57,7 @@ module Weary
|
|
63
57
|
end
|
64
58
|
end
|
65
59
|
|
66
|
-
def requires=(params)
|
60
|
+
def requires=(params)
|
67
61
|
if @with.is_a?(Hash)
|
68
62
|
params.each { |key| @with[key] = nil unless @with.has_key?(key) }
|
69
63
|
@requires = params.collect {|x| x.to_sym}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
|
+
|
3
|
+
describe Weary::HTTPVerb do
|
4
|
+
before do
|
5
|
+
@test = Weary::HTTPVerb.new("Get")
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should set the verb' do
|
9
|
+
@test.verb.should == "Get"
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should normalize' do
|
13
|
+
@test.normalize.should == :get
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should know what net/http class to request with' do
|
17
|
+
@test.request_class.should == Net::HTTP::Get
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/spec/weary/resource_spec.rb
CHANGED
@@ -77,7 +77,8 @@ describe Weary::Resource do
|
|
77
77
|
it 'should only allow specified http methods' do
|
78
78
|
@test.via = "Post"
|
79
79
|
@test.via.should == :post
|
80
|
-
|
80
|
+
@test.via = :foobar
|
81
|
+
@test.via.should == :get
|
81
82
|
end
|
82
83
|
|
83
84
|
it 'format should be a symbol' do
|
data/spec/weary_spec.rb
CHANGED
@@ -124,6 +124,14 @@ describe Weary do
|
|
124
124
|
@test.on_domain "http://foobar.com/"
|
125
125
|
end
|
126
126
|
|
127
|
+
it 'should have a common method for building requests' do
|
128
|
+
block = Proc.new { |r| r.via = :post; r.url = "http://bar.foo/name.json"}
|
129
|
+
test = @test.build_resource('name',:get,block)
|
130
|
+
test.class.should == Weary::Resource
|
131
|
+
test.url.should == "http://bar.foo/name.json"
|
132
|
+
test.via.should == :post
|
133
|
+
end
|
134
|
+
|
127
135
|
it 'should add a new resource' do
|
128
136
|
@test.get "resource"
|
129
137
|
@test.resources[0].has_key?(:resource).should == true
|
data/weary.gemspec
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
1
4
|
# -*- encoding: utf-8 -*-
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = %q{weary}
|
5
|
-
s.version = "0.5.
|
8
|
+
s.version = "0.5.1"
|
6
9
|
|
7
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
11
|
s.authors = ["Mark Wunsch"]
|
9
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-10-26}
|
10
13
|
s.description = %q{The Weary need REST: a tiny DSL that makes the consumption of RESTful web services simple.}
|
11
14
|
s.email = %q{mark@markwunsch.com}
|
12
15
|
s.extra_rdoc_files = [
|
@@ -23,6 +26,7 @@ Gem::Specification.new do |s|
|
|
23
26
|
"examples/status.rb",
|
24
27
|
"lib/weary.rb",
|
25
28
|
"lib/weary/exceptions.rb",
|
29
|
+
"lib/weary/httpverb.rb",
|
26
30
|
"lib/weary/request.rb",
|
27
31
|
"lib/weary/resource.rb",
|
28
32
|
"lib/weary/response.rb",
|
@@ -30,21 +34,22 @@ Gem::Specification.new do |s|
|
|
30
34
|
"spec/fixtures/twitter.xml",
|
31
35
|
"spec/fixtures/vimeo.json",
|
32
36
|
"spec/spec_helper.rb",
|
37
|
+
"spec/weary/httpverb_spec.rb",
|
33
38
|
"spec/weary/request_spec.rb",
|
34
39
|
"spec/weary/resource_spec.rb",
|
35
40
|
"spec/weary/response_spec.rb",
|
36
41
|
"spec/weary_spec.rb",
|
37
42
|
"weary.gemspec"
|
38
43
|
]
|
39
|
-
s.has_rdoc = true
|
40
44
|
s.homepage = %q{http://github.com/mwunsch/weary}
|
41
45
|
s.rdoc_options = ["--charset=UTF-8"]
|
42
46
|
s.require_paths = ["lib"]
|
43
47
|
s.rubyforge_project = %q{weary}
|
44
|
-
s.rubygems_version = %q{1.3.
|
48
|
+
s.rubygems_version = %q{1.3.5}
|
45
49
|
s.summary = %q{A little DSL for consuming RESTful web services}
|
46
50
|
s.test_files = [
|
47
51
|
"spec/spec_helper.rb",
|
52
|
+
"spec/weary/httpverb_spec.rb",
|
48
53
|
"spec/weary/request_spec.rb",
|
49
54
|
"spec/weary/resource_spec.rb",
|
50
55
|
"spec/weary/response_spec.rb",
|
@@ -55,7 +60,7 @@ Gem::Specification.new do |s|
|
|
55
60
|
|
56
61
|
if s.respond_to? :specification_version then
|
57
62
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
58
|
-
s.specification_version =
|
63
|
+
s.specification_version = 3
|
59
64
|
|
60
65
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
61
66
|
s.add_runtime_dependency(%q<crack>, [">= 0.1.2"])
|
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.5.
|
4
|
+
version: 0.5.1
|
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-
|
12
|
+
date: 2009-10-26 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- examples/status.rb
|
42
42
|
- lib/weary.rb
|
43
43
|
- lib/weary/exceptions.rb
|
44
|
+
- lib/weary/httpverb.rb
|
44
45
|
- lib/weary/request.rb
|
45
46
|
- lib/weary/resource.rb
|
46
47
|
- lib/weary/response.rb
|
@@ -48,6 +49,7 @@ files:
|
|
48
49
|
- spec/fixtures/twitter.xml
|
49
50
|
- spec/fixtures/vimeo.json
|
50
51
|
- spec/spec_helper.rb
|
52
|
+
- spec/weary/httpverb_spec.rb
|
51
53
|
- spec/weary/request_spec.rb
|
52
54
|
- spec/weary/resource_spec.rb
|
53
55
|
- spec/weary/response_spec.rb
|
@@ -55,6 +57,8 @@ files:
|
|
55
57
|
- weary.gemspec
|
56
58
|
has_rdoc: true
|
57
59
|
homepage: http://github.com/mwunsch/weary
|
60
|
+
licenses: []
|
61
|
+
|
58
62
|
post_install_message:
|
59
63
|
rdoc_options:
|
60
64
|
- --charset=UTF-8
|
@@ -75,12 +79,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
79
|
requirements: []
|
76
80
|
|
77
81
|
rubyforge_project: weary
|
78
|
-
rubygems_version: 1.3.
|
82
|
+
rubygems_version: 1.3.5
|
79
83
|
signing_key:
|
80
|
-
specification_version:
|
84
|
+
specification_version: 3
|
81
85
|
summary: A little DSL for consuming RESTful web services
|
82
86
|
test_files:
|
83
87
|
- spec/spec_helper.rb
|
88
|
+
- spec/weary/httpverb_spec.rb
|
84
89
|
- spec/weary/request_spec.rb
|
85
90
|
- spec/weary/resource_spec.rb
|
86
91
|
- spec/weary/response_spec.rb
|