Class: Apisync::Http::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/apisync/http/url.rb

Overview

Responsible for generating URLs

Constant Summary collapse

DEFAULT_HOST =
"https://api.apisync.io"

Instance Method Summary collapse

Constructor Details

#initialize(resource_name:, id: nil, filters: nil, options:) ⇒ Url

  • resource_name: a name in plural such as ‘users’, ‘profiles’ etc.

  • id: id of the resource that you’re looking for

  • filters: these will define what’s in the query string, such as ‘filter=value’

  • options: allows you to pass options such ‘host’. Accepted options are

    • host: a custom host for the URL, defaults to DEFAULT_HOST



15
16
17
18
19
20
21
22
# File 'lib/apisync/http/url.rb', line 15

def initialize(resource_name:, id: nil, filters: nil, options:)
  @resource_name = resource_name
  @id = id
  @filters = filters
  @options = {
    host: nil
  }.merge(options)
end

Instance Method Details

#to_sObject

to_s

Takes a host, api_version, resource name and id and form the URL. Then pass filters and other options into QueryString class which will return whatever is after the ‘?` symbol.

Returns a string such as

'https://api.apisync.io/inventory-items?filter[application-id]=abc'

If there are no query strings, omits the ‘?`

'https://api.apisync.io/inventory-items'


38
39
40
41
42
43
44
45
46
47
# File 'lib/apisync/http/url.rb', line 38

def to_s
  url = [
    host,
    api_version,
    normalized_resource_name,
    @id
  ].compact.join("/")
  url = remove_duplicated_slashes(url)
  [url, query_string].compact.join("?")
end