orthrus 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require 'orthrus/remote_method'
2
2
  require 'orthrus/remote_options'
3
+ require 'orthrus/logger'
3
4
 
4
5
  module Orthrus
5
6
  # Include Orthrus functionality in the given class
@@ -0,0 +1,22 @@
1
+ module Orthrus
2
+ module Logger
3
+ # Initialize logger wether for rails or standalone
4
+ @logger = if defined?(Rails)
5
+ Rails.logger
6
+ else
7
+ require 'logger'
8
+ ::Logger.new(STDOUT)
9
+ end
10
+
11
+ # Display warn message in log
12
+ # @param [String] message
13
+ def self.warn(message)
14
+ @logger.warn("orthrus: #{message}")
15
+ end
16
+
17
+ # @param [String] message
18
+ def self.debug(message)
19
+ @logger.debug("orthrus: #{message}")
20
+ end
21
+ end
22
+ end
@@ -9,9 +9,10 @@ module Orthrus
9
9
  options[:method] ||= :get
10
10
  @options = options === RemoteOptions ? options : RemoteOptions.new(options)
11
11
  @base_uri = options.delete(:base_uri)
12
- @path = options.delete(:path)
12
+ @path = options.delete(:path) || ""
13
13
  @on_success = options[:on_success] || lambda { |response| response }
14
14
  @on_failure = options[:on_failure] || lambda { |response| response }
15
+ @debug = options.delete(:debug)
15
16
  end
16
17
 
17
18
  # Perform the request, handle response and return the result
@@ -19,8 +20,9 @@ module Orthrus
19
20
  # @return [Response, Object] the Typhoeus::Response or the result of the on_complete block
20
21
  def run(args = {})
21
22
  options = @options.smart_merge(args)
22
- url = base_uri + interpolated_path(options)
23
+ url = interpolate(base_uri + path, options)
23
24
  request = Typhoeus::Request.new(url, options)
25
+ Orthrus::Logger.debug("request to #{url}\n with options #{options.inspect}") if @debug
24
26
  handle_response(request)
25
27
  if options[:return_request]
26
28
  request
@@ -31,21 +33,22 @@ module Orthrus
31
33
  end
32
34
  end
33
35
 
34
- # Interpolate parts of the path marked through color
36
+ # Interpolate parts of the given url which are marked by colon
37
+ # @param [String] url to be interpolated
35
38
  # @param [Hash] args to perform interpolation
36
- # @return [String] the interpolated path
37
- # @example Interpolate a path
38
- # path = "/planet/:identifier"
39
- # interpolated_path({:identifier => "mars"}) #=> "/planet/mars"
40
- def interpolated_path(args = {})
41
- interpolated_path = @path || ""
39
+ # @return [String] the interpolated url
40
+ # @example Interpolate a url
41
+ # interpolate("http://example.com/planet/:identifier", {:identifier => "mars"}) #=> "http://example.com/planet/mars"
42
+ def interpolate(url, args = {})
43
+ result = url
42
44
  args.each do |key, value|
43
- if interpolated_path.include?(":#{key}")
44
- interpolated_path.sub!(":#{key}", value.to_s)
45
+ if result.include?(":#{key}")
46
+ result.sub!(":#{key}", value.to_s)
45
47
  args.delete(key)
46
48
  end
47
49
  end
48
- interpolated_path
50
+ Orthrus::Logger.debug("interpolation result is #{result}") if @debug
51
+ result
49
52
  end
50
53
 
51
54
  # Call success and failure handler on request complete
@@ -1,3 +1,3 @@
1
1
  module Orthrus
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -3,9 +3,9 @@ require 'test_helper'
3
3
  class TestDefineRemoteMethod < Test::Unit::TestCase
4
4
 
5
5
  def test_path_interpolation
6
- remote_method = Orthrus::RemoteMethod.new :path => "/some/:id/with/:child"
6
+ remote_method = Orthrus::RemoteMethod.new
7
7
  args = { :id => 4, :child => 2, :another => 3 }
8
- interpolated = remote_method.interpolated_path(args)
8
+ interpolated = remote_method.interpolate('/some/:id/with/:child', args)
9
9
  assert_equal "/some/4/with/2", interpolated
10
10
  assert_equal({ :another => 3 }, args)
11
11
  end
@@ -35,7 +35,7 @@ class TestDefineRemoteMethod < Test::Unit::TestCase
35
35
  )
36
36
  assert_equal @mars_response.body, remote_method.run(:identifier => :mars).body
37
37
  end
38
-
38
+
39
39
  def test_remote_method_with_empty_path
40
40
  remote_method = Orthrus::RemoteMethod.new(
41
41
  :base_uri => "http://astronomical.test/planets/mars",
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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
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-07-15 00:00:00 +02:00
18
+ date: 2011-10-14 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -81,6 +81,7 @@ files:
81
81
  - README.markdown
82
82
  - Rakefile
83
83
  - lib/orthrus.rb
84
+ - lib/orthrus/logger.rb
84
85
  - lib/orthrus/remote_method.rb
85
86
  - lib/orthrus/remote_options.rb
86
87
  - lib/orthrus/version.rb