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
- #caching? ⇒ Boolean
- #clear_cache ⇒ Object
-
#connection(options = {}) ⇒ Object
Returns a Fraday::Connection object.
-
#default_middleware(options = {}) ⇒ Object
Default middleware stack that uses default adapter as specified at configuration stage.
- #default_options(options = {}) ⇒ Object
-
#stack(options = {}, &block) ⇒ Object
Exposes middleware builder to facilitate custom stacks and easy addition of new extensions such as cache adapter.
Instance Method Details
#caching? ⇒ Boolean
57 58 59 |
# File 'lib/nimbu-api/connection.rb', line 57 def caching? !@connection.nil? end |
#clear_cache ⇒ Object
53 54 55 |
# File 'lib/nimbu-api/connection.rb', line 53 def clear_cache @connection = nil end |
#connection(options = {}) ⇒ Object
Returns a Fraday::Connection object
76 77 78 79 80 81 82 |
# File 'lib/nimbu-api/connection.rb', line 76 def connection(={}) = ().keep_if {|k,_| ALLOWED_OPTIONS.include? k } clear_cache unless .empty? puts "OPTIONS:#{conn_options.inspect}" if ENV['DEBUG'] Faraday.new(.merge(:builder => stack())) end |
#default_middleware(options = {}) ⇒ Object
Default middleware stack that uses default adapter as specified at configuration stage.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nimbu-api/connection.rb', line 26 def default_middleware(={}) Proc.new do |builder| unless [: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 [:raw] builder.use Nimbu::Response::Mashify builder.use Nimbu::Response::Json end builder.adapter adapter end end |
#default_options(options = {}) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/nimbu-api/connection.rb', line 16 def (={}) { :ssl => .fetch(:ssl) { ssl }, :url => .fetch(:endpoint) { Nimbu.endpoint } }.merge() end |
#stack(options = {}, &block) ⇒ Object
Exposes middleware builder to facilitate custom stacks and easy addition of new extensions such as cache adapter.
64 65 66 67 68 69 70 71 72 |
# File 'lib/nimbu-api/connection.rb', line 64 def stack(={}, &block) @stack ||= begin if block_given? Faraday::RackBuilder.new(&block) else Faraday::RackBuilder.new(&default_middleware()) end end end |