Class: Typeform::Connection

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

Constant Summary collapse

BASE_URI =
"https://api.typeform.io"
TYPEFORM_API_VERSION =
"v0.4"

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
11
12
13
14
# File 'lib/typeform/connection.rb', line 6

def initialize(api_key)
  @api_key = api_key
  @conn = Faraday.new(url: BASE_URI) do |faraday|
    faraday.request :json
    faraday.response :logger
    faraday.adapter  Faraday.default_adapter
    faraday.use FaradayMiddleware::ParseJson, content_type: /\bjson$/
  end
end

Instance Method Details

#get(path) ⇒ Object



21
22
23
24
25
26
# File 'lib/typeform/connection.rb', line 21

def get(path)
  @conn.get do |request|
    set_base_headers(request)
    request.url "/#{TYPEFORM_API_VERSION}#{path}"
  end
end

#post(path, body) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/typeform/connection.rb', line 28

def post(path, body)
  @conn.post do |request|
    set_base_headers(request)
    request.url "/#{TYPEFORM_API_VERSION}#{path}"
    request.body = body
  end
end

#set_base_headers(request) ⇒ Object



16
17
18
19
# File 'lib/typeform/connection.rb', line 16

def set_base_headers(request)
  request.headers["X-API-TOKEN"] = @api_key
  request.headers["Accept-Encoding"] = "gzip, deflate"
end