Class: RestChain::Chain

Inherits:
Link
  • Object
show all
Defined in:
lib/rest_chain/chain.rb

Constant Summary collapse

DEFAULT_SCHEME =
"https".freeze

Instance Attribute Summary collapse

Attributes inherited from Link

#name, #predecessor

Instance Method Summary collapse

Methods inherited from Link

#delete, #dispatch, #get, #log, #method_missing, #patch, #post, #put, #respond_to?

Constructor Details

#initialize(settings = {}) ⇒ Chain

Constructor for the Chain class.

Parameters

settings

A Hash of the settings for the chain. Currently recognised settings are…

* :host        The REST API host to be interacted with, this
               must be specified.
* :logger      The Logger to be used in the Chain.
* :parameters  A Hash of default parameters that get sent
               with all requests.
* :path        Either a String or an Array. If its an array
               the elements will be concatentated together
               into a String. This specifies common elements
               of the path to avoid having to repeatedly
               specify them as part of the chain.
* :port        The port number to contact the REST API host
               on. Need not be specified if the default port
               (80 for HTTP and 443 for HTTPS) are to be used.
* :scheme      Should be given a value of either "http" or "https".
               This is the scheme that will be used when talking
               to the REST API. Defaults to "https" if not 
               explicitly specified.

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rest_chain/chain.rb', line 29

def initialize(settings={})
  super("", nil, settings[:logger])
  @host   = settings.fetch(:host, "").strip
  raise RestChainError.new("No REST API host specified when creating a chain.") if @host == ""

  @port   = settings[:port]
  @scheme = settings.fetch(:scheme, DEFAULT_SCHEME).downcase
  @path   = settings.fetch(:path, [])
  @path   = @path.split("/").delete_if {|s| s == ""} if @path.kind_of?(String)
  @parameters = {}.merge(settings.fetch(:parameters, {}))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RestChain::Link

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



41
42
43
# File 'lib/rest_chain/chain.rb', line 41

def host
  @host
end

#pathObject (readonly)

Returns the value of attribute path.



41
42
43
# File 'lib/rest_chain/chain.rb', line 41

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



41
42
43
# File 'lib/rest_chain/chain.rb', line 41

def port
  @port
end

#schemeObject (readonly)

Returns the value of attribute scheme.



41
42
43
# File 'lib/rest_chain/chain.rb', line 41

def scheme
  @scheme
end

Instance Method Details

#full_parameters(extras = {}) ⇒ Object



43
44
45
# File 'lib/rest_chain/chain.rb', line 43

def full_parameters(extras={})
  {}.merge(@parameters).merge(extras)
end

#urlObject



47
48
49
50
51
52
53
54
# File 'lib/rest_chain/chain.rb', line 47

def url
  text = StringIO.new
  text << @scheme << "://" << @host
  text << ":#{@port}" if @port
  text << "/" if text.string[-1,1] != "/"
  text << "#{@path.join('/')}"
  text.string
end