Module: Winker::Connection

Included in:
Winker
Defined in:
lib/winker/connection.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/winker/connection.rb', line 3

def connection
  @connection ||= Faraday.new(Winker.endpoint) do |conn|
    conn.options[:timeout]      = 5
    conn.options[:open_timeout] = 2

    conn.request  :json
    conn.response :json, :content_type => /\bjson$/

    conn.authorization :bearer, Winker.access_token if Winker.access_token
    conn.response :detailed_logger, Winker.logger if Winker.logger

    conn.adapter Faraday.default_adapter # make requests with Net::HTTP
  end
end

#get(path, body = {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/winker/connection.rb', line 18

def get(path, body = {})
  body = MultiJson.dump(body) if Hash === body
  connection.get(path) do |req|
    req.body = body if body
  end
end

#post(path, body = {}) ⇒ Object



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

def post(path, body = {})
  body = MultiJson.dump(body) if Hash === body
  connection.post(path) do |req|
    req.body = body if body
  end
end

#put(path, body = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/winker/connection.rb', line 32

def put(path, body = {})
  body = MultiJson.dump(body) if Hash === body
  connection.put(path) do |req|
    req.body = body if body
  end
end