Class: Typhoeus::RemoteMethod
- Inherits:
-
Object
- Object
- Typhoeus::RemoteMethod
- Defined in:
- lib/typhoeus/remote_method.rb
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#cache_ttl ⇒ Object
Returns the value of attribute cache_ttl.
-
#http_method ⇒ Object
Returns the value of attribute http_method.
-
#on_failure ⇒ Object
Returns the value of attribute on_failure.
-
#on_success ⇒ Object
Returns the value of attribute on_success.
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #add_response_block(block, args, options) ⇒ Object
- #already_called?(args, options) ⇒ Boolean
- #args_options_key(args, options) ⇒ Object
- #argument_names ⇒ Object
- #cache_responses? ⇒ Boolean
- #call_response_blocks(result, args, options) ⇒ Object
- #calling(args, options) ⇒ Object
- #clear_cache ⇒ Object
-
#compile(path) ⇒ Object
rippped from Sinatra.
-
#initialize(options = {}) ⇒ RemoteMethod
constructor
A new instance of RemoteMethod.
- #interpolate_path_with_arguments(args) ⇒ Object
- #memoize_responses? ⇒ Boolean
- #merge_options(new_options) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RemoteMethod
Returns a new instance of RemoteMethod.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/typhoeus/remote_method.rb', line 5 def initialize( = {}) @http_method = .delete(:method) || :get @options = @base_uri = .delete(:base_uri) @path = .delete(:path) @on_success = [:on_success] @on_failure = [:on_failure] @cache_responses = .delete(:cache_responses) @memoize_responses = .delete(:memoize_responses) || @cache_responses @cache_ttl = @cache_responses == true ? 0 : @cache_responses @keys = nil clear_cache end |
Instance Attribute Details
#base_uri ⇒ Object
Returns the value of attribute base_uri.
3 4 5 |
# File 'lib/typhoeus/remote_method.rb', line 3 def base_uri @base_uri end |
#cache_ttl ⇒ Object
Returns the value of attribute cache_ttl.
3 4 5 |
# File 'lib/typhoeus/remote_method.rb', line 3 def cache_ttl @cache_ttl end |
#http_method ⇒ Object
Returns the value of attribute http_method.
3 4 5 |
# File 'lib/typhoeus/remote_method.rb', line 3 def http_method @http_method end |
#on_failure ⇒ Object
Returns the value of attribute on_failure.
3 4 5 |
# File 'lib/typhoeus/remote_method.rb', line 3 def on_failure @on_failure end |
#on_success ⇒ Object
Returns the value of attribute on_success.
3 4 5 |
# File 'lib/typhoeus/remote_method.rb', line 3 def on_success @on_success end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/typhoeus/remote_method.rb', line 3 def @options end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/typhoeus/remote_method.rb', line 3 def path @path end |
Instance Method Details
#add_response_block(block, args, options) ⇒ Object
40 41 42 |
# File 'lib/typhoeus/remote_method.rb', line 40 def add_response_block(block, args, ) @response_blocks[(args, )] << block end |
#already_called?(args, options) ⇒ Boolean
36 37 38 |
# File 'lib/typhoeus/remote_method.rb', line 36 def already_called?(args, ) @called_methods.has_key? (args, ) end |
#args_options_key(args, options) ⇒ Object
28 29 30 |
# File 'lib/typhoeus/remote_method.rb', line 28 def (args, ) "#{args.to_s}+#{.to_s}" end |
#argument_names ⇒ Object
75 76 77 78 79 |
# File 'lib/typhoeus/remote_method.rb', line 75 def argument_names return @keys if @keys pattern, keys = compile(@path) @keys = keys.collect {|k| k.to_sym} end |
#cache_responses? ⇒ Boolean
20 21 22 |
# File 'lib/typhoeus/remote_method.rb', line 20 def cache_responses? @cache_responses end |
#call_response_blocks(result, args, options) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/typhoeus/remote_method.rb', line 44 def call_response_blocks(result, args, ) key = (args, ) @response_blocks[key].each {|block| block.call(result)} @response_blocks.delete(key) @called_methods.delete(key) end |
#calling(args, options) ⇒ Object
32 33 34 |
# File 'lib/typhoeus/remote_method.rb', line 32 def calling(args, ) @called_methods[(args, )] = true end |
#clear_cache ⇒ Object
51 52 53 54 |
# File 'lib/typhoeus/remote_method.rb', line 51 def clear_cache @response_blocks = Hash.new {|h, k| h[k] = []} @called_methods = {} end |
#compile(path) ⇒ Object
rippped from Sinatra. clean up stuff we don’t need later
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/typhoeus/remote_method.rb', line 82 def compile(path) path ||= "" keys = [] if path.respond_to? :to_str special_chars = %w{. + ( )} pattern = path.gsub(/((:\w+)|[\*#{special_chars.join}])/) do |match| case match when "*" keys << 'splat' "(.*?)" when *special_chars Regexp.escape(match) else keys << $2[1..-1] "([^/?&#]+)" end end [/^#{pattern}$/, keys] elsif path.respond_to? :match [path, keys] else raise TypeError, path end end |
#interpolate_path_with_arguments(args) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/typhoeus/remote_method.rb', line 67 def interpolate_path_with_arguments(args) interpolated_path = @path argument_names.each do |arg| interpolated_path = interpolated_path.gsub(":#{arg}", args[arg].to_s) end interpolated_path end |
#memoize_responses? ⇒ Boolean
24 25 26 |
# File 'lib/typhoeus/remote_method.rb', line 24 def memoize_responses? @memoize_responses end |
#merge_options(new_options) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/typhoeus/remote_method.rb', line 56 def () merged = .merge() if .has_key?(:params) && .has_key?(:params) merged[:params] = [:params].merge([:params]) end argument_names.each {|a| merged.delete(a)} merged.delete(:on_success) if merged[:on_success].nil? merged.delete(:on_failure) if merged[:on_failure].nil? merged end |