Class: CrAPI::Proxy

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(add:, to:, headers: nil) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • add (String)

    The new base path. This path (added to the parent's base path) will be used as the base path for the CrAPI::Proxy's CRUD methods.

  • to (CrAPI::Client, CrAPI::Proxy)

    The parent CrAPI::Client or CrAPI::Proxy that we'll be delegating CRUD calls to after proprocessing of headers and paths.

  • headers (Hash) (defaults to: nil)

    The default headers to send with every request (unless overriden elsewhere).



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_headersObject

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

Returns:

  • (Object)

See Also:



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

Returns:

  • (Object)

See Also:



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.

Parameters:

  • segment (String) (defaults to: '/')

    The segment to add to this proxy's path.

  • headers (Hash) (defaults to: nil)

    The default headers for the new proxy.

Returns:

See Also:



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

Returns:

  • (Object)

See Also:



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

Returns:

  • (Object)

See Also:



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

Returns:

  • (Object)

See Also:



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