class TinyClient::UrlBuilder

Convenient class used to build a request URL.

Constants

SEPARATOR

Public Class Methods

new(url) click to toggle source
# File lib/tiny_client/url_builder.rb, line 40
def initialize(url)
  @url = parse_url(url)
  @path = []
  @query = {}
end
url(url) click to toggle source
# File lib/tiny_client/url_builder.rb, line 8
def self.url(url)
  new(url)
end

Public Instance Methods

build() click to toggle source

@return [String] url with all paths and query params

# File lib/tiny_client/url_builder.rb, line 26
def build
  url = "#{[@url, @path].join(SEPARATOR)}.json"
  url = "#{url}?#{@query.to_query}" unless @query.empty?
  url
end
build!() click to toggle source

@deprecated Please use {#build} instead

# File lib/tiny_client/url_builder.rb, line 33
def build!
  ActiveSupport::Deprecation.warn('`build!` is deprecated. Please use `build` instead')
  build
end
path(*paths) click to toggle source
# File lib/tiny_client/url_builder.rb, line 12
def path(*paths)
  paths.each do |path|
    new_path = fix_path(path)
    @path << new_path if new_path.present?
  end
  self
end
query(params = {}) click to toggle source
# File lib/tiny_client/url_builder.rb, line 20
def query(params = {})
  @query.merge!(params) unless params.empty?
  self
end

Private Instance Methods

fix_path(path) click to toggle source
# File lib/tiny_client/url_builder.rb, line 55
def fix_path(path)
  case path
  when String
    path = path.gsub(/\.json$/, '')
    path = path[1..-1] if path.start_with?('/')
    path = path[0..-2] if path.end_with?('/')
    path
  else
    path
  end
end
parse_url(url) click to toggle source
# File lib/tiny_client/url_builder.rb, line 46
def parse_url(url)
  if url.blank? || url == SEPARATOR
    ''
  else
    url = url[0..-2] if url.end_with?(SEPARATOR)
    url
  end
end