Class: ResponseMate::Connection
- Inherits:
-
Object
- Object
- ResponseMate::Connection
- Defined in:
- lib/response_mate/connection.rb
Overview
This class provides a layer above the HTTP client
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
- #fetch(request) ⇒ Object
- #headers_from_manifest(manifest) ⇒ Object
-
#initialize(base_url = nil) ⇒ Connection
constructor
A new instance of Connection.
Constructor Details
#initialize(base_url = nil) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/response_mate/connection.rb', line 11 def initialize(base_url = nil) @base_url = base_url @client = Faraday.new do |c| c.use FaradayMiddleware::FollowRedirects, limit: 5 c.adapter Faraday.default_adapter end client.headers.merge!(ResponseMate::DEFAULT_HEADERS) client.url_prefix = base_url if base_url end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/response_mate/connection.rb', line 9 def base_url @base_url end |
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/response_mate/connection.rb', line 9 def client @client end |
Instance Method Details
#fetch(request) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/response_mate/connection.rb', line 22 def fetch(request) client.params = request[:params] if !request[:params].nil? unless base_url || request[:path] =~ %r{http://} request[:path] = 'http://' + request[:path] end verb, path = [request[:verb].downcase.to_sym, "#{base_url}#{request[:path]}"] client.send verb, path do |req| req.headers = headers_from_manifest(manifest) if request[:data_mode] == 'raw' req.headers['Content-Type'] = 'text/plain; charset=UTF-8' req.body = request[:data] elsif request[:data_mode] == 'urlencoded' req.headers['Content-Type'] = 'application/x-www-form-urlencoded' req.headers['Content-Type'] = 'text/plain; charset=UTF-8' end end rescue Faraday::Error::ConnectionFailed puts "Is a server up and running at #{request[:path]}?".red exit 1 end |
#headers_from_manifest(manifest) ⇒ Object
46 47 48 49 50 |
# File 'lib/response_mate/connection.rb', line 46 def headers_from_manifest(manifest) manifest.default_headers.inject({}) do |hash, (k, v)| hash[ResponseMate::Helpers.headerize(k)] = v end end |