Module: Nimbu::Connection

Extended by:
Connection
Includes:
Utils::Constants
Included in:
Connection, Endpoint
Defined in:
lib/nimbu-api/connection.rb

Constant Summary collapse

ALLOWED_OPTIONS =
[
  :headers,
  :url,
  :params,
  :request,
  :ssl,
].freeze

Constants included from Utils::Constants

Utils::Constants::ACCEPT, Utils::Constants::ACCEPTED_OAUTH_SCOPES, Utils::Constants::ACCEPT_CHARSET, Utils::Constants::CACHE_CONTROL, Utils::Constants::CONTENT_LENGTH, Utils::Constants::CONTENT_TYPE, Utils::Constants::DATE, Utils::Constants::ETAG, Utils::Constants::HEADER_LAST, Utils::Constants::HEADER_LINK, Utils::Constants::HEADER_NEXT, Utils::Constants::LOCATION, Utils::Constants::META_FIRST, Utils::Constants::META_LAST, Utils::Constants::META_NEXT, Utils::Constants::META_PREV, Utils::Constants::META_REL, Utils::Constants::NIMBU_SITE, Utils::Constants::OAUTH_SCOPES, Utils::Constants::PARAM_PAGE, Utils::Constants::PARAM_PER_PAGE, Utils::Constants::PARAM_START_PAGE, Utils::Constants::RATELIMIT_LIMIT, Utils::Constants::RATELIMIT_REMAINING, Utils::Constants::SERVER, Utils::Constants::USER_AGENT

Instance Method Summary collapse

Instance Method Details

#caching?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/nimbu-api/connection.rb', line 58

def caching?
  !@connection.nil?
end

#clear_cacheObject



54
55
56
# File 'lib/nimbu-api/connection.rb', line 54

def clear_cache
  @connection = nil
end

#connection(options = {}) ⇒ Object

Returns a Fraday::Connection object



75
76
77
78
79
80
81
# File 'lib/nimbu-api/connection.rb', line 75

def connection(options = {})
  conn_options = default_options(options).keep_if { |k, _| ALLOWED_OPTIONS.include?(k) }
  clear_cache unless options.empty?
  puts "OPTIONS:#{conn_options.inspect}" if ENV["DEBUG"]

  Faraday.new(conn_options.merge(builder: stack(options)))
end

#default_middleware(options = {}) ⇒ Object

Default middleware stack that uses default adapter as specified at configuration stage.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nimbu-api/connection.rb', line 27

def default_middleware(options = {})
  proc do |builder|
    unless options[:with_attachments]
      builder.use(Nimbu::Request::Json)
    end
    builder.use(Faraday::Request::Multipart)
    builder.use(Faraday::Request::UrlEncoded)
    builder.use(Nimbu::Request::OAuth2, oauth_token) if oauth_token?
    builder.use(Nimbu::Request::BasicAuth, authentication) if basic_authed?
    builder.use(Nimbu::Request::UserAgent)
    builder.use(Nimbu::Request::SiteHeader, subdomain)
    builder.use(Nimbu::Request::ContentLocale, content_locale)

    builder.use(Faraday::Response::Logger) if ENV["DEBUG"]
    builder.use(Nimbu::Response::RaiseError)
    unless options[:raw]
      builder.use(Nimbu::Response::Mashify)
      builder.use(Nimbu::Response::Json)
    end
    builder.adapter(adapter)
  end
end

#default_options(options = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/nimbu-api/connection.rb', line 17

def default_options(options = {})
  {
    ssl: options.fetch(:ssl) { ssl },
    url: options.fetch(:endpoint) { Nimbu.endpoint },
  }.merge(options)
end

#stack(options = {}, &block) ⇒ Object

Exposes middleware builder to facilitate custom stacks and easy addition of new extensions such as cache adapter.



65
66
67
68
69
70
71
# File 'lib/nimbu-api/connection.rb', line 65

def stack(options = {}, &block)
  @stack ||= if block_given?
    Faraday::RackBuilder.new(&block)
  else
    Faraday::RackBuilder.new(&default_middleware(options))
  end
end