orthrus 0.0.2 → 0.0.3
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/CHANGELOG.markdown +13 -0
- data/README.markdown +40 -10
- data/lib/orthrus/remote_method.rb +10 -5
- data/lib/orthrus/version.rb +1 -1
- data/test/test_remote_method.rb +12 -3
- metadata +5 -4
data/CHANGELOG.markdown
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
0.0.3
|
2
|
+
-----
|
3
|
+
* Typhoeus::Hydra compatibility added
|
4
|
+
|
5
|
+
0.0.2
|
6
|
+
-----
|
7
|
+
* Improved default option handling
|
8
|
+
|
9
|
+
0.0.1
|
10
|
+
-----
|
11
|
+
* Extracted remote method defining out of Typhoeus
|
12
|
+
* Reestablished compatibility with currenct Typhoeus request interface
|
13
|
+
* Added test to cover whole functionaliy
|
data/README.markdown
CHANGED
@@ -21,7 +21,10 @@ easy interface to work with.
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
-
|
24
|
+
In general, Orthrus will accept all options that Typhoeus does. Here are some
|
25
|
+
examples how to use it:
|
26
|
+
|
27
|
+
### Simple Example: OpenLibrary
|
25
28
|
|
26
29
|
require 'rubygems'
|
27
30
|
require 'typhoeus'
|
@@ -29,15 +32,15 @@ easy interface to work with.
|
|
29
32
|
|
30
33
|
class OpenLibrary
|
31
34
|
include Orthrus
|
32
|
-
remote_defaults :base_uri => "http://openlibrary.org/api"
|
35
|
+
remote_defaults :base_uri => "http://openlibrary.org/api", :format => "json"
|
33
36
|
|
34
37
|
define_remote_method :book, :path => '/books'
|
35
38
|
end
|
36
39
|
|
37
|
-
book = OpenLibrary.book(:params => {:bibkeys => "ISBN:0451526538"
|
40
|
+
book = OpenLibrary.book(:params => {:bibkeys => "ISBN:0451526538"})
|
38
41
|
puts book.inspect # Typhoeus::Response
|
39
42
|
|
40
|
-
|
43
|
+
### Advanced: Twitter Example with JSON handling and path interpolation
|
41
44
|
|
42
45
|
require 'rubygems'
|
43
46
|
require 'typhoeus'
|
@@ -48,7 +51,8 @@ easy interface to work with.
|
|
48
51
|
include Orthrus
|
49
52
|
remote_defaults :on_success => lambda { |response| JSON.parse(response.body) },
|
50
53
|
:on_failure => lambda { |response| puts "error code: #{response.code}"; {} },
|
51
|
-
:base_uri => "http://api.twitter.com"
|
54
|
+
:base_uri => "http://api.twitter.com",
|
55
|
+
:version => 1
|
52
56
|
|
53
57
|
define_remote_method :search, :path => "/:version/search.json"
|
54
58
|
define_remote_method :trends, :path => "/:version/trends/:time_frame.json"
|
@@ -57,19 +61,45 @@ easy interface to work with.
|
|
57
61
|
end
|
58
62
|
|
59
63
|
# Get all tweets mentioning pluto
|
60
|
-
tweets = Twitter.search(:
|
64
|
+
tweets = Twitter.search(:params => {:q => "pluto"})
|
61
65
|
|
62
66
|
# Get all current trends
|
63
|
-
trends = Twitter.trends(:
|
67
|
+
trends = Twitter.trends(:time_frame => :current)
|
64
68
|
|
65
69
|
# Submit a tweet. Authentication skipped in example.
|
66
|
-
Twitter.tweet(:
|
70
|
+
Twitter.tweet(:params => {:status => "I #love #planets! :)"})
|
71
|
+
|
72
|
+
|
73
|
+
### Hydra
|
74
|
+
|
75
|
+
To use Typhoeus' hydra to perform multiple request parallel, you can set the `return_request` option
|
76
|
+
in `remote_defaults`, `define_remote_method` or even on per request basis to `true`.
|
77
|
+
Orthrus will return the crafted Typhoeus::Request, including the success and error handling.
|
78
|
+
|
79
|
+
require 'rubygems'
|
80
|
+
require 'typhoeus'
|
81
|
+
require 'orthrus'
|
82
|
+
|
83
|
+
class WeatherInformation
|
84
|
+
include Orthrus
|
85
|
+
remote_defaults :base_uri => "http://wackyweather.test",
|
86
|
+
:return_request => true
|
87
|
+
|
88
|
+
define_remote_method :city, :path => "/city/:name"
|
89
|
+
end
|
90
|
+
|
91
|
+
hydra = Typhoeus::Hydra.new
|
92
|
+
hydra.queue new_york = WeatherInformation.city(:name => "new_york")
|
93
|
+
hydra.queue berlin = WeatherInformation.city(:name => "berlin")
|
94
|
+
hydra.run
|
95
|
+
|
96
|
+
puts berlin.handled_response
|
97
|
+
|
98
|
+
For more information about the hydra feature, visit: http://github.com/dbalatero/typhoeus
|
67
99
|
|
68
100
|
## TODO
|
69
101
|
|
70
102
|
- no cache handling yet.
|
71
|
-
- make requests hydra-compatible
|
72
|
-
- set default parameters
|
73
103
|
|
74
104
|
## Author
|
75
105
|
|
@@ -18,12 +18,17 @@ module Orthrus
|
|
18
18
|
# @param [Hash] args for interpolation and request options
|
19
19
|
# @return [Response, Object] the Typhoeus::Response or the result of the on_complete block
|
20
20
|
def run(args = {})
|
21
|
-
|
22
|
-
|
21
|
+
options = @options.smart_merge(args)
|
22
|
+
url = base_uri + interpolated_path(options)
|
23
|
+
request = Typhoeus::Request.new(url, options)
|
23
24
|
handle_response(request)
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
if options[:return_request]
|
26
|
+
request
|
27
|
+
else
|
28
|
+
Typhoeus::Hydra.hydra.queue request
|
29
|
+
Typhoeus::Hydra.hydra.run
|
30
|
+
request.handled_response
|
31
|
+
end
|
27
32
|
end
|
28
33
|
|
29
34
|
# Interpolate parts of the path marked through color
|
data/lib/orthrus/version.rb
CHANGED
data/test/test_remote_method.rb
CHANGED
@@ -38,10 +38,10 @@ class TestDefineRemoteMethod < Test::Unit::TestCase
|
|
38
38
|
|
39
39
|
def test_remote_method_with_success_handler
|
40
40
|
remote_method = Orthrus::RemoteMethod.new(
|
41
|
-
:base_uri
|
42
|
-
:path
|
41
|
+
:base_uri => "http://astronomical.test",
|
42
|
+
:path => "/planets/:identifier",
|
43
43
|
:on_success => lambda { |response| JSON.parse(response.body) },
|
44
|
-
:method
|
44
|
+
:method => :get
|
45
45
|
)
|
46
46
|
response = remote_method.run(:identifier => :mars)
|
47
47
|
assert_equal '3.9335 g/cm3', response['mars']['density']
|
@@ -68,4 +68,13 @@ class TestDefineRemoteMethod < Test::Unit::TestCase
|
|
68
68
|
response = remote_method.run(:body => '{"planet":"naboo"}')
|
69
69
|
assert_equal "Creating planets is not your business!", response.body
|
70
70
|
end
|
71
|
+
|
72
|
+
def test_remote_method_returns_request
|
73
|
+
remote_method = Orthrus::RemoteMethod.new(
|
74
|
+
:base_uri => "http://astronomical.test",
|
75
|
+
:path => "/planets",
|
76
|
+
:return_request => true
|
77
|
+
)
|
78
|
+
assert_instance_of Typhoeus::Request, remote_method.run
|
79
|
+
end
|
71
80
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: orthrus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Johannes Opper
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-06-
|
18
|
+
date: 2011-06-20 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -74,6 +74,7 @@ extra_rdoc_files: []
|
|
74
74
|
|
75
75
|
files:
|
76
76
|
- .gitignore
|
77
|
+
- CHANGELOG.markdown
|
77
78
|
- Gemfile
|
78
79
|
- LICENSE
|
79
80
|
- README.markdown
|