Class: CrAPI::Proxy
- Inherits:
-
Object
- Object
- CrAPI::Proxy
- Defined in:
- lib/crapi/proxy.rb
Overview
Proxies simple CRUD methods (#delete / #get / #patch / #post / #put) for a CrAPI::Client or another CrAPI::Proxy. Like CrAPI::Client, it also provides a proxy generator.
A CrAPI::Proxy has its own set of default headers and has a segment that is prepended to its CRUD methods’ path before being passed to the parent CrAPI::Client/CrAPI::Proxy. This makes the proxxy functionally equivalent to a new CrAPI::Client with a new base path (the parent’s base path plus the proxy’s segment) and more default headers, but without the need for a separate connection to the target system.
Instance Attribute Summary collapse
-
#default_headers ⇒ Object
A Hash containing headers to send with every request (unless overriden elsewhere).
Instance Method Summary collapse
-
#delete(path, headers: {}, query: {}) ⇒ Object
CRUD method: DELETE.
-
#get(path, headers: {}, query: {}) ⇒ Object
CRUD method: GET.
-
#initialize(add:, to:, headers: nil) ⇒ Proxy
constructor
A new instance of Proxy.
-
#new_proxy(segment = '/', headers: nil) ⇒ CrAPI::Proxy
Returns a new CrAPI::Proxy for this proxy.
-
#patch(path, headers: {}, query: {}, payload: {}) ⇒ Object
CRUD method: PATCH.
-
#post(path, headers: {}, query: {}, payload: {}) ⇒ Object
CRUD method: POST.
-
#put(path, headers: {}, query: {}, payload: {}) ⇒ Object
CRUD method: PUT.
Constructor Details
#initialize(add:, to:, headers: nil) ⇒ Proxy
Returns a new instance of Proxy.
37 38 39 40 41 |
# File 'lib/crapi/proxy.rb', line 37 def initialize(add:, to:, headers: nil) @parent = to @segment = add @default_headers = (headers || {}).with_indifferent_access end |
Instance Attribute Details
#default_headers ⇒ Object
A Hash containing headers to send with every request (unless overriden elsewhere). In case of conflicts, headers set as default for a CrAPI::Proxy override those set by the parent. As in a CrAPI::Client, headers set by the user via the CRUD methods’ headers still override any conflicting default header.
24 25 26 |
# File 'lib/crapi/proxy.rb', line 24 def default_headers @default_headers end |
Instance Method Details
#delete(path, headers: {}, query: {}) ⇒ Object
CRUD method: DELETE
70 71 72 73 |
# File 'lib/crapi/proxy.rb', line 70 def delete(path, headers: {}, query: {}) @parent.delete("/#{@segment}/#{path}".gsub(%r{/+}, '/'), headers: @default_headers.merge(headers), query: query) end |
#get(path, headers: {}, query: {}) ⇒ Object
CRUD method: GET
82 83 84 85 |
# File 'lib/crapi/proxy.rb', line 82 def get(path, headers: {}, query: {}) @parent.get("/#{@segment}/#{path}".gsub(%r{/+}, '/'), headers: @default_headers.merge(headers), query: query) end |
#new_proxy(segment = '/', headers: nil) ⇒ CrAPI::Proxy
Returns a new CrAPI::Proxy for this proxy.
57 58 59 |
# File 'lib/crapi/proxy.rb', line 57 def new_proxy(segment = '/', headers: nil) Proxy.new(add: segment, to: self, headers: headers) end |
#patch(path, headers: {}, query: {}, payload: {}) ⇒ Object
CRUD method: PATCH
94 95 96 97 |
# File 'lib/crapi/proxy.rb', line 94 def patch(path, headers: {}, query: {}, payload: {}) @parent.patch("/#{@segment}/#{path}".gsub(%r{/+}, '/'), headers: @default_headers.merge(headers), query: query, payload: payload) end |
#post(path, headers: {}, query: {}, payload: {}) ⇒ Object
CRUD method: POST
106 107 108 109 |
# File 'lib/crapi/proxy.rb', line 106 def post(path, headers: {}, query: {}, payload: {}) @parent.post("/#{@segment}/#{path}".gsub(%r{/+}, '/'), headers: @default_headers.merge(headers), query: query, payload: payload) end |
#put(path, headers: {}, query: {}, payload: {}) ⇒ Object
CRUD method: PUT
118 119 120 121 |
# File 'lib/crapi/proxy.rb', line 118 def put(path, headers: {}, query: {}, payload: {}) @parent.put("/#{@segment}/#{path}".gsub(%r{/+}, '/'), headers: @default_headers.merge(headers), query: query, payload: payload) end |