Class: Typhoeus::RemoteMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/typhoeus/remote_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @http_method       = options.delete(:method) || :get
  @options           = options
  @base_uri          = options.delete(:base_uri)
  @path              = options.delete(:path)
  @on_success        = options[:on_success]
  @on_failure        = options[:on_failure]
  @cache_responses   = options.delete(:cache_responses)
  @memoize_responses = options.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
  @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, options)
  @response_blocks[args_options_key(args, options)] << block
end

#already_called?(args, options) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/typhoeus/remote_method.rb', line 36

def already_called?(args, options)
  @called_methods.has_key? args_options_key(args, options)
end

#args_options_key(args, options) ⇒ Object



28
29
30
# File 'lib/typhoeus/remote_method.rb', line 28

def args_options_key(args, options)
  "#{args.to_s}+#{options.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

Returns:

  • (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, options)
  key = args_options_key(args, options)
  @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, options)
  @called_methods[args_options_key(args, options)] = 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

Returns:

  • (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 merge_options(new_options)
  merged = options.merge(new_options)
  if options.has_key?(:params) && new_options.has_key?(:params)
    merged[:params] = options[:params].merge(new_options[: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