Module: Love::ResourceURI

Included in:
Client
Defined in:
lib/love.rb

Overview

Module to work with Tender REST URIs.

Instance Method Summary collapse

Instance Method Details

#append_query(base_uri, added_params = {}) ⇒ URI

Appends GET parameters to a URI instance. Duplicate parameters will be replaced with the new value.

Parameters:

  • base_uri (URI)

    The original URI to work with (will not be modified)

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

    To GET params to add.

Returns:

  • (URI)

    The URI with appended GET parameters



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/love.rb', line 107

def append_query(base_uri, added_params = {})
  base_params = base_uri.query ? CGI.parse(base_uri.query) : {}
  get_params = base_params.merge(added_params.stringify_keys)
  base_uri.dup.tap do |uri|
    assignments = get_params.map do |k, v|
      case v
        when Array; v.map { |val| "#{::CGI.escape(k.to_s)}=#{::CGI.escape(val.to_s)}" }.join('&')
        else "#{::CGI.escape(k.to_s)}=#{::CGI.escape(v.to_s)}"
      end
    end
    uri.query = assignments.join('&')
  end
end

#collection_uri(input) ⇒ URI

Returns a collection URI, based on an URI instance, a complete URI string or just a resource name.

Returns:

  • (URI)

    The URI on which the REST resource collection is accessible through the Tender REST API.

Raises:

  • (Love::Exception)

    If the input cannot be converted into a resource collection URI.



75
76
77
78
79
80
81
82
83
84
# File 'lib/love.rb', line 75

def collection_uri(input)
  case input.to_s
  when /^[\w-]+$/
    ::URI.parse("https://api.tenderapp.com/#{site}/#{input}")
  when %r[^https?://api\.tenderapp\.com/#{site}/[\w-]+]
    ::URI.parse(input.to_s)
  else
    raise Love::Exception, "This does not appear to be a valid Tender category URI!"
  end
end

#singleton_uri(input, kind) ⇒ URI

Returns a resource URI, based on an URI instance, a complete URI string or just a resource ID.

Parameters:

  • Object (Object input The complete URI or just resource ID as URI, String or Integer.)

    input The complete URI or just resource ID as URI, String or Integer.

  • kind (String)

    The kind of resource.

Returns:

  • (URI)

    The URI on which the REST resource is accessible through the Tender REST API.

Raises:

  • (Love::Exception)

    If the input cannot be converted into a resource URI.



91
92
93
94
95
96
97
98
99
100
# File 'lib/love.rb', line 91

def singleton_uri(input, kind)
  case input.to_s
  when /^\d+/
    ::URI.parse("https://api.tenderapp.com/#{site}/#{kind}/#{input}")
  when %r[^https?://api\.tenderapp\.com/#{site}/#{kind}/\d+]
    ::URI.parse(input.to_s)
  else
    raise Love::Exception, "This does not appear to be a Tender #{kind} URI or ID!"
  end
end