Class: Munson::Connection
- Inherits:
-
Object
- Object
- Munson::Connection
- Defined in:
- lib/munson/connection.rb
Overview
Faraday::Connection wrapper for making JSON API Requests
Constant Summary collapse
- CONNECTION_OPTIONS =
[:response_key_format].freeze
- FARADAY_OPTIONS =
[:request, :proxy, :ssl, :builder, :url, :parallel_manager, :params, :headers, :builder_class].freeze
Instance Attribute Summary collapse
- #faraday ⇒ Object readonly
- #options ⇒ Object readonly
Instance Method Summary collapse
-
#clone ⇒ Object
Clones a connection.
-
#configure(args = {}, &block) ⇒ Object
Configure the connection.
-
#get(path: nil, params: nil, headers: nil) ⇒ Faraday::Response
JSON API Spec GET request.
-
#initialize(args, &block) ⇒ Connection
constructor
Create a new connection.
-
#post(body: {}, path: nil, headers: nil, http_method: :post) ⇒ Faraday::Response
JSON API Spec POST request.
- #response_key_format ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(args, &block) ⇒ Connection
Create a new connection. A connection serves as a thin wrapper around a a faraday connection that includes two pieces of middleware for handling JSON API Spec
40 41 42 |
# File 'lib/munson/connection.rb', line 40 def initialize(args, &block) configure(args, &block) end |
Instance Attribute Details
#faraday ⇒ Object (readonly)
6 7 8 |
# File 'lib/munson/connection.rb', line 6 def faraday @faraday end |
#options ⇒ Object (readonly)
6 7 8 |
# File 'lib/munson/connection.rb', line 6 def @options end |
Instance Method Details
#clone ⇒ Object
Clones a connection
45 46 47 |
# File 'lib/munson/connection.rb', line 45 def clone Connection.new(.dup, &@block) end |
#configure(args = {}, &block) ⇒ Object
Configure the connection
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/munson/connection.rb', line 99 def configure(args={}, &block) # Cache these for #clone method @options = args @block = block = @options.reject { |key, value| !FARADAY_OPTIONS.include?(key.to_sym) } @faraday = Faraday.new() do |conn| yield conn if block_given? conn.request :"Munson::Middleware::EncodeJsonApi", key_formatter conn.response :"Munson::Middleware::JsonParser", key_formatter conn.adapter Faraday.default_adapter end end |
#get(path: nil, params: nil, headers: nil) ⇒ Faraday::Response
JSON API Spec GET request
58 59 60 61 62 63 |
# File 'lib/munson/connection.rb', line 58 def get(path: nil, params: nil, headers: nil) faraday.get do |request| request.headers.merge!(headers) if headers request.url path.to_s, externalize_keys(params) end end |
#post(body: {}, path: nil, headers: nil, http_method: :post) ⇒ Faraday::Response
JSON API Spec POST request
72 73 74 75 76 77 78 |
# File 'lib/munson/connection.rb', line 72 def post(body: {}, path: nil, headers: nil, http_method: :post) faraday.send(http_method) do |request| request.headers.merge!(headers) if headers request.url path.to_s request.body = body end end |
#response_key_format ⇒ Object
130 131 132 |
# File 'lib/munson/connection.rb', line 130 def response_key_format @options[:response_key_format] end |
#url ⇒ Object
126 127 128 |
# File 'lib/munson/connection.rb', line 126 def url faraday.url_prefix.to_s end |