Class: Phlanket::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/phlanket/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



46
47
48
49
50
51
52
# File 'lib/phlanket/wrapper.rb', line 46

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)



56
57
58
59
60
61
62
# File 'lib/phlanket/wrapper.rb', line 56

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



34
35
36
# File 'lib/phlanket/wrapper.rb', line 34

def extension
  @extension
end

#headersObject

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



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

def headers
  @headers
end

#paramsObject

Attribute accessor for params that should be applied to all requests



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

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:

  • (Blanket::Response, Array)

    A wrapped Blanket::Response or an Array



40
# File 'lib/phlanket/wrapper.rb', line 40

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:

  • (Blanket::Response, Array)

    A wrapped Blanket::Response or an Array



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

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:

  • (Blanket::Response, Array)

    A wrapped Blanket::Response or an Array



39
# File 'lib/phlanket/wrapper.rb', line 39

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:

  • (Blanket::Response, Array)

    A wrapped Blanket::Response or an Array



37
# File 'lib/phlanket/wrapper.rb', line 37

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:

  • (Blanket::Response, Array)

    A wrapped Blanket::Response or an Array



38
# File 'lib/phlanket/wrapper.rb', line 38

add_action :put