Class: Leif::Connection

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

Defined Under Namespace

Classes: Exchange

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Connection

Returns a new instance of Connection.



8
9
10
# File 'lib/leif/connection.rb', line 8

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/leif/connection.rb', line 6

def connection
  @connection
end

Class Method Details

.default_connection(url, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/leif/connection.rb', line 23

def self.default_connection(url, options = {})
  ssl_verify = options.fetch(:ssl_verify, true)
  Faraday.new(url: url, ssl: { verify: ssl_verify }) do |config|
    config.request  :url_encoded
    config.response :json, :content_type => /\bjson$/
    config.adapter  Faraday.default_adapter
  end
end

.to_url(url, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/leif/connection.rb', line 12

def self.to_url(url, options = {})
  connection = default_connection(url, options)
  if options.has_key?(:username) and options.has_key?(:password)
    connection.basic_auth options.fetch(:username),
                          options.fetch(:password)
  elsif options.has_key?(:token)
    connection.token_auth options.fetch(:token)
  end
  new(connection)
end

Instance Method Details

#request(path, data = {}, method = :unset) ⇒ Object



32
33
34
35
# File 'lib/leif/connection.rb', line 32

def request(path, data = {}, method = :unset)
  method = data.empty? ? :get : :post if method == :unset
  Exchange.new(connection.send(method, path, data))
end