Class: FunnelHttp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/funnel_http/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_request_header: {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • default_request_header (Hash{String => String, Array<String>}) (defaults to: {})


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_headerHash{String => String, Array<String>}

Returns:

  • (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

Parameters:

  • name (String)

    Header name

  • value (String, Array<String>)

    Header value

Returns:



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}>, ...

Overloads:

  • #normalize_requests(requests) ⇒ Array<Hash{Symbol => Object}>, ...

    Parameters:

    • requests (Array<Hash{Symbol => Object}>)

      Array of following Hash

    Options Hash (requests):

    • :method (String, Symbol)

      [required] Request method (e.g. :get, "POST")

    • :url (String)

      [required] Request url

    • :header (Hash{String => String, Array<String>}, nil)

      Request header

  • #normalize_requests(request) ⇒ Array<Hash{Symbol => Object}>, ...

    Parameters:

    • request (Hash{Symbol => Object})

    Options Hash (request):

    • :method (String, Symbol)

      [required] Request method (e.g. :get, "POST")

    • :url (String)

      [required] Request url

    • :header (Hash{String => String, Array<String>}, nil)

      Request header

Returns:

  • (Array<Hash{Symbol => Object}>)

    Array of following Hash

  • (String)

    :method Request method (e.g. "POST")

  • (String)

    :url Request url

  • (Hash{String => Array<String>})

    :header Request header



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

Overloads:

  • #perform(requests) ⇒ Array<Hash<Symbol => Object>>, ...

    Parameters:

    • requests (Array<Hash{Symbol => Object}>)

      Array of following Hash

    Options Hash (requests):

    • :method (String, Symbol)

      [required] Request method (e.g. :get, "POST")

    • :url (String)

      [required] Request url

    • :header (Hash{String => String, Array<String>}, nil)

      Request header

  • #perform(request) ⇒ Array<Hash<Symbol => Object>>, ...

    Parameters:

    • request (Hash{Symbol => Object})

    Options Hash (request):

    • :method (String, Symbol)

      [required] Request method (e.g. :get, "POST")

    • :url (String)

      [required] Request url

    • :header (Hash{String => String, Array<String>}, nil)

      Request header

Returns:

  • (Array<Hash<Symbol => Object>>)

    Array of following Hash

  • (Integer)

    :status_code

  • (String)

    :body Response body

  • (Hash{String => Array<String>})

    :header Response header



41
42
43
# File 'lib/funnel_http/client.rb', line 41

def perform(requests)
  ext_client.run_requests(normalize_requests(requests))
end