class Tickethub::Request

Attributes

auth_type[RW]
body[RW]
follow_redirection[RW]
format[R]
headers[RW]
max_attempts[RW]
method[RW]
options[R]
params[RW]
password[RW]
proxy[RW]
retries[RW]
ssl_options[RW]
timeout[RW]
url[R]
user[RW]

Public Class Methods

new(url, options = {}) click to toggle source
# File lib/tickethub/request.rb, line 12
def initialize(url, options = {})
  @url = url.to_s
  @id  = SecureRandom.uuid
  @retries = 6
  @timeout = 10

  @options = {
    :method           => :get,
    :params           => {},
    :headers          => {},
    :format           => :form,
    :max_attempts     => 5,
    :follow_redirection => true
  }.merge(options)

  @options.each do |key, val|
    method = "#{key}="
    send(method, val) if respond_to?(method)
  end
end

Public Instance Methods

encoded() click to toggle source
# File lib/tickethub/request.rb, line 79
def encoded
  params.any? ? format.encode(params) : body
end
encoded?() click to toggle source
# File lib/tickethub/request.rb, line 75
def encoded?
  [:post, :patch].include?(method)
end
execute() click to toggle source
# File lib/tickethub/request.rb, line 83
def execute
  with_redirection do
    if encoded?
      result = connection.send(method, query_path, encoded, build_headers)
    else
      result = connection.send(method, query_path, build_headers)
    end

    Response.new(result, uri)
  end
rescue ServerError, RequestError => err
  raise err if (@retries -= 1) == 0
  execute
end
format=(mime_or_format) click to toggle source
# File lib/tickethub/request.rb, line 33
def format=(mime_or_format)
  @format = mime_or_format.is_a?(Symbol) ?
    Formats[mime_or_format].new : mime_or_format
end
path() click to toggle source
# File lib/tickethub/request.rb, line 59
def path
  uri.path
end
query_path() click to toggle source
# File lib/tickethub/request.rb, line 63
def query_path
  query_path   = path.dup
  query_params = uri_params.dup
  query_params.merge!(params) unless encoded?

  if query_params.any?
    query_path += '?' + Helpers.to_url_param(query_params)
  end

  query_path
end
uri() click to toggle source
# File lib/tickethub/request.rb, line 44
def uri
  return @uri if @uri

  url = @url.match(/\Ahttps?:\/\//) ? @url : "http://#{@url}"

  @uri = URI.parse(url)
  @uri.path = '/' if @uri.path.empty?

  @uri
end
uri_params() click to toggle source
# File lib/tickethub/request.rb, line 55
def uri_params
  uri.query ? Helpers.from_param(uri.query) : {}
end
url=(value) click to toggle source
# File lib/tickethub/request.rb, line 38
def url=(value)
  @url = value
  @uri = nil
  @url
end

Protected Instance Methods

auth_headers() click to toggle source
# File lib/tickethub/request.rb, line 143
def auth_headers
  if auth_type == :bearer
    { 'Authorization' => "Bearer #{@password}" }
  elsif auth_type == :basic
    { 'Authorization' => 'Basic ' + ["#{@user}:#{@password}"].pack('m').delete("\r\n") }
  else
    { }
  end
end
build_headers() click to toggle source
# File lib/tickethub/request.rb, line 153
def build_headers
  auth_headers
    .merge(content_type_headers)
    .merge(headers)
    .merge('X-Request-ID' => @id)
end
connection() click to toggle source
# File lib/tickethub/request.rb, line 126
def connection
  Connection.new(uri,
    :proxy       => proxy,
    :timeout     => timeout,
    :ssl_options => ssl_options,
    :request     => self
  )
end
content_type_headers() click to toggle source
# File lib/tickethub/request.rb, line 135
def content_type_headers
  if encoded?
    {'Content-Type' => format.mime_type}
  else
    {}
  end
end
with_redirection() { || ... } click to toggle source
# File lib/tickethub/request.rb, line 100
def with_redirection(&block)
  attempts = 1

  begin
    yield
  rescue Redirection => error
    raise error unless follow_redirection

    attempts += 1

    raise error unless error.response['Location']
    raise RedirectionLoop.new(self, error.response) if attempts > max_attempts

    location = error.response['Location'].scrub
    location = URI.parse(location)

    # Path is relative
    unless location.host
      location = URI.join(uri, location)
    end

    self.url = location.to_s
    retry
  end
end