orthrus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.8.7@orthrus
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in orthrus.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,57 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.co.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the GPL
3
+ (see COPYING.txt file), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) rename any non-standard executables so the names do not conflict
21
+ with standard executables, which must also be provided.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or executable
26
+ form, provided that you do at least ONE of the following:
27
+
28
+ a) distribute the executables and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard executables non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under this terms.
43
+
44
+ They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
45
+ files under the ./missing directory. See each file for the copying
46
+ condition.
47
+
48
+ 5. The scripts and library files supplied as input to or produced as
49
+ output from the software do not automatically fall under the
50
+ copyright of the software, but belong to whomever generated them,
51
+ and may be sold commercially, and may be aggregated with this
52
+ software.
53
+
54
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
55
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
56
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57
+ PURPOSE.
@@ -0,0 +1,81 @@
1
+ # Orthrus
2
+
3
+ http://github.com/xijo/orthrus
4
+
5
+ In Greek mythology, Orthrus (Greek: Όρθρος) was a two-headed dog and son of Typhoeus.
6
+ He was charged with guarding Geryon's herd of red cattle in Erytheia.
7
+
8
+ ## Typhoeus
9
+
10
+ Thanks to the guys who are developing Typhoeus (http://github.com/dbalatero/typhoeus)
11
+
12
+ Orthrus is a small extension inspired by the original remote method API from Paul Dix
13
+ which is deprecated in the current Typhoeus release.
14
+
15
+ It can be used to encapsulate remote method calls in a clean way and provide an
16
+ easy interface to work with.
17
+
18
+ ## Installation
19
+
20
+ gem install orthrus
21
+
22
+ ## Usage
23
+
24
+ *Simple: OpenLibrary Example*
25
+
26
+ require 'rubygems'
27
+ require 'typhoeus'
28
+ require 'orthrus'
29
+
30
+ class OpenLibrary
31
+ include Orthrus
32
+ remote_defaults :base_uri => "http://openlibrary.org/api"
33
+
34
+ define_remote_method :book, :path => '/books'
35
+ end
36
+
37
+ book = OpenLibrary.book(:params => {:bibkeys => "ISBN:0451526538", :format => "json"})
38
+ puts book.inspect # Typhoeus::Response
39
+
40
+ *Advanced: Twitter Example with JSON handling and path interpolation*
41
+
42
+ require 'rubygems'
43
+ require 'typhoeus'
44
+ require 'orthrus'
45
+ require 'json'
46
+
47
+ class Twitter
48
+ include Orthrus
49
+ remote_defaults :on_success => lambda { |response| JSON.parse(response.body) },
50
+ :on_failure => lambda { |response| puts "error code: #{response.code}"; {} },
51
+ :base_uri => "http://api.twitter.com"
52
+
53
+ define_remote_method :search, :path => "/:version/search.json"
54
+ define_remote_method :trends, :path => "/:version/trends/:time_frame.json"
55
+ define_remote_method :tweet, :path => "/:version/statuses/update.json",
56
+ :method => :post
57
+ end
58
+
59
+ # Get all tweets mentioning pluto
60
+ tweets = Twitter.search(:version => 1, :params => {:q => "pluto"})
61
+
62
+ # Get all current trends
63
+ trends = Twitter.trends(:version => 1, :time_frame => :current)
64
+
65
+ # Submit a tweet. Authentication skipped in example.
66
+ Twitter.tweet(:version => 1, :params => {:status => "I #love #planets! :)"})
67
+
68
+ ## TODO
69
+
70
+ - no cache handling yet.
71
+ - make requests hydra-compatible
72
+ - set default parameters
73
+
74
+ ## Author
75
+
76
+ Johannes Opper <xijo@gmx.de>
77
+
78
+ ## License
79
+
80
+ Published under the Ruby License. For detailed information see
81
+ http://www.ruby-lang.org/en/LICENSE.txt and http://www.ruby-lang.org/en/COPYING.txt
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
@@ -0,0 +1,51 @@
1
+ require 'orthrus/remote_method'
2
+
3
+ module Orthrus
4
+ # Include Orthrus functionality in the given class
5
+ # @param base class to include
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ # Define default settings for the remote connection.
13
+ # @attribute remote_defaults
14
+ # @param [Hash] options to be set as default
15
+ # @return [Hash] the current remote default settings
16
+ def remote_defaults(options)
17
+ @remote_defaults ||= {}
18
+ @remote_defaults.merge!(options) if options
19
+ @remote_defaults
20
+ end
21
+
22
+ # If we get subclassed, make sure that child inherits the remote defaults
23
+ # of the parent class.
24
+ def inherited(child)
25
+ child.__send__(:remote_defaults, @remote_defaults)
26
+ end
27
+
28
+ # Declare a remote method and create a class method as wrapper
29
+ # @param [Symbol] name of the remote method
30
+ # @param [Hash] options for the remote method
31
+ def define_remote_method(name, options = {})
32
+ remote_options = (@remote_defaults || {}).merge(options)
33
+ @remote_methods ||= {}
34
+ @remote_methods[name] = RemoteMethod.new(remote_options)
35
+
36
+ class_eval <<-SRC
37
+ def self.#{name.to_s}(args = {})
38
+ call_remote_method(:#{name.to_s}, args)
39
+ end
40
+ SRC
41
+ end
42
+
43
+ # Find the specified remote method and pass the arguments
44
+ # @param [Symbol] method_name to identify the remote method
45
+ # @param [Hash] args to pass through
46
+ def call_remote_method(method_name, args)
47
+ remote_method = @remote_methods[method_name]
48
+ remote_method.run(args)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,58 @@
1
+ module Orthrus
2
+ class RemoteMethod
3
+ attr_accessor :options, :base_uri, :path, :on_success, :on_failure
4
+
5
+ # Extract request options to class variables. All other options
6
+ # will be passed through to Typhoeus.
7
+ # @param [Hash] options for the request
8
+ def initialize(options = {})
9
+ options[:method] ||= :get
10
+ @options = options
11
+ @base_uri = options.delete(:base_uri)
12
+ @path = options.delete(:path)
13
+ @on_success = options[:on_success] || lambda { |response| response }
14
+ @on_failure = options[:on_failure] || lambda { |response| response }
15
+ end
16
+
17
+ # Perform the request, handle response and return the result
18
+ # @param [Hash] args for interpolation and request options
19
+ # @return [Response, Object] the Typhoeus::Response or the result of the on_complete block
20
+ def run(args = {})
21
+ url = base_uri + interpolated_path(args)
22
+ request = Typhoeus::Request.new(url, @options.merge(args))
23
+ handle_response(request)
24
+ Typhoeus::Hydra.hydra.queue request
25
+ Typhoeus::Hydra.hydra.run
26
+ request.handled_response
27
+ end
28
+
29
+ # Interpolate parts of the path marked through color
30
+ # @param [Hash] args to perform interpolation
31
+ # @return [String] the interpolated path
32
+ # @example Interpolate a path
33
+ # path = "/planet/:identifier"
34
+ # interpolated_path({:identifier => "mars"}) #=> "/planet/mars"
35
+ def interpolated_path(args = {})
36
+ interpolated_path = @path
37
+ args.each do |key, value|
38
+ if interpolated_path.include?(":#{key}")
39
+ interpolated_path.sub!(":#{key}", value.to_s)
40
+ args.delete(key)
41
+ end
42
+ end
43
+ interpolated_path
44
+ end
45
+
46
+ # Call success and failure handler on request complete
47
+ # @param [Typhoeus::Request] request that needs success or failure handling
48
+ def handle_response(request)
49
+ request.on_complete do |response|
50
+ if response.success?
51
+ @on_success.call(response)
52
+ else
53
+ @on_failure.call(response)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Orthrus
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "orthrus/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "orthrus"
7
+ s.version = Orthrus::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Johannes Opper"]
10
+ s.email = ["xijo@gmx.de"]
11
+ s.homepage = "http://github.com/xijo/orthrus"
12
+ s.summary = %q{Remote method handling for Typhoeus}
13
+ s.description = %q{Orthrus extends Typhoeus with remote method handling, since it is deprecated in Typhoeus itself.}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- test/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_runtime_dependency 'typhoeus', '~> 0.2'
21
+
22
+ s.add_development_dependency 'test-unit', '~> 2.3'
23
+ s.add_development_dependency 'json', '~> 1.5'
24
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class TestDefineRemoteMethod < Test::Unit::TestCase
4
+ class A
5
+ include Orthrus
6
+ define_remote_method :method_on_a,
7
+ :path => 'entities',
8
+ :base_uri => "http://fancydomain.com"
9
+ end
10
+
11
+ class B
12
+ include Orthrus
13
+ remote_defaults :base_uri => "http://superfancy.com"
14
+ define_remote_method :method_on_b, :path => "entities/index"
15
+ end
16
+
17
+ def test_define_remote_method_without_defaults
18
+ assert A.respond_to? :method_on_a
19
+ end
20
+
21
+ def test_define_remote_method_with_remote_defaults
22
+ assert B.respond_to? :method_on_b
23
+ end
24
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'typhoeus'
4
+ require 'orthrus'
5
+ require 'json'
6
+
7
+ class Test::Unit::TestCase
8
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class TestRemoteDefaults < Test::Unit::TestCase
4
+ class A
5
+ include Orthrus
6
+ remote_defaults :headers => { :authentication => "Basic someting" },
7
+ :base_uri => "http://fancydomain.com"
8
+ end
9
+
10
+ class B < A
11
+ remote_defaults :base_uri => "http://superfancy.com"
12
+ end
13
+
14
+ def test_setting_remote_defaults
15
+ assert_not_nil defaults = A.instance_variable_get(:@remote_defaults)
16
+ assert_equal "http://fancydomain.com", defaults[:base_uri]
17
+ assert_equal "Basic someting", defaults[:headers][:authentication]
18
+ end
19
+
20
+ def test_overwritting_remote_defaults_in_subclass
21
+ assert_not_nil defaults = B.instance_variable_get(:@remote_defaults)
22
+ assert_equal "http://superfancy.com", defaults[:base_uri]
23
+ assert_equal "Basic someting", defaults[:headers][:authentication]
24
+ end
25
+
26
+ end
@@ -0,0 +1,82 @@
1
+ require 'test_helper'
2
+
3
+ class TestDefineRemoteMethod < Test::Unit::TestCase
4
+ def setup
5
+ hydra = Typhoeus::Hydra.hydra
6
+ @index_response = Typhoeus::Response.new(:code => 200, :body => '{ "planets" : ["mars", "earth", "venus"] }', :time => 0.3)
7
+ @show_response = Typhoeus::Response.new(:code => 200, :body => '{ "mars" : { "density" : "3.9335 g/cm3" , "temperature" : "210K" } }', :time => 0.3)
8
+ @error_response = Typhoeus::Response.new(:code => 404, :body => '{ "eris" : "is no planet but a TNO" }', :time => 0.3)
9
+ @put_response = Typhoeus::Response.new(:code => 403, :body => 'Creating planets is not your business!', :time => 0.3)
10
+ hydra.stub(:get, "http://astronomical.joe/planets").and_return(@index_response)
11
+ hydra.stub(:get, "http://astronomical.joe/planets/mars").and_return(@show_response)
12
+ hydra.stub(:get, "http://astronomical.joe/planets/eris").and_return(@error_response)
13
+ hydra.stub(:put, "http://astronomical.joe/planets").and_return(@put_response)
14
+ end
15
+
16
+ def test_path_interpolation
17
+ remote_method = Orthrus::RemoteMethod.new :path => "/some/:id/with/:child"
18
+ args = { :id => 4, :child => 2, :another => 3 }
19
+ interpolated = remote_method.interpolated_path(args)
20
+ assert_equal "/some/4/with/2", interpolated
21
+ assert_equal({ :another => 3 }, args)
22
+ end
23
+
24
+ def test_simple_remote_method_get
25
+ remote_method = Orthrus::RemoteMethod.new(
26
+ :base_uri => "http://astronomical.joe",
27
+ :path => "/planets",
28
+ :method => :get
29
+ )
30
+ assert_equal @index_response.body, remote_method.run.body
31
+ end
32
+
33
+ def test_simple_remote_method_with_get_as_default
34
+ remote_method = Orthrus::RemoteMethod.new(
35
+ :base_uri => "http://astronomical.joe",
36
+ :path => "/planets"
37
+ )
38
+ assert_equal @index_response.body, remote_method.run.body
39
+ end
40
+
41
+ def test_remote_method_with_interpolation
42
+ remote_method = Orthrus::RemoteMethod.new(
43
+ :base_uri => "http://astronomical.joe",
44
+ :path => "/planets/:identifier",
45
+ :method => :get
46
+ )
47
+ assert_equal @show_response.body, remote_method.run(:identifier => :mars).body
48
+ end
49
+
50
+ def test_remote_method_with_success_handler
51
+ remote_method = Orthrus::RemoteMethod.new(
52
+ :base_uri => "http://astronomical.joe",
53
+ :path => "/planets/:identifier",
54
+ :on_success => lambda { |response| JSON.parse(response.body) },
55
+ :method => :get
56
+ )
57
+ response = remote_method.run(:identifier => :mars)
58
+ assert_equal '3.9335 g/cm3', response['mars']['density']
59
+ assert_equal '210K', response['mars']['temperature']
60
+ end
61
+
62
+ def test_remote_method_with_failure_handler
63
+ remote_method = Orthrus::RemoteMethod.new(
64
+ :base_uri => "http://astronomical.joe",
65
+ :path => "/planets/:identifier",
66
+ :on_failure => lambda { |response| JSON.parse(response.body) },
67
+ :method => :get
68
+ )
69
+ response = remote_method.run(:identifier => :eris)
70
+ assert_equal "is no planet but a TNO", response['eris']
71
+ end
72
+
73
+ def test_remote_method_put
74
+ remote_method = Orthrus::RemoteMethod.new(
75
+ :base_uri => "http://astronomical.joe",
76
+ :path => "/planets",
77
+ :method => :put
78
+ )
79
+ response = remote_method.run(:body => '{ :planet => :naboo }')
80
+ assert_equal "Creating planets is not your business!", response.body
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orthrus
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Johannes Opper
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-16 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: typhoeus
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 0
32
+ - 2
33
+ version: "0.2"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: test-unit
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 5
45
+ segments:
46
+ - 2
47
+ - 3
48
+ version: "2.3"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: json
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 5
60
+ segments:
61
+ - 1
62
+ - 5
63
+ version: "1.5"
64
+ type: :development
65
+ version_requirements: *id003
66
+ description: Orthrus extends Typhoeus with remote method handling, since it is deprecated in Typhoeus itself.
67
+ email:
68
+ - xijo@gmx.de
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files: []
74
+
75
+ files:
76
+ - .gitignore
77
+ - .rvmrc
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.markdown
81
+ - Rakefile
82
+ - lib/orthrus.rb
83
+ - lib/orthrus/remote_method.rb
84
+ - lib/orthrus/version.rb
85
+ - orthrus.gemspec
86
+ - test/test_define_remote_method.rb
87
+ - test/test_helper.rb
88
+ - test/test_remote_defaults.rb
89
+ - test/test_remote_method.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/xijo/orthrus
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.6.2
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Remote method handling for Typhoeus
124
+ test_files:
125
+ - test/test_define_remote_method.rb
126
+ - test/test_helper.rb
127
+ - test/test_remote_defaults.rb
128
+ - test/test_remote_method.rb