Class: JsonApiClient::Connection
- Inherits:
-
Object
- Object
- JsonApiClient::Connection
- Defined in:
- lib/json_api_client/connection.rb
Instance Attribute Summary collapse
-
#faraday ⇒ Object
readonly
Returns the value of attribute faraday.
Instance Method Summary collapse
- #delete(middleware) ⇒ Object
-
#initialize(options = {}) {|_self| ... } ⇒ Connection
constructor
A new instance of Connection.
- #run(request_method, path, params: nil, headers: {}, body: nil) ⇒ Object
-
#use(middleware, *args, &block) ⇒ Object
insert middleware before ParseJson - middleware executed in reverse order - inserted middleware will run after json parsed.
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Connection
Returns a new instance of Connection.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/json_api_client/connection.rb', line 6 def initialize( = {}) site = .fetch(:site) = .slice(:proxy, :ssl, :request, :headers, :params) = Array(.fetch(:adapter, Faraday.default_adapter)) = {} [:custom_handlers] = [:status_handlers] if [:status_handlers].present? @faraday = Faraday.new(site, ) do |builder| builder.request :json builder.use Middleware::JsonRequest builder.use Middleware::Status, builder.use Middleware::ParseJson builder.use ::FaradayMiddleware::Gzip builder.adapter(*) end yield(self) if block_given? end |
Instance Attribute Details
#faraday ⇒ Object (readonly)
Returns the value of attribute faraday.
4 5 6 |
# File 'lib/json_api_client/connection.rb', line 4 def faraday @faraday end |
Instance Method Details
#delete(middleware) ⇒ Object
30 31 32 |
# File 'lib/json_api_client/connection.rb', line 30 def delete(middleware) faraday.builder.delete(middleware) end |
#run(request_method, path, params: nil, headers: {}, body: nil) ⇒ Object
34 35 36 37 38 |
# File 'lib/json_api_client/connection.rb', line 34 def run(request_method, path, params: nil, headers: {}, body: nil) faraday.run_request(request_method, path, body, headers) do |request| request.params.update(params) if params end end |
#use(middleware, *args, &block) ⇒ Object
insert middleware before ParseJson - middleware executed in reverse order -
inserted middleware will run after json parsed
25 26 27 28 |
# File 'lib/json_api_client/connection.rb', line 25 def use(middleware, *args, &block) return if faraday.builder.locked? faraday.builder.insert_before(Middleware::ParseJson, middleware, *args, &block) end |