Class: FunnelHttp::Client
- Inherits:
-
Object
- Object
- FunnelHttp::Client
- Defined in:
- lib/funnel_http/client.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#add_default_request_header(name, value) ⇒ Hash{String => String, Array<String>}
Add header to #default_request_header.
-
#initialize(default_request_header: {}) ⇒ Client
constructor
A new instance of Client.
- #normalize_requests(arg) ⇒ Array<Hash{Symbol => Object}>, ...
-
#perform(requests) ⇒ Array<Hash<Symbol => Object>>, ...
perform HTTP requests in parallel.
Constructor Details
#initialize(default_request_header: {}) ⇒ Client
Returns a new instance of Client.
10 11 12 |
# File 'lib/funnel_http/client.rb', line 10 def initialize(default_request_header: {}) @default_request_header = {"User-Agent" => USER_AGENT}.merge(default_request_header) end |
Instance Attribute Details
#default_request_header ⇒ Hash{String => String, Array<String>}
7 8 9 |
# File 'lib/funnel_http/client.rb', line 7 def default_request_header @default_request_header end |
Instance Method Details
#add_default_request_header(name, value) ⇒ Hash{String => String, Array<String>}
Add header to #default_request_header
19 20 21 |
# File 'lib/funnel_http/client.rb', line 19 def add_default_request_header(name, value) default_request_header.merge!(name => value) end |
#normalize_requests(requests) ⇒ Array<Hash{Symbol => Object}>, ... #normalize_requests(request) ⇒ Array<Hash{Symbol => Object}>, ...
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/funnel_http/client.rb', line 61 def normalize_requests(arg) requests = case arg when Array arg when Hash [arg] else raise ArgumentError, "#{arg} must be Array or Hash" end requests.map do |request| raise ArgumentError, "#{arg} contains something other than Hash" unless request.is_a?(Hash) raise ArgumentError, "#{arg} key does not contain all :method and :url" if !request.key?(:method) || !request.key?(:url) { url: request[:url].to_s, method: request[:method].to_s.upcase, header: normalize_header(request[:header]), } end end |
#perform(requests) ⇒ Array<Hash<Symbol => Object>>, ... #perform(request) ⇒ Array<Hash<Symbol => Object>>, ...
perform HTTP requests in parallel
41 42 43 |
# File 'lib/funnel_http/client.rb', line 41 def perform(requests) ext_client.run_requests(normalize_requests(requests)) end |