Class: ResponseMate::Connection

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

Overview

This class provides a layer above the HTTP client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection


8
9
10
11
12
13
# File 'lib/response_mate/connection.rb', line 8

def initialize
  @client = Faraday.new do |c|
    c.use FaradayMiddleware::FollowRedirects, limit: 5
    c.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client


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

def client
  @client
end

Instance Method Details

#fetch(request) ⇒ Object

Performs the supplied request

Parameters:


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/response_mate/connection.rb', line 17

def fetch(request)
  uri = URI.parse(request[:url])

  if request[:params]
    query = request[:params].to_query
    query = uri.query ? uri.query << '&' << query : query
    uri.query = query
  end

  verb = request[:verb] || 'GET'

  client.headers = request[:headers] if request[:headers]
  client.send verb.downcase.to_sym, uri.to_s
rescue Faraday::Error::ConnectionFailed
  puts "Is a server up and running at #{request[:path]}?".red
  exit 1
end