Class: ApiTester::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options = {}) ⇒ Connection

Returns a new instance of Connection.



7
8
9
# File 'lib/api_tester/connection.rb', line 7

def initialize(endpoint, options = {})
  @endpoint = endpoint
end

Instance Method Details

#connectionObject



11
12
13
14
15
16
# File 'lib/api_tester/connection.rb', line 11

def connection
  @conn ||= Faraday.new(:url => @endpoint) do |faraday|
    faraday.request  :url_encoded
    faraday.adapter  Faraday.default_adapter
  end
end

#headersObject



18
19
20
# File 'lib/api_tester/connection.rb', line 18

def headers
  'application/json'
end

#post(api, body) ⇒ Object



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

def post(api, body)
  resp = connection.post do |req|
    req.url api
    req.options.timeout = 3600
    req.options.open_timeout = 3600
    req.headers['Content-Type'] = headers
    req.body = body
  end
end

#pretty_print(resp) ⇒ Object



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

def pretty_print(resp)
  begin
    puts JSON.pretty_generate(JSON.parse(resp.body))
  rescue
    puts resp.body
  end
end