Class: Farscape::Agent::HTTPClient

Inherits:
BaseClient show all
Defined in:
lib/farscape/client/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseClient

#idempotent_method?, #safe_method?, #unsafe_method?

Constructor Details

#initialize(agent) ⇒ HTTPClient

Returns a new instance of HTTPClient.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/farscape/client/http_client.rb', line 14

def initialize(agent)
  @connection = Faraday.new do |builder|
    builder.request :url_encoded
    agent.middleware_stack.each do |middleware|
      if middleware.key?(:config)
        config = middleware[:config]
        if config.is_a?(Array)
          builder.use(middleware[:class], *config)
        else
          builder.use(middleware[:class], config)
        end
      else
        builder.use(middleware[:class])
      end
    end

    builder.adapter faraday_adapter
  end
end

Instance Attribute Details

#connectionObject (readonly)

The Faraday connection instance.



11
12
13
# File 'lib/farscape/client/http_client.rb', line 11

def connection
  @connection
end

#PluginsObject (readonly)

Returns the value of attribute Plugins.



12
13
14
# File 'lib/farscape/client/http_client.rb', line 12

def Plugins
  @Plugins
end

Instance Method Details

#dispatch_error(response) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/farscape/client/http_client.rb', line 65

def dispatch_error(response)
  errors = Farscape::Exceptions
  http_code = {
    400 => errors::BadRequest,
    401 => errors::Unauthorized,
    403 => errors::Forbidden,
    404 => errors::NotFound,
    405 => errors::MethodNotAllowed,
    406 => errors::NotAcceptable,
    407 => errors::ProxyAuthenticationRequired,
    408 => errors::RequestTimeout,
    409 => errors::Conflict,
    410 => errors::Gone,
    411 => errors::LengthRequired,
    412 => errors::PreconditionFailed,
    413 => errors::RequestEntityTooLarge,
    414 => errors::RequestUriTooLong,
    415 => errors::UnsupportedMediaType,
    416 => errors::RequestedRangeNotSatisfiable,
    417 => errors::ExpectationFailed,
    418 => errors::ImaTeapot,
    422 => errors::UnprocessableEntity,
    500 => errors::InternalServerError,
    501 => errors::NotImplemented,
    502 => errors::BadGateway,
    503 => errors::ServiceUnavailable,
    504 => errors::GatewayTimeout,
    505 => errors::ProtocolVersionNotSupported,
  }
  http_code[response.status] || errors::ProtocolException unless response.success?
end

#faraday_adapterObject

Override this in a subclass to create clients with custom Faraday adapters



35
36
37
# File 'lib/farscape/client/http_client.rb', line 35

def faraday_adapter
  Faraday.default_adapter
end

#interface_methodsObject



57
58
59
60
61
62
63
# File 'lib/farscape/client/http_client.rb', line 57

def interface_methods
  {
    idempotent: ['PUT', 'DELETE'],
    unsafe: ['POST', 'PATCH'], # http://tools.ietf.org/html/rfc5789
    safe: ['GET', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']
  }
end

#invoke(options = {}) ⇒ Faraday::Response

Makes a Faraday request given the specified options

Returns:

  • (Faraday::Response)

    The response object resulting from the Faraday call



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/farscape/client/http_client.rb', line 45

def invoke(options = {})
  defaults = { method:  'get'}
  options = defaults.merge(options)

  connection.send(options[:method].to_s.downcase) do |req|
    req.url options[:url]
    req.body = options[:body] if options.has_key?(:body)
    options[:params].each { |k,v| req.params[k] = v } if options.has_key?(:params)
    options[:headers].each { |k,v| req.headers[k] = v } if options.has_key?(:headers)
  end
end