class Apisync::Http::Url
Responsible for generating URLs
Constants
- DEFAULT_HOST
Public Class Methods
new(resource_name:, id: nil, filters: nil, options:)
click to toggle source
-
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
-
# 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
Public Instance Methods
to_s()
click to toggle source
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'
# 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
Private Instance Methods
api_version()
click to toggle source
# File lib/apisync/http/url.rb, line 51 def api_version Apisync::HttpClient::VERSION_PREFIX end
host()
click to toggle source
# File lib/apisync/http/url.rb, line 55 def host @options[:host] || DEFAULT_HOST end
normalized_resource_name()
click to toggle source
# File lib/apisync/http/url.rb, line 64 def normalized_resource_name @resource_name.to_s.downcase.gsub("_", "-") end
query_string()
click to toggle source
# File lib/apisync/http/url.rb, line 59 def query_string str = Apisync::Http::QueryString.new(filters: @filters).to_s str if str != "" end
remove_duplicated_slashes(string)
click to toggle source
# File lib/apisync/http/url.rb, line 68 def remove_duplicated_slashes(string) string.gsub(/([^:])\/\//, '\1/') end