Module: HttpMimic::ModuleMethods

Included in:
HttpMimic
Defined in:
lib/http_mimic/module_methods.rb

Instance Method Summary collapse

Instance Method Details

#auto_fallback(enabled = nil) ⇒ Object



49
50
51
52
# File 'lib/http_mimic/module_methods.rb', line 49

def auto_fallback(enabled = nil)
  return default_options[:auto_fallback] if enabled.nil?
  default_options[:auto_fallback] = enabled
end

#base_uri(uri = nil) ⇒ Object



5
6
7
8
# File 'lib/http_mimic/module_methods.rb', line 5

def base_uri(uri = nil)
  return default_options[:base_uri] if uri.nil?
  default_options[:base_uri] = uri
end

#cookies(c = nil) ⇒ Object



38
39
40
41
42
# File 'lib/http_mimic/module_methods.rb', line 38

def cookies(c = nil)
  return default_options[:cookies] if c.nil?
  default_options[:cookies] ||= {}
  default_options[:cookies].merge!(c)
end

#default_optionsObject



59
60
61
# File 'lib/http_mimic/module_methods.rb', line 59

def default_options
  @default_options ||= {}
end

#default_params(params = nil) ⇒ Object Also known as: default_query



16
17
18
19
20
# File 'lib/http_mimic/module_methods.rb', line 16

def default_params(params = nil)
  return default_options[:query] if params.nil?
  default_options[:query] ||= {}
  default_options[:query].merge!(params)
end

#default_timeout(seconds = nil) ⇒ Object



23
24
25
26
# File 'lib/http_mimic/module_methods.rb', line 23

def default_timeout(seconds = nil)
  return default_options[:timeout] if seconds.nil?
  default_options[:timeout] = seconds
end

#delete(url, options = {}) ⇒ Object



79
80
81
# File 'lib/http_mimic/module_methods.rb', line 79

def delete(url, options = {})
  request(:delete, url, options)
end

#get(url, options = {}) ⇒ Object



63
64
65
# File 'lib/http_mimic/module_methods.rb', line 63

def get(url, options = {})
  request(:get, url, options)
end

#head(url, options = {}) ⇒ Object



83
84
85
# File 'lib/http_mimic/module_methods.rb', line 83

def head(url, options = {})
  request(:head, url, options)
end

#headers(h = nil) ⇒ Object



10
11
12
13
14
# File 'lib/http_mimic/module_methods.rb', line 10

def headers(h = nil)
  return default_options[:headers] if h.nil?
  default_options[:headers] ||= {}
  default_options[:headers].merge!(h)
end

#impersonate(target = nil) ⇒ Object



28
29
30
31
# File 'lib/http_mimic/module_methods.rb', line 28

def impersonate(target = nil)
  return default_options[:impersonate] if target.nil?
  default_options[:impersonate] = target
end

#mode(m = nil) ⇒ Object



44
45
46
47
# File 'lib/http_mimic/module_methods.rb', line 44

def mode(m = nil)
  return default_options[:mode] if m.nil?
  default_options[:mode] = m
end

#options(url, options = {}) ⇒ Object



87
88
89
# File 'lib/http_mimic/module_methods.rb', line 87

def options(url, options = {})
  request(:options, url, options)
end

#patch(url, options = {}) ⇒ Object



75
76
77
# File 'lib/http_mimic/module_methods.rb', line 75

def patch(url, options = {})
  request(:patch, url, options)
end

#post(url, options = {}) ⇒ Object



67
68
69
# File 'lib/http_mimic/module_methods.rb', line 67

def post(url, options = {})
  request(:post, url, options)
end

#proxy(proxy_str = nil) ⇒ Object



33
34
35
36
# File 'lib/http_mimic/module_methods.rb', line 33

def proxy(proxy_str = nil)
  return default_options[:proxy] if proxy_str.nil?
  default_options[:proxy] = proxy_str
end

#put(url, options = {}) ⇒ Object



71
72
73
# File 'lib/http_mimic/module_methods.rb', line 71

def put(url, options = {})
  request(:put, url, options)
end

#request(method, url, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/http_mimic/module_methods.rb', line 91

def request(method, url, options = {})
  merged = default_options.merge(options)

  # Deep merge headers
  if default_options[:headers] || options[:headers]
    base_h = default_options[:headers] || {}
    opt_h = options[:headers] || {}
    merged[:headers] = base_h.merge(opt_h)
  end

  # Deep merge query
  base_q = default_options[:query] || {}
  opt_q = options[:query] || options[:params] || {}
  if !base_q.empty? || !opt_q.empty?
    merged[:query] = base_q.merge(opt_q)
  end

  # Deep merge cookies
  if default_options[:cookies].is_a?(Hash) && options[:cookies].is_a?(Hash)
    merged[:cookies] = default_options[:cookies].merge(options[:cookies])
  end

  Request.new(method, url, merged).perform
end

#retry_statuses(statuses = nil) ⇒ Object



54
55
56
57
# File 'lib/http_mimic/module_methods.rb', line 54

def retry_statuses(statuses = nil)
  return default_options[:retry_statuses] if statuses.nil?
  default_options[:retry_statuses] = statuses
end