Class: IronAdmin::Adapters::Http::Connection
- Inherits:
-
Object
- Object
- IronAdmin::Adapters::Http::Connection
- Defined in:
- lib/iron_admin/adapters/http/connection.rb
Overview
Wraps Faraday for HTTP communication with REST APIs.
Handles request building, response parsing, auth headers, and error mapping to IronAdmin exceptions.
Instance Method Summary collapse
- #delete(id) ⇒ Object
- #get(params: {}) ⇒ Object
- #get_one(id) ⇒ Object
-
#initialize(config) ⇒ Connection
constructor
A new instance of Connection.
- #patch(id, attrs) ⇒ Object
- #post(attrs) ⇒ Object
Constructor Details
#initialize(config) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 |
# File 'lib/iron_admin/adapters/http/connection.rb', line 11 def initialize(config) @config = config @base_url = config.full_url end |
Instance Method Details
#delete(id) ⇒ Object
50 51 52 53 |
# File 'lib/iron_admin/adapters/http/connection.rb', line 50 def delete(id) response = safe_request { faraday.delete("#{@base_url}/#{id}") } handle_errors(response) end |
#get(params: {}) ⇒ Object
16 17 18 19 20 |
# File 'lib/iron_admin/adapters/http/connection.rb', line 16 def get(params: {}) response = safe_request { faraday.get(@base_url, params) } handle_errors(response) parse_collection(response) end |
#get_one(id) ⇒ Object
22 23 24 25 26 |
# File 'lib/iron_admin/adapters/http/connection.rb', line 22 def get_one(id) response = safe_request { faraday.get("#{@base_url}/#{id}") } handle_errors(response) parse_json(response.body) end |
#patch(id, attrs) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/iron_admin/adapters/http/connection.rb', line 39 def patch(id, attrs) response = safe_request do faraday.patch("#{@base_url}/#{id}") do |req| req.headers["Content-Type"] = "application/json" req.body = attrs.to_json end end handle_errors(response) parse_json(response.body) end |
#post(attrs) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/iron_admin/adapters/http/connection.rb', line 28 def post(attrs) response = safe_request do faraday.post(@base_url) do |req| req.headers["Content-Type"] = "application/json" req.body = attrs.to_json end end handle_errors(response) parse_json(response.body) end |