Class: Blanket::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/blanket/wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, options = {}) ⇒ Blanket

Wraps the base URL for an API

Parameters:

  • base_uri (String, Symbol)

    The root URL of the API you wish to wrap.

  • options (Hash) (defaults to: {})

    An options hash with global values for :headers, :extension and :params



42
43
44
45
46
47
48
# File 'lib/blanket/wrapper.rb', line 42

def initialize(base_uri, options={})
  @base_uri = base_uri
  @uri_parts = []
  @headers = options[:headers] || {}
  @params = options[:params] || {}
  @extension = options[:extension]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



52
53
54
55
56
57
58
# File 'lib/blanket/wrapper.rb', line 52

def method_missing(method, *args, &block)
  Wrapper.new uri_from_parts([method, args.first]), {
    headers: @headers,
    extension: @extension,
    params: @params
  }
end

Instance Attribute Details

#extensionObject

Attribute accessor for file extension that should be appended to all requests



30
31
32
# File 'lib/blanket/wrapper.rb', line 30

def extension
  @extension
end

#headersObject

Attribute accessor for HTTP Headers that should be applied to all requests



22
23
24
# File 'lib/blanket/wrapper.rb', line 22

def headers
  @headers
end

#paramsObject

Attribute accessor for params that should be applied to all requests



26
27
28
# File 'lib/blanket/wrapper.rb', line 26

def params
  @params
end

Instance Method Details

#deleteBlanket::Response, Array

Performs a delete request on the wrapped URL

Parameters:

  • id (String, Symbol, Numeric)

    The resource identifier to attach to the last part of the request

  • options (Hash)

    An options hash with values for :headers, :extension, :params and :body

Returns:



36
# File 'lib/blanket/wrapper.rb', line 36

add_action :delete

#getBlanket::Response, Array

Performs a get request on the wrapped URL

Parameters:

  • id (String, Symbol, Numeric)

    The resource identifier to attach to the last part of the request

  • options (Hash)

    An options hash with values for :headers, :extension, :params and :body

Returns:



32
# File 'lib/blanket/wrapper.rb', line 32

add_action :get

#patchBlanket::Response, Array

Performs a patch request on the wrapped URL

Parameters:

  • id (String, Symbol, Numeric)

    The resource identifier to attach to the last part of the request

  • options (Hash)

    An options hash with values for :headers, :extension, :params and :body

Returns:



35
# File 'lib/blanket/wrapper.rb', line 35

add_action :patch

#postBlanket::Response, Array

Performs a post request on the wrapped URL

Parameters:

  • id (String, Symbol, Numeric)

    The resource identifier to attach to the last part of the request

  • options (Hash)

    An options hash with values for :headers, :extension, :params and :body

Returns:



33
# File 'lib/blanket/wrapper.rb', line 33

add_action :post

#putBlanket::Response, Array

Performs a put request on the wrapped URL

Parameters:

  • id (String, Symbol, Numeric)

    The resource identifier to attach to the last part of the request

  • options (Hash)

    An options hash with values for :headers, :extension, :params and :body

Returns:



34
# File 'lib/blanket/wrapper.rb', line 34

add_action :put