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 =
{}
GETISH_ACTIONS =
%i(get delete)
POSTISH_ACTIONS =
%i(post put)
HTTP_ACTIONS =
GETISH_ACTIONS + POSTISH_ACTIONS
CODE_ERRORS =
{
  400 => Errors::ClientError,
  401 => Errors::UnauthorizedError,
  404 => Errors::NotFoundError,
  409 => Errors::ConflictError,
  500 => Errors::ServerError
}

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
36
37
38
# File 'lib/aptly/connection.rb', line 25

def initialize(**kwords)
  @query = kwords.fetch(:query, DEFAULT_QUERY)

  uri = URI.parse('')
  uri.scheme = 'http'
  uri.host = ::Aptly.configuration.host
  uri.port = ::Aptly.configuration.port

  @connection = Faraday.new(url: uri.to_s) do |c|
    c.request :multipart
    c.request :url_encoded
    c.adapter :net_http
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



40
41
42
43
44
45
46
47
48
# File 'lib/aptly/connection.rb', line 40

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