Class: LoogiHttp::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/loogi_http/connection.rb

Overview

HTTP connection over a configured ‘Faraday::Connection` (aka a stack). Methods supported are:

#get #post

Instance Method Summary collapse

Constructor Details

#initialize(faraday_connection) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • faraday_connection (Faraday::Connection)

    The configured ‘Faraday::Connection`



13
14
15
# File 'lib/loogi_http/connection.rb', line 13

def initialize(faraday_connection)
  @faraday_connection = faraday_connection
end

Instance Method Details

#basic_auth(username:, password:) ⇒ void

This method returns an undefined value.

Basic Authentication

Parameters:

  • username (String)
  • password (String)


82
83
84
# File 'lib/loogi_http/connection.rb', line 82

def basic_auth(username:, password:)
  faraday_connection.basic_auth(username, password)
end

#get(url, params: {}) ⇒ LoogiHttp::Response

HTTP GET request.

Parameters:

  • url (String)

    URL

  • params (Hash) (defaults to: {})

    Query params to be sent, defaults to ‘{}`

Returns:



22
23
24
25
26
27
28
# File 'lib/loogi_http/connection.rb', line 22

def get(url, params: {})
  response faraday_connection.get(url, params)
rescue Faraday::TimeoutError => e
  raise LoogiHttp::TimeoutError, e
rescue Faraday::ConnectionFailed => e
  raise LoogiHttp::ConnectionFailed, e
end

#post(url, params: {}, data: nil, options: {}, headers: {}) ⇒ LoogiHttp::Response

HTTP POST request.

Parameters:

  • url (String)

    URL

  • params (Hash) (defaults to: {})

    Query params to be sent, defaults to ‘{}`

  • data (Object) (defaults to: nil)

    POST body that will eventually be converted into a ‘String`, defaults to `nil`

  • options (Hash) (defaults to: {})

    Faraday options, defaults to ‘{}`

  • headers (Hash) (defaults to: {})

    Headers for the POST, defaults to ‘{}`

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/loogi_http/connection.rb', line 39

def post(url, params: {}, data: nil, options: {}, headers: {})
  response(faraday_connection.post(url, data, headers) do |request|
    request.params.update params if params
    if (debug = options.delete(:debug))
      options[:context] ||= {}
      options[:context][:debug] = debug
    end
    request.options.update options if options
  end)
rescue Faraday::TimeoutError => e
  raise LoogiHttp::TimeoutError, e
rescue Faraday::ConnectionFailed => e
  raise LoogiHttp::ConnectionFailed, e
end

#put(url, params: {}, data: nil, options: {}, headers: {}) ⇒ LoogiHttp::Response

HTTP PUT request.

Parameters:

  • url (String)

    URL

  • params (Hash) (defaults to: {})

    Query params to be sent, defaults to ‘{}`

  • data (Object) (defaults to: nil)

    PUT body that will eventually be converted into a ‘String`, defaults to `nil`

  • options (Hash) (defaults to: {})

    Faraday options, defaults to ‘{}`

  • headers (Hash) (defaults to: {})

    Headers for the PUT, defaults to ‘{}`

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/loogi_http/connection.rb', line 63

def put(url, params: {}, data: nil, options: {}, headers: {})
  response(faraday_connection.put(url, data, headers) do |request|
    request.params.update params if params
    if (debug = options.delete(:debug))
      options[:context] ||= {}
      options[:context][:debug] = debug
    end
    request.options.update options if options
  end)
rescue Faraday::TimeoutError => e
  raise LoogiHttp::TimeoutError, e
end