Class: Aptly::Connection

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

Overview

Connection adaptor. This class wraps HTTP interactions for our purposes and adds general purpose automation on top of the raw HTTP actions.

Constant Summary collapse

DEFAULT_QUERY =
{}.freeze
GETISH_ACTIONS =
%i(get delete).freeze
POSTISH_ACTIONS =
%i(post put).freeze
HTTP_ACTIONS =
GETISH_ACTIONS + POSTISH_ACTIONS
CODE_ERRORS =
{
  400 => Errors::ClientError,
  401 => Errors::UnauthorizedError,
  404 => Errors::NotFoundError,
  409 => Errors::ConflictError,
  500 => Errors::ServerError
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(**kwords) ⇒ Connection

Returns a new instance of Connection.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aptly/connection.rb', line 25

def initialize(**kwords)
  @query = kwords.fetch(:query, DEFAULT_QUERY)
  @base_uri = kwords.delete(:uri) { ::Aptly.configuration.uri.clone }

  raise if uri.nil?
  @connection = Faraday.new(uri) do |c|
    c.request :multipart
    c.request :url_encoded
    c.adapter :excon, @adapter_options
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, **kwords) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/aptly/connection.rb', line 37

def method_missing(symbol, *args, **kwords)
  return super(symbol, *args, kwords) unless HTTP_ACTIONS.include?(symbol)

  kwords[:query] = build_query(kwords)
  kwords.delete(:query) if kwords[:query].empty?

  relative_path = args.shift
  http_call(symbol, add_api(relative_path), kwords)
end