mwunsch-weary 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/weary/resource.rb +51 -6
- data/lib/weary/response.rb +1 -5
- data/lib/weary.rb +12 -4
- data/spec/weary/resource_spec.rb +61 -0
- data/spec/weary_spec.rb +34 -65
- data/weary.gemspec +4 -2
- metadata +4 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/weary/resource.rb
CHANGED
@@ -1,15 +1,52 @@
|
|
1
1
|
module Weary
|
2
2
|
class Resource
|
3
|
-
|
4
|
-
attr_accessor :domain, :with, :requires, :via, :format, :url, :authenticates, :follows
|
3
|
+
attr_accessor :name, :domain, :with, :requires, :via, :format, :url, :authenticates, :follows
|
5
4
|
|
6
5
|
def initialize(name)
|
7
|
-
|
6
|
+
self.name = name
|
8
7
|
self.via = :get
|
9
8
|
self.authenticates = false
|
10
9
|
self.follows = true
|
11
10
|
self.with = []
|
12
|
-
self.requires
|
11
|
+
self.requires = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def name=(resource_name)
|
15
|
+
resource_name = resource_name.to_s unless resource_name.is_a?(String)
|
16
|
+
@name = resource_name.downcase.strip.gsub(/\s/,'_')
|
17
|
+
end
|
18
|
+
|
19
|
+
def via=(http_verb)
|
20
|
+
@via = case http_verb
|
21
|
+
when *Methods[:get]
|
22
|
+
:get
|
23
|
+
when *Methods[:post]
|
24
|
+
:post
|
25
|
+
when *Methods[:put]
|
26
|
+
:put
|
27
|
+
when *Methods[:delete]
|
28
|
+
:delete
|
29
|
+
else
|
30
|
+
raise ArgumentError, "#{http_verb} is not a supported method"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def format=(type)
|
35
|
+
type = type.downcase if type.is_a?(String)
|
36
|
+
@format = case type
|
37
|
+
when *ContentTypes[:json]
|
38
|
+
:json
|
39
|
+
when *ContentTypes[:xml]
|
40
|
+
:xml
|
41
|
+
when *ContentTypes[:html]
|
42
|
+
:html
|
43
|
+
when *ContentTypes[:yaml]
|
44
|
+
:yaml
|
45
|
+
when *ContentTypes[:plain]
|
46
|
+
:plain
|
47
|
+
else
|
48
|
+
raise ArgumentError, "#{type} is not a recognized format."
|
49
|
+
end
|
13
50
|
end
|
14
51
|
|
15
52
|
def with=(params)
|
@@ -36,11 +73,19 @@ module Weary
|
|
36
73
|
end
|
37
74
|
|
38
75
|
def authenticates?
|
39
|
-
@authenticates
|
76
|
+
if @authenticates
|
77
|
+
true
|
78
|
+
else
|
79
|
+
false
|
80
|
+
end
|
40
81
|
end
|
41
82
|
|
42
83
|
def follows_redirects?
|
43
|
-
@follows
|
84
|
+
if @follows
|
85
|
+
true
|
86
|
+
else
|
87
|
+
false
|
88
|
+
end
|
44
89
|
end
|
45
90
|
|
46
91
|
def to_hash
|
data/lib/weary/response.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Weary
|
2
2
|
class Response
|
3
3
|
|
4
|
-
attr_reader :raw, :method, :code, :message, :header, :content_type, :cookie, :body
|
4
|
+
attr_reader :raw, :method, :code, :message, :header, :content_type, :cookie, :body, :format
|
5
5
|
alias mime_type content_type
|
6
6
|
|
7
7
|
def initialize(http_response, http_method)
|
@@ -42,10 +42,6 @@ module Weary
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
def format
|
46
|
-
@format
|
47
|
-
end
|
48
|
-
|
49
45
|
def follow_redirect
|
50
46
|
if redirected?
|
51
47
|
Request.new(@raw['location'], @method).perform
|
data/lib/weary.rb
CHANGED
@@ -24,6 +24,11 @@ module Weary
|
|
24
24
|
:put => [:put, :PUT, /\bput\b/i],
|
25
25
|
:delete => [:delete, :del, :DELETE, :DEL, /\bdelete\b/i],
|
26
26
|
:head => [:head, :HEAD, /\bhead\b/i] }
|
27
|
+
ContentTypes = { :json => [:json, 'json', 'application/json', 'text/json', 'application/javascript', 'text/javascript'],
|
28
|
+
:xml => [:xml, 'xml', 'text/xml', 'application/xml'],
|
29
|
+
:html => [:html, 'html', 'text/html'],
|
30
|
+
:yaml => [:yaml, 'yaml', 'application/x-yaml', 'text/yaml'],
|
31
|
+
:plain => [:plain, 'plain', 'text/plain'] }
|
27
32
|
UserAgents = { } # will be a collection of user agent strings
|
28
33
|
|
29
34
|
# Weary::Query quickly performs a GET request on a URL and parses the request.
|
@@ -47,8 +52,8 @@ module Weary
|
|
47
52
|
|
48
53
|
# Sets a default format to make your Requests in.
|
49
54
|
# Defaults to JSON.
|
50
|
-
def as_format(format)
|
51
|
-
@default_format = format
|
55
|
+
def as_format(format)
|
56
|
+
@default_format = format
|
52
57
|
end
|
53
58
|
|
54
59
|
# Construct a URL pattern for your resources to follow.
|
@@ -147,8 +152,10 @@ module Weary
|
|
147
152
|
if resource.via == (:post || :put)
|
148
153
|
code << %Q{options[:body] = params unless params.empty? \n}
|
149
154
|
else
|
150
|
-
code << %Q{
|
151
|
-
|
155
|
+
code << %Q{
|
156
|
+
options[:query] = params unless params.empty?
|
157
|
+
url << "?" + options[:query].to_params unless options[:query].nil?
|
158
|
+
}
|
152
159
|
end
|
153
160
|
if resource.authenticates?
|
154
161
|
code << %Q{options[:basic_auth] = {:username => "#{@username}", :password => "#{@password}"} \n}
|
@@ -161,6 +168,7 @@ module Weary
|
|
161
168
|
end
|
162
169
|
}
|
163
170
|
class_eval code
|
171
|
+
return code
|
164
172
|
end
|
165
173
|
|
166
174
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'rspec'
|
3
|
+
require 'spec'
|
4
|
+
require File.join(File.dirname(__FILE__), '../..', 'lib', 'weary')
|
5
|
+
|
6
|
+
describe Weary::Resource do
|
7
|
+
before do
|
8
|
+
@test = Weary::Resource.new("test")
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'defaults to a GET request' do
|
12
|
+
@test.via.should == :get
|
13
|
+
end
|
14
|
+
it 'should add requirements to the "with" array' do
|
15
|
+
@test.requires = [:foo, :bar]
|
16
|
+
@test.with.should == [:foo, :bar]
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'with paramaters could be comma delimited strings' do
|
20
|
+
@test.with = "foo", "bar"
|
21
|
+
@test.with.should == [:foo, :bar]
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'authenticates? should be boolean' do
|
25
|
+
@test.authenticates = "foobar"
|
26
|
+
@test.authenticates?.should == true
|
27
|
+
@test.authenticates = false
|
28
|
+
@test.authenticates?.should == false
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'follows_redirects? should be boolean' do
|
32
|
+
@test.follows = "false"
|
33
|
+
@test.follows_redirects?.should == true
|
34
|
+
@test.follows = false
|
35
|
+
@test.follows_redirects?.should == false
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should be intelligent about crafting names' do
|
39
|
+
@test.name = "foo bar \n"
|
40
|
+
@test.name.should == "foo_bar"
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should only allow specified http methods' do
|
44
|
+
@test.via = "Post"
|
45
|
+
@test.via.should == :post
|
46
|
+
lambda { @test.via = :foobar }.should raise_error
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
it 'format should be a symbol' do
|
51
|
+
@test.format = "xml"
|
52
|
+
@test.format.class.should == Symbol
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'format should be an allowed format' do
|
56
|
+
@test.format = 'text/json'
|
57
|
+
@test.format.should == :json
|
58
|
+
lambda { @test.format = :foobar }.should raise_error
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/spec/weary_spec.rb
CHANGED
@@ -9,40 +9,41 @@ describe Weary do
|
|
9
9
|
@test.instance_eval { extend Weary }
|
10
10
|
end
|
11
11
|
|
12
|
-
describe
|
12
|
+
describe 'default domain' do
|
13
13
|
it 'should be set with a url' do
|
14
|
-
@test.on_domain
|
15
|
-
@test.domain.should ==
|
14
|
+
@test.on_domain 'http://twitter.com/'
|
15
|
+
@test.domain.should == 'http://twitter.com/'
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'should raise an exception when a url is not present' do
|
19
19
|
lambda { @test.on_domain("foobar") }.should raise_error
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it 'should only take the first url given' do
|
23
23
|
@test.on_domain("with http://google.com/ and http://yahoo.com/")
|
24
24
|
@test.domain.should == "http://google.com/"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
describe
|
28
|
+
describe 'default format' do
|
29
29
|
it 'can be set' do
|
30
|
-
@test.as_format(
|
31
|
-
@test.
|
30
|
+
@test.as_format(:xml)
|
31
|
+
@test.instance_variable_get(:@default_format).should == :xml
|
32
32
|
end
|
33
33
|
|
34
|
-
it 'should be a
|
35
|
-
@test.as_format(
|
36
|
-
@test.
|
34
|
+
it 'should be a recognized format' do
|
35
|
+
@test.as_format('text/yaml')
|
36
|
+
@test.on_domain('http://foobar.com/')
|
37
|
+
@test.get('foobar').format.should == :yaml
|
37
38
|
end
|
38
39
|
end
|
39
40
|
|
40
|
-
describe
|
41
|
+
describe 'default url pattern' do
|
41
42
|
it 'can be set' do
|
42
43
|
@test.construct_url("<domain><resource>.<format>")
|
43
44
|
@test.instance_variable_defined?(:@url_pattern).should == true
|
44
45
|
end
|
45
|
-
|
46
|
+
|
46
47
|
it 'should be a string' do
|
47
48
|
@test.construct_url(123)
|
48
49
|
@test.instance_variable_get(:@url_pattern).class.should == String
|
@@ -50,76 +51,44 @@ describe Weary do
|
|
50
51
|
end
|
51
52
|
|
52
53
|
describe "basic authentication credentials" do
|
54
|
+
# i want to change this to be more specific about it being
|
55
|
+
# basic authentication
|
53
56
|
it "should accept a username and password" do
|
54
57
|
@test.authenticates_with("foo","bar")
|
55
58
|
@test.instance_variable_get(:@username).should == "foo"
|
56
59
|
@test.instance_variable_get(:@password).should == "bar"
|
57
|
-
end
|
60
|
+
end
|
58
61
|
end
|
59
62
|
|
60
|
-
describe
|
63
|
+
describe 'resource declaration' do
|
61
64
|
before do
|
62
|
-
@test.on_domain "http://
|
65
|
+
@test.on_domain "http://foobar.com/"
|
63
66
|
end
|
64
67
|
|
65
|
-
it
|
66
|
-
@test.
|
68
|
+
it 'should add a new resource' do
|
69
|
+
@test.get "resource"
|
67
70
|
@test.resources[0].has_key?(:resource).should == true
|
68
71
|
end
|
69
72
|
|
70
|
-
it
|
71
|
-
@test.
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should default to JSON if no format is defined" do
|
75
|
-
@test.declare_resource("resource")[:resource][:format].should == :json
|
73
|
+
it 'should instantiate a Resource object' do
|
74
|
+
@test.get("resource").class.should == Weary::Resource
|
76
75
|
end
|
77
76
|
|
78
|
-
it
|
79
|
-
@test.
|
80
|
-
@test.
|
77
|
+
it 'should craft code to be evaluated' do
|
78
|
+
r = @test.get("resource")
|
79
|
+
@test.send(:craft_methods, r).class.should == String
|
81
80
|
end
|
82
81
|
|
83
|
-
it
|
84
|
-
@test.
|
85
|
-
@test.declare_resource("resource",{:format => :yaml})[:resource][:format].should == :yaml
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should form a url if there is a default pattern" do
|
89
|
-
@test.declare_resource("resource")[:resource][:url].should == "http://twitter.com/resource.json"
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should override the default pattern with it's own url" do
|
93
|
-
@test.declare_resource("resource",{:url => "http://foobar.com/<resource>"})[:resource][:url].should == "http://foobar.com/resource"
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should be able to contain a set of allowed parameters" do
|
97
|
-
@test.declare_resource("resource",{:with => [:id]})[:resource][:with].empty?.should == false
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should be able to contain a set of required parameters" do
|
101
|
-
@test.declare_resource("resource",{:requires => [:id]})[:resource][:requires].empty?.should == false
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should merge required parameters into allowed parameters" do
|
105
|
-
@test.declare_resource("resource",{:requires => [:id]})[:resource][:with].empty?.should == false
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should authenticate with username and password" do
|
109
|
-
@test.authenticates_with("foo","bar")
|
110
|
-
@test.declare_resource("resource",{:authenticates => true})[:resource][:authenticates].should == true
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should raise an exception if authentication is required but no credentials are supplied" do
|
114
|
-
lambda do
|
115
|
-
@test.declare_resource("resource",{:authenticates => true})
|
116
|
-
end.should raise_error
|
117
|
-
end
|
118
|
-
|
119
|
-
it "should create a method for an instantiated object" do
|
120
|
-
@test.declare_resource("resource")
|
82
|
+
it 'should define new instance methods' do
|
83
|
+
@test.get("resource")
|
121
84
|
@test.public_method_defined?(:resource).should == true
|
122
85
|
end
|
123
86
|
|
124
|
-
|
87
|
+
it 'should use sensible default formats' do
|
88
|
+
@test.get("resource").format.should == :json
|
89
|
+
@test.as_format(:xml)
|
90
|
+
@test.post("foo").format.should == :xml
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
125
94
|
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.2.
|
5
|
+
s.version = "0.2.1"
|
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-12}
|
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 = [
|
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
"lib/weary/resource.rb",
|
28
28
|
"lib/weary/response.rb",
|
29
29
|
"spec/weary/request_spec.rb",
|
30
|
+
"spec/weary/resource_spec.rb",
|
30
31
|
"spec/weary_spec.rb",
|
31
32
|
"weary.gemspec"
|
32
33
|
]
|
@@ -38,6 +39,7 @@ Gem::Specification.new do |s|
|
|
38
39
|
s.summary = %q{A little DSL for consuming RESTful web services}
|
39
40
|
s.test_files = [
|
40
41
|
"spec/weary/request_spec.rb",
|
42
|
+
"spec/weary/resource_spec.rb",
|
41
43
|
"spec/weary_spec.rb",
|
42
44
|
"examples/repo.rb",
|
43
45
|
"examples/status.rb"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mwunsch-weary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.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-06-
|
12
|
+
date: 2009-06-12 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- lib/weary/resource.rb
|
46
46
|
- lib/weary/response.rb
|
47
47
|
- spec/weary/request_spec.rb
|
48
|
+
- spec/weary/resource_spec.rb
|
48
49
|
- spec/weary_spec.rb
|
49
50
|
- weary.gemspec
|
50
51
|
has_rdoc: false
|
@@ -75,6 +76,7 @@ specification_version: 3
|
|
75
76
|
summary: A little DSL for consuming RESTful web services
|
76
77
|
test_files:
|
77
78
|
- spec/weary/request_spec.rb
|
79
|
+
- spec/weary/resource_spec.rb
|
78
80
|
- spec/weary_spec.rb
|
79
81
|
- examples/repo.rb
|
80
82
|
- examples/status.rb
|