Module: Arke::Resource::URL::ClassMethods

Defined in:
lib/arke/resource/url.rb

Instance Method Summary collapse

Instance Method Details

#endpoint(endpoint = nil) ⇒ String

Gets or sets the device endpoint. By default the endpoint uses #resource_name if an endpoint has not been provided.

Parameters:

  • the (String)

    service endpoint. if left nil it will simply return the existing endpoint

Returns:

  • (String)

    the service endpoint



54
55
56
57
# File 'lib/arke/resource/url.rb', line 54

def endpoint(endpoint=nil)
  @endpoint = endpoint if endpoint
  @endpoint ||= resource_name
end

#host(host = nil) ⇒ String

Gets or sets the resource host.

Parameters:

  • host (String) (defaults to: nil)

    the resource host. if left nil, the method will simply return the current resource host

Returns:

  • (String)

    the current resource host



30
31
32
33
# File 'lib/arke/resource/url.rb', line 30

def host(host=nil)
  @host = host if host
  @host
end

#url(options = {}) ⇒ String

Gets the resource url. This compiles the url template using the passed options and returns the resulting string.

query.

Parameters:

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

    the url options to pass to the template. By default the template looks for a an id and a

Returns:

  • (String)

    the full resource url



42
43
44
45
46
# File 'lib/arke/resource/url.rb', line 42

def url(options={})
  template = options[:_template] ? Addressable::Template.new(options.delete(:_template)) : nil ||self.url_template
  association_options = {parent_resource_name: options.delete(:_parent_resource_name), relation_id: options.delete(:_relation_id), resource_name: options.delete(:_resource_name)}
  host + template.expand(association_options.merge(options.merge(endpoint: endpoint))).to_s
end

#url_template(template = nil) ⇒ Addressable::Template

Gets or sets the url template for the resource. The url template is a Addressable template and defaults to DEFAULT_URL_TEMPLATE. If a parameter is passed to this method it will set the url template to the passed parameter, otherwise it will just return the URL template.

url template

Parameters:

  • template (String) (defaults to: nil)

    the url template to set, if left nil, the method will simply return the current

Returns:

  • (Addressable::Template)

    the url template. If none has been set, it references DEFAULT_URL_TEMPLATE



20
21
22
23
# File 'lib/arke/resource/url.rb', line 20

def url_template(template=nil)
  @url_template = Addressable::Template.new(template) if template
  @url_template || Addressable::Template.new(DEFAULT_URL_TEMPLATE)
end