weary 0.5.1 → 0.6.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/README.md +31 -36
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/examples/repo.rb +12 -15
- data/examples/status.rb +5 -4
- data/lib/weary.rb +35 -232
- data/lib/weary/base.rb +126 -0
- data/lib/weary/request.rb +4 -8
- data/lib/weary/resource.rb +92 -88
- data/lib/weary/response.rb +0 -11
- data/spec/spec_helper.rb +3 -2
- data/spec/weary/base_spec.rb +321 -0
- data/spec/weary/request_spec.rb +2 -0
- data/spec/weary/resource_spec.rb +230 -74
- data/spec/weary/response_spec.rb +7 -11
- data/spec/weary_spec.rb +0 -157
- data/weary.gemspec +11 -4
- metadata +15 -2
data/README.md
CHANGED
@@ -17,21 +17,19 @@ Peruse the [Wiki](http://wiki.github.com/mwunsch/weary) to discover libraries bu
|
|
17
17
|
## Requirements
|
18
18
|
|
19
19
|
+ [Crack](http://github.com/jnunemaker/crack) >= 0.1.2
|
20
|
-
+ [
|
21
|
-
+ [OAuth](http://github.com/mojodna/oauth) >= 0.3.5 (if you want to use OAuth)
|
20
|
+
+ [OAuth](http://github.com/mojodna/oauth) >= 0.3.5
|
22
21
|
+ [RSpec](http://rspec.info/) (for running the tests)
|
23
22
|
|
24
23
|
## Installation
|
25
24
|
|
26
|
-
You do have Rubygems right?
|
25
|
+
You do have Rubygems right? You do use [Gemcutter](http://gemcutter.org/), right?
|
27
26
|
|
28
|
-
|
27
|
+
gem install weary
|
29
28
|
|
30
29
|
## Quick Start
|
31
30
|
|
32
31
|
# http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users%C2%A0show
|
33
|
-
class TwitterUser
|
34
|
-
extend Weary
|
32
|
+
class TwitterUser < Weary::Base
|
35
33
|
|
36
34
|
domain "http://twitter.com/users/"
|
37
35
|
|
@@ -41,7 +39,7 @@ You do have Rubygems right?
|
|
41
39
|
end
|
42
40
|
|
43
41
|
user = TwitterUser.new
|
44
|
-
me = user.show(:id => "markwunsch")
|
42
|
+
me = user.show(:id => "markwunsch").perform
|
45
43
|
puts me["name"]
|
46
44
|
|
47
45
|
Hey, that's me!
|
@@ -49,10 +47,9 @@ Hey, that's me!
|
|
49
47
|
|
50
48
|
## How it works
|
51
49
|
|
52
|
-
Create a class
|
50
|
+
Create a class that inherits from `Weary::Base` to give it methods to craft a resource request:
|
53
51
|
|
54
|
-
class Foo
|
55
|
-
extend Weary
|
52
|
+
class Foo < Weary::Base
|
56
53
|
|
57
54
|
declare "foo" do |resource|
|
58
55
|
resource.url = "http://path/to/foo"
|
@@ -66,12 +63,9 @@ Besides the name of the resource, you can also give `declare_resource` a block l
|
|
66
63
|
declare "foo" do |r|
|
67
64
|
r.url = "path/to/foo"
|
68
65
|
r.via = :post # defaults to :get
|
69
|
-
r.format = :xml # defaults to :json
|
70
66
|
r.requires = [:id, :bar] # an array of params that the resource requires to be in the query/body
|
71
67
|
r.with = [:blah] # an array of params that you can optionally send to the resource
|
72
|
-
r.authenticates = false # does the method require
|
73
|
-
r.oauth = false # does this resource use OAuth to authorize you? it's boolean
|
74
|
-
r.access_token = nil # if you're using OAuth, you should provide the user's access token.
|
68
|
+
r.authenticates = false # does the method require authentication? defaults to false
|
75
69
|
r.follows = false # if this is set to false, the formed request will not follow redirects.
|
76
70
|
r.headers = {'Accept' => 'text/html'} # send custom headers. defaults to nil.
|
77
71
|
end
|
@@ -81,64 +75,65 @@ So this would form a method:
|
|
81
75
|
x = Foo.new
|
82
76
|
x.foo(:id => "mwunsch", :bar => 123)
|
83
77
|
|
84
|
-
That method would return a Weary::
|
78
|
+
That method would return a `Weary::Request` object. Use the `perform` method and get a `Weary::Response` that you could parse and/or examine.
|
85
79
|
|
86
80
|
### Parsing the Body
|
87
81
|
|
88
82
|
Once you make your request with the fancy method that Weary created for you, you can do stuff with what it returns...which could be a good reason you're using Weary in the first place. Let's look at the above example:
|
89
83
|
|
90
84
|
x = Foo.new
|
91
|
-
y = x.foo(:id => "mwunsch", :bar => 123).parse
|
85
|
+
y = x.foo(:id => "mwunsch", :bar => 123).perform.parse
|
92
86
|
y["foos"]["user"]
|
93
87
|
|
94
|
-
Weary parses with Crack
|
88
|
+
Weary parses with Crack, but you're not beholden to it. You can get the raw Request body to have your way with:
|
95
89
|
|
96
90
|
x = Foo.new
|
97
|
-
y = x.foo(:id => "mwunsch", :bar => 123)
|
98
|
-
y.
|
91
|
+
y = x.foo(:id => "mwunsch", :bar => 123).perform
|
92
|
+
Nokogiri.parse(y.body)
|
99
93
|
|
100
|
-
|
94
|
+
*note: Weary used to have Nokogiri built in, using the `#search` method, but that was dropped.*
|
101
95
|
|
102
96
|
### Shortcuts
|
103
97
|
|
104
98
|
Of course, you don't always have to use `declare`; that is a little too ambiguous. You can also use `get`, `post`, `delete`, etc. Those do the obvious.
|
105
99
|
|
106
|
-
The `#requires` and `#with` methods can either be arrays of symbols, or a comma delimited list of strings.
|
107
|
-
|
108
100
|
### Forming URLs
|
109
101
|
|
110
102
|
There are many ways to form URLs in Weary. You can define URLs for the entire class by typing:
|
111
103
|
|
112
|
-
class Foo
|
113
|
-
extend Weary
|
104
|
+
class Foo < Weary::Base
|
114
105
|
|
115
106
|
domain "http://foo.bar/"
|
116
|
-
url "<domain><resource>.<format>"
|
117
107
|
format :xml
|
118
108
|
|
119
109
|
get "show_users"
|
120
110
|
end
|
121
111
|
|
122
|
-
|
123
|
-
|
124
|
-
If you use the `<domain>` flag but don't define a domain, an exception will be raised.
|
112
|
+
If you don't supply a url when declaring the Resource, Weary will look to see if you've defined a domain, and will make a url for you. The above `get` declaration creates a url that looks like: *http://foo.bar/show_users.xml*
|
125
113
|
|
126
114
|
### Weary DSL
|
127
115
|
|
128
116
|
You can create some defaults for all of our resources easily:
|
129
117
|
|
130
|
-
class Foo
|
131
|
-
|
118
|
+
class Foo < Weary::Base
|
119
|
+
|
120
|
+
def initialize(username,password)
|
121
|
+
self.credentials username,password #basic authentication
|
122
|
+
self.defaults = {:user => username} #parameters that will be passed in every request
|
123
|
+
end
|
132
124
|
|
133
125
|
domain "http://foo.bar/"
|
134
|
-
url "<domain><resource>.<format>"
|
135
126
|
format :xml
|
136
|
-
headers {'Accept' => 'text/html'}
|
137
|
-
authenticates "basic_username","basic_password" # basic authentication
|
138
|
-
with [:login, :token] # params that should be sent with every request
|
139
|
-
oauth OAuth::AccessToken.new(consumer, "token", "secret") # an access token for OAuth
|
127
|
+
headers {'Accept' => 'text/html'} # set headers
|
140
128
|
|
141
|
-
post "update" # uses the defaults defined above!
|
129
|
+
post "update" {|r| r.authenticates = true} # uses the defaults defined above!
|
142
130
|
end
|
143
131
|
|
132
|
+
Then you can do something like this:
|
133
|
+
|
134
|
+
f = Foo.new('me','secretz')
|
135
|
+
f.update
|
136
|
+
|
137
|
+
Which will create a POST Request for *http://foo.bar/update.xml* that will authenticate you, using basic authentication, with the username/password of "me"/"secrets" and will send the parameter `{:user => "me"}`. Easy.
|
138
|
+
|
144
139
|
There's more to discover in the Wiki.
|
data/Rakefile
CHANGED
@@ -23,6 +23,7 @@ begin
|
|
23
23
|
gemspec.description = "The Weary need REST: a tiny DSL that makes the consumption of RESTful web services simple."
|
24
24
|
gemspec.authors = "Mark Wunsch"
|
25
25
|
gemspec.add_dependency('crack', '>= 0.1.2')
|
26
|
+
gemspec.add_dependency('oauth', '>= 0.3.5')
|
26
27
|
end
|
27
28
|
Jeweler::GemcutterTasks.new
|
28
29
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/examples/repo.rb
CHANGED
@@ -1,23 +1,20 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require 'rubygems'
|
3
|
+
require 'weary'
|
2
4
|
|
3
|
-
class Repository
|
4
|
-
extend Weary
|
5
|
+
class Repository < Weary::Base
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def initialize(user, repo)
|
8
|
+
self.modify_resource(:show) do |r|
|
9
|
+
r.url = "http://github.com/api/v2/yaml/repos/show/#{user}/#{repo}"
|
10
|
+
end
|
11
|
+
end
|
11
12
|
|
12
13
|
get "show" do |r|
|
13
|
-
r.url = "
|
14
|
-
end
|
15
|
-
|
16
|
-
get "network" do |r|
|
17
|
-
r.url = "<domain><format>/repos/show/#{@gh_user}/#{@gh_repo}/network"
|
14
|
+
r.url = "http://github.com/api/v2/yaml/repos/show/__gh_user__/__gh_repo__"
|
18
15
|
end
|
19
16
|
|
20
17
|
end
|
21
18
|
|
22
|
-
weary = Repository.new
|
23
|
-
puts weary.show.body
|
19
|
+
weary = Repository.new('mwunsch','weary')
|
20
|
+
puts weary.show.perform.body
|
data/examples/status.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require 'rubygems'
|
3
|
+
require 'weary'
|
2
4
|
|
3
|
-
class Status
|
4
|
-
extend Weary
|
5
|
+
class Status < Weary::Base
|
5
6
|
|
6
7
|
domain "http://twitter.com/statuses/"
|
7
8
|
|
@@ -13,5 +14,5 @@ class Status
|
|
13
14
|
end
|
14
15
|
|
15
16
|
toots = Status.new
|
16
|
-
recent_toot = toots.user_timeline(:id => "markwunsch")[0]
|
17
|
+
recent_toot = toots.user_timeline(:id => "markwunsch").perform[0]
|
17
18
|
puts "@" + recent_toot["user"]["screen_name"] + ": " + "\"#{recent_toot['text']}\""
|
data/lib/weary.rb
CHANGED
@@ -1,24 +1,18 @@
|
|
1
|
-
$:.unshift(File.dirname(__FILE__))
|
2
|
-
|
3
1
|
require 'uri'
|
4
2
|
require 'net/http'
|
5
3
|
require 'net/https'
|
6
4
|
|
7
|
-
require 'rubygems'
|
8
5
|
require 'crack'
|
6
|
+
require 'oauth'
|
9
7
|
|
10
|
-
gem 'nokogiri'
|
11
|
-
gem 'oauth'
|
12
8
|
autoload :Yaml, 'yaml'
|
13
|
-
autoload :Nokogiri, 'nokogiri'
|
14
|
-
autoload :OAuth, 'oauth'
|
15
9
|
|
16
10
|
require 'weary/request'
|
17
11
|
require 'weary/response'
|
18
12
|
require 'weary/resource'
|
19
13
|
require 'weary/exceptions'
|
20
14
|
require 'weary/httpverb'
|
21
|
-
|
15
|
+
require 'weary/base'
|
22
16
|
|
23
17
|
module Weary
|
24
18
|
|
@@ -27,8 +21,39 @@ module Weary
|
|
27
21
|
:xml => [:xml, 'xml', 'text/xml', 'application/xml'],
|
28
22
|
:html => [:html, 'html', 'text/html'],
|
29
23
|
:yaml => [:yaml, 'yaml', 'application/x-yaml', 'text/yaml'],
|
30
|
-
:plain => [:plain, 'plain', 'text/plain']
|
31
|
-
|
24
|
+
:plain => [:plain, 'plain', 'text/plain']
|
25
|
+
}
|
26
|
+
# A collection of User Agent strings that I stole from HURL (http://hurl.it)
|
27
|
+
UserAgents = {
|
28
|
+
"Firefox 1.5.0.12 - Mac" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12",
|
29
|
+
"Firefox 1.5.0.12 - Windows" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12",
|
30
|
+
"Firefox 2.0.0.12 - Mac" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12",
|
31
|
+
"Firefox 2.0.0.12 - Windows" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12",
|
32
|
+
"Firefox 3.0.4 - Mac" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4",
|
33
|
+
"Firefox 3.0.4 - Windows" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/2008102920 Firefox/3.0.4",
|
34
|
+
"Firefox 3.5.2 - Mac" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2",
|
35
|
+
"Firefox 3.5.2 - Windows" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2",
|
36
|
+
"Internet Explorer 5.2.3 – Mac" => "Mozilla/4.0 (compatible; MSIE 5.23; Mac_PowerPC)",
|
37
|
+
"Internet Explorer 5.5" => "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)",
|
38
|
+
"Internet Explorer 6.0" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
|
39
|
+
"Internet Explorer 7.0" => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
|
40
|
+
"Internet Explorer 8.0" => "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0)",
|
41
|
+
"Lynx 2.8.4rel.1 on Linux" => "Lynx/2.8.4rel.1 libwww-FM/2.14",
|
42
|
+
"MobileSafari 1.1.3 - iPhone" => "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3",
|
43
|
+
"MobileSafari 1.1.3 - iPod touch" => "Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3",
|
44
|
+
"Opera 9.25 - Mac" => "Opera/9.25 (Macintosh; Intel Mac OS X; U; en)",
|
45
|
+
"Opera 9.25 - Windows" => "Opera/9.25 (Windows NT 5.1; U; en)",
|
46
|
+
"Safari 1.2.4 - Mac" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.5.7 (KHTML, like Gecko) Safari/125.12",
|
47
|
+
"Safari 1.3.2 - Mac" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.6",
|
48
|
+
"Safari 2.0.4 - Mac" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/419 (KHTML, like Gecko) Safari/419.3",
|
49
|
+
"Safari 3.0.4 - Mac" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10",
|
50
|
+
"Safari 3.1.2 - Mac" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_2; en-us) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13",
|
51
|
+
"Safari 3.1.2 - Windows" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-us) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13",
|
52
|
+
"Safari 3.2.1 - Mac" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1",
|
53
|
+
"Safari 3.2.1 - Windows" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1",
|
54
|
+
"Safari 4.0.2 - Mac" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.19.2 (KHTML, like Gecko) Version/4.0.2 Safari/530.19",
|
55
|
+
"Safari 4.0.2 - Windows" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/530.19.2 (KHTML, like Gecko) Version/4.0.2 Safari/530.19.1"
|
56
|
+
}
|
32
57
|
|
33
58
|
# Weary::Query quickly performs a GET request on a URL and parses the request.
|
34
59
|
def self.Query(url)
|
@@ -36,226 +61,4 @@ module Weary
|
|
36
61
|
req.parse
|
37
62
|
end
|
38
63
|
|
39
|
-
attr_reader :resources
|
40
|
-
|
41
|
-
# Sets the domain the resource is on or use it to retrieve a domain you've already set.
|
42
|
-
# It's a getter and a setter. It's an attribute!
|
43
|
-
#
|
44
|
-
# If the domain is not provided and you use a URL pattern that asks for it,
|
45
|
-
# an exception will be raised.
|
46
|
-
def domain(dom=nil)
|
47
|
-
raise ArgumentError, 'No domain provided' if (dom.nil? && @domain.nil?)
|
48
|
-
if (!dom.nil?)
|
49
|
-
parse_domain = URI.extract(dom)
|
50
|
-
raise ArgumentError, 'The domain must be a URL.' if parse_domain.empty?
|
51
|
-
@domain = parse_domain[0]
|
52
|
-
end
|
53
|
-
return @domain
|
54
|
-
end
|
55
|
-
alias on_domain domain
|
56
|
-
|
57
|
-
# Sets a default format to make your Requests in.
|
58
|
-
# Defaults to JSON.
|
59
|
-
def format(format)
|
60
|
-
@default_format = format
|
61
|
-
end
|
62
|
-
alias as_format format
|
63
|
-
|
64
|
-
# Construct a URL pattern for your resources to follow.
|
65
|
-
# You can use flags like
|
66
|
-
# * <domain>
|
67
|
-
# * <format>
|
68
|
-
# * <resource>
|
69
|
-
# To aid your construction. Defaults to "<domain><resource>.<format>"
|
70
|
-
def url(pattern)
|
71
|
-
@url_pattern = pattern.to_s
|
72
|
-
end
|
73
|
-
alias construct_url url
|
74
|
-
|
75
|
-
def authenticates(username,password)
|
76
|
-
@username = username
|
77
|
-
@password = password
|
78
|
-
return nil
|
79
|
-
end
|
80
|
-
alias authenticates_with authenticates
|
81
|
-
|
82
|
-
def with(params)
|
83
|
-
@always_with = params
|
84
|
-
end
|
85
|
-
alias always_with with
|
86
|
-
|
87
|
-
# Set custom Headers for your Request
|
88
|
-
def headers(headers)
|
89
|
-
@headers = headers
|
90
|
-
end
|
91
|
-
alias set_headers headers
|
92
|
-
|
93
|
-
# Set the Access Token for OAuth. This must be an OAuth::AccessToken object.
|
94
|
-
# See http://github.com/mojodna/oauth/ to learn how to create Tokens
|
95
|
-
# Setting this will make resources use OAuth and this token by default.
|
96
|
-
def oauth(token)
|
97
|
-
raise ArgumentError, "Token needs to be an OAuth::AccessToken object" unless token.is_a?(OAuth::AccessToken)
|
98
|
-
@oauth = token
|
99
|
-
end
|
100
|
-
|
101
|
-
# Declare a resource. Use it with a block to setup the resource
|
102
|
-
#
|
103
|
-
# Methods that are understood are:
|
104
|
-
# [<tt>via</tt>] Get, Post, etc. Defaults to a GET request
|
105
|
-
# [<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>
|
106
|
-
# [<tt>requires</tt>] Array of members of <tt>:with</tt> that are required by the resource.
|
107
|
-
# [<tt>authenticates</tt>] Boolean value; does the resource require authentication?
|
108
|
-
# [<tt>oauth</tt>] Boolean value; does the resource use OAuth?
|
109
|
-
# [<tt>access_token</tt>] Provide the Token for OAuth. Must be an OAuth::AccessToken object.
|
110
|
-
# [<tt>url</tt>] The url of the resource. You can use the same flags as #construct_url
|
111
|
-
# [<tt>format</tt>] The format you would like to request. Defaults to json
|
112
|
-
# [<tt>follows</tt>] Boolean; Does this follow redirects? Defaults to true
|
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>)
|
114
|
-
# [<tt>headers</tt>] Set headers for the HTTP Request
|
115
|
-
def declare(name,&block)
|
116
|
-
build_resource(name, :get, block)
|
117
|
-
end
|
118
|
-
alias get declare
|
119
|
-
|
120
|
-
def post(name,&block)
|
121
|
-
build_resource(name, :post, block)
|
122
|
-
end
|
123
|
-
|
124
|
-
def put(name,&block)
|
125
|
-
build_resource(name, :put, block)
|
126
|
-
end
|
127
|
-
|
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?
|
135
|
-
form_resource(resource)
|
136
|
-
return resource
|
137
|
-
end
|
138
|
-
|
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
|
150
|
-
end
|
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
|
162
|
-
end
|
163
|
-
end
|
164
|
-
@resources ||= []
|
165
|
-
@resources << resource.to_hash
|
166
|
-
craft_methods(resource)
|
167
|
-
return resource.to_hash
|
168
|
-
end
|
169
|
-
|
170
|
-
private
|
171
|
-
|
172
|
-
def craft_methods(resource)
|
173
|
-
code = %Q{
|
174
|
-
def #{resource.name}(params={})
|
175
|
-
options ||= {}
|
176
|
-
url = "#{resource.url}"
|
177
|
-
}
|
178
|
-
if resource.with.is_a?(Hash)
|
179
|
-
hash_string = ""
|
180
|
-
resource.with.each_pair {|k,v|
|
181
|
-
if k.is_a?(Symbol)
|
182
|
-
k_string = ":#{k}"
|
183
|
-
else
|
184
|
-
k_string = "'#{k}'"
|
185
|
-
end
|
186
|
-
hash_string << "#{k_string} => '#{v}',"
|
187
|
-
}
|
188
|
-
code << %Q{
|
189
|
-
params = {#{hash_string.chop}}.delete_if {|key,value| value.empty? }.merge(params)
|
190
|
-
}
|
191
|
-
end
|
192
|
-
unless resource.requires.nil?
|
193
|
-
if resource.requires.is_a?(Array)
|
194
|
-
resource.requires.each do |required|
|
195
|
-
code << %Q{ raise ArgumentError, "This resource requires parameter: ':#{required}'" unless params.has_key?(:#{required}) \n}
|
196
|
-
end
|
197
|
-
else
|
198
|
-
resource.requires.each_key do |required|
|
199
|
-
code << %Q{ raise ArgumentError, "This resource requires parameter: ':#{required}'" unless params.has_key?(:#{required}) \n}
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
unless resource.with.empty?
|
204
|
-
if resource.with.is_a?(Array)
|
205
|
-
with = %Q{[#{resource.with.collect {|x| x.is_a?(Symbol) ? ":#{x}" : "'#{x}'" }.join(',')}]}
|
206
|
-
else
|
207
|
-
with = %Q{[#{resource.with.keys.collect {|x| x.is_a?(Symbol) ? ":#{x}" : "'#{x}'"}.join(',')}]}
|
208
|
-
end
|
209
|
-
code << %Q{
|
210
|
-
unnecessary = params.keys - #{with}
|
211
|
-
unnecessary.each { |x| params.delete(x) }
|
212
|
-
}
|
213
|
-
end
|
214
|
-
if resource.via == (:post || :put)
|
215
|
-
code << %Q{options[:body] = params unless params.empty? \n}
|
216
|
-
else
|
217
|
-
code << %Q{
|
218
|
-
options[:query] = params unless params.empty?
|
219
|
-
url << "?" + options[:query].to_params unless options[:query].nil?
|
220
|
-
}
|
221
|
-
end
|
222
|
-
unless (resource.headers.nil? || resource.headers.empty?)
|
223
|
-
header_hash = ""
|
224
|
-
resource.headers.each_pair {|k,v|
|
225
|
-
header_hash << "'#{k}' => '#{v}',"
|
226
|
-
}
|
227
|
-
code << %Q{ options[:headers] = {#{header_hash.chop}} \n}
|
228
|
-
end
|
229
|
-
if resource.authenticates?
|
230
|
-
code << %Q{options[:basic_auth] = {:username => "#{@username}", :password => "#{@password}"} \n}
|
231
|
-
end
|
232
|
-
if resource.oauth?
|
233
|
-
consumer_options = ""
|
234
|
-
resource.access_token.consumer.options.each_pair {|k,v|
|
235
|
-
if k.is_a?(Symbol)
|
236
|
-
k_string = ":#{k}"
|
237
|
-
else
|
238
|
-
k_string = "'#{k}'"
|
239
|
-
end
|
240
|
-
if v.is_a?(Symbol)
|
241
|
-
v_string = ":#{v}"
|
242
|
-
else
|
243
|
-
v_string = "'#{v}'"
|
244
|
-
end
|
245
|
-
consumer_options << "#{k_string} => #{v_string},"
|
246
|
-
}
|
247
|
-
code << %Q{ oauth_consumer = OAuth::Consumer.new("#{resource.access_token.consumer.key}","#{resource.access_token.consumer.secret}",#{consumer_options.chop}) \n}
|
248
|
-
code << %Q{ options[:oauth] = OAuth::AccessToken.new(oauth_consumer, "#{resource.access_token.token}", "#{resource.access_token.secret}") \n}
|
249
|
-
end
|
250
|
-
unless resource.follows_redirects?
|
251
|
-
code << %Q{options[:no_follow] = true \n}
|
252
|
-
end
|
253
|
-
code << %Q{
|
254
|
-
Weary::Request.new(url, :#{resource.via}, options).perform
|
255
|
-
end
|
256
|
-
}
|
257
|
-
class_eval code
|
258
|
-
return code
|
259
|
-
end
|
260
|
-
|
261
64
|
end
|