Class: Kayakoapi::URLGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/kayakoapi-ruby/urlgenerator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_root) ⇒ URLGenerator

Returns a new instance of URLGenerator.



7
8
9
10
11
# File 'lib/kayakoapi-ruby/urlgenerator.rb', line 7

def initialize(url_root)
  @url_root = url_root
  @params = {}
  @first_param = true
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/kayakoapi-ruby/urlgenerator.rb', line 5

def params
  @params
end

#url_rootObject

Returns the value of attribute url_root.



5
6
7
# File 'lib/kayakoapi-ruby/urlgenerator.rb', line 5

def url_root
  @url_root
end

Class Method Details

.urlncode(string) ⇒ Object



38
39
40
# File 'lib/kayakoapi-ruby/urlgenerator.rb', line 38

def self.urlncode(string)
  URI.escape(string, Regexp.new("[Generating an API Signature^#{URI::PATTERN::UNRESERVED}]"))
end

Instance Method Details

#append_to_url(key, value) ⇒ Object



13
14
15
# File 'lib/kayakoapi-ruby/urlgenerator.rb', line 13

def append_to_url(key, value)
  @params[key] = value
end

#first_param?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/kayakoapi-ruby/urlgenerator.rb', line 34

def first_param?
  @first_param
end

#full_urlObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kayakoapi-ruby/urlgenerator.rb', line 17

def full_url
  current_url = @url_root
  if @params.empty?
    raise NoURLParameters.new(msg: "Params hash is empty while attempting to build a URL.")
  else
    @params.each do |key, value|
      if @first_param
        current_url += "?#{key}=#{value}"
        @first_param = false
      else
        current_url += "&#{key}=#{value}"
      end
    end
    return current_url
  end
end