Module: HttpMimic::ModuleMethods
- Included in:
- HttpMimic
- Defined in:
- lib/http_mimic/module_methods.rb
Instance Method Summary collapse
- #base_uri(uri = nil) ⇒ Object
- #cookies(c = nil) ⇒ Object
- #default_options ⇒ Object
- #default_params(params = nil) ⇒ Object (also: #default_query)
- #default_timeout(seconds = nil) ⇒ Object
- #delete(url, options = {}) ⇒ Object
- #get(url, options = {}) ⇒ Object
- #head(url, options = {}) ⇒ Object
- #headers(h = nil) ⇒ Object
- #impersonate(target = nil) ⇒ Object
- #options(url, options = {}) ⇒ Object
- #patch(url, options = {}) ⇒ Object
- #post(url, options = {}) ⇒ Object
- #proxy(proxy_str = nil) ⇒ Object
- #put(url, options = {}) ⇒ Object
- #request(method, url, options = {}) ⇒ Object
Instance Method Details
#base_uri(uri = nil) ⇒ Object
5 6 7 8 |
# File 'lib/http_mimic/module_methods.rb', line 5 def base_uri(uri = nil) return [:base_uri] if uri.nil? [:base_uri] = uri end |
#cookies(c = nil) ⇒ Object
38 39 40 41 42 |
# File 'lib/http_mimic/module_methods.rb', line 38 def (c = nil) return [:cookies] if c.nil? [:cookies] ||= {} [:cookies].merge!(c) end |
#default_options ⇒ Object
44 45 46 |
# File 'lib/http_mimic/module_methods.rb', line 44 def @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 [:query] if params.nil? [:query] ||= {} [: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 [:timeout] if seconds.nil? [:timeout] = seconds end |
#delete(url, options = {}) ⇒ Object
64 65 66 |
# File 'lib/http_mimic/module_methods.rb', line 64 def delete(url, = {}) request(:delete, url, ) end |
#get(url, options = {}) ⇒ Object
48 49 50 |
# File 'lib/http_mimic/module_methods.rb', line 48 def get(url, = {}) request(:get, url, ) end |
#head(url, options = {}) ⇒ Object
68 69 70 |
# File 'lib/http_mimic/module_methods.rb', line 68 def head(url, = {}) request(:head, url, ) end |
#headers(h = nil) ⇒ Object
10 11 12 13 14 |
# File 'lib/http_mimic/module_methods.rb', line 10 def headers(h = nil) return [:headers] if h.nil? [:headers] ||= {} [: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 [:impersonate] if target.nil? [:impersonate] = target end |
#options(url, options = {}) ⇒ Object
72 73 74 |
# File 'lib/http_mimic/module_methods.rb', line 72 def (url, = {}) request(:options, url, ) end |
#patch(url, options = {}) ⇒ Object
60 61 62 |
# File 'lib/http_mimic/module_methods.rb', line 60 def patch(url, = {}) request(:patch, url, ) end |
#post(url, options = {}) ⇒ Object
52 53 54 |
# File 'lib/http_mimic/module_methods.rb', line 52 def post(url, = {}) request(:post, url, ) 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 [:proxy] if proxy_str.nil? [:proxy] = proxy_str end |
#put(url, options = {}) ⇒ Object
56 57 58 |
# File 'lib/http_mimic/module_methods.rb', line 56 def put(url, = {}) request(:put, url, ) end |
#request(method, url, options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/http_mimic/module_methods.rb', line 76 def request(method, url, = {}) merged = .merge() # Deep merge headers if [:headers] || [:headers] base_h = [:headers] || {} opt_h = [:headers] || {} merged[:headers] = base_h.merge(opt_h) end # Deep merge query base_q = [:query] || {} opt_q = [:query] || [:params] || {} if !base_q.empty? || !opt_q.empty? merged[:query] = base_q.merge(opt_q) end # Deep merge cookies if [:cookies].is_a?(Hash) && [:cookies].is_a?(Hash) merged[:cookies] = [:cookies].merge([:cookies]) end Request.new(method, url, merged).perform end |