Class: Clutch::Client

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

Defined Under Namespace

Modules: NeverCapitalize

Constant Summary collapse

HEADER_KEYS =
%w[brand location terminal].map{ |key| key.extend(NeverCapitalize) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection = nil) ⇒ Client

Returns a new instance of Client.



24
25
26
# File 'lib/clutch/client.rb', line 24

def initialize(connection = nil)
  @connection = connection || build_connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



11
12
13
# File 'lib/clutch/client.rb', line 11

def connection
  @connection
end

Instance Method Details

#build_connectionObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/clutch/client.rb', line 28

def build_connection
  #opts = { ssl: { verify: false }, proxy: "http://localhost:8888" }
  opts = {}
  Faraday.new(api_base, opts) do |faraday|
    faraday.request :json

    faraday.response :mashify
    faraday.response :json
    faraday.adapter :net_http
  end.tap do |faraday|
    faraday.basic_auth api_key, api_secret
  end
end

#headersObject



20
21
22
# File 'lib/clutch/client.rb', line 20

def headers
  Hash[HEADER_KEYS.map{ |key| [key, send(key) ] }]
end

#post(path, hash) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/clutch/client.rb', line 42

def post(path, hash)
  response = connection.post(path, JSON.generate(hash)) do |conn|
    conn.headers.merge!(headers)
  end

  response.body
end