Class: Tickethub::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/tickethub/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Request

Returns a new instance of Request.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tickethub/request.rb', line 12

def initialize(url, options = {})
  @url = url.to_s
  @id  = SecureRandom.uuid
  @retries = 3
  @timeout = 10

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

  self.method  ||= :get
  self.params  ||= {}
  self.headers ||= {}
  self.format  ||= :form
end

Instance Attribute Details

#auth_typeObject

Connection options



10
11
12
# File 'lib/tickethub/request.rb', line 10

def auth_type
  @auth_type
end

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/tickethub/request.rb', line 7

def body
  @body
end

#formatObject

Returns the value of attribute format.



6
7
8
# File 'lib/tickethub/request.rb', line 6

def format
  @format
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/tickethub/request.rb', line 7

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



7
8
9
# File 'lib/tickethub/request.rb', line 7

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/tickethub/request.rb', line 6

def options
  @options
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/tickethub/request.rb', line 7

def params
  @params
end

#passwordObject

Connection options



10
11
12
# File 'lib/tickethub/request.rb', line 10

def password
  @password
end

#proxyObject

Connection options



10
11
12
# File 'lib/tickethub/request.rb', line 10

def proxy
  @proxy
end

#retriesObject

Connection options



10
11
12
# File 'lib/tickethub/request.rb', line 10

def retries
  @retries
end

#ssl_optionsObject

Connection options



10
11
12
# File 'lib/tickethub/request.rb', line 10

def ssl_options
  @ssl_options
end

#timeoutObject

Connection options



10
11
12
# File 'lib/tickethub/request.rb', line 10

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/tickethub/request.rb', line 6

def url
  @url
end

#userObject

Connection options



10
11
12
# File 'lib/tickethub/request.rb', line 10

def user
  @user
end

Instance Method Details

#executeObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tickethub/request.rb', line 61

def execute
  if encoded?
    result = connection.send(method, path, encoded, build_headers)
  else
    result = connection.send(method, query_path, build_headers)
  end

  Response.new(result)

rescue TimeoutError, ServerError => err
  raise err if (@retries -= 1) == 0
  execute
rescue Redirection => error
  raise error unless error.response['Location']
  location = URI.parse(error.response['Location'])

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

  self.url = location.to_s
  execute
end

#pathObject



57
58
59
# File 'lib/tickethub/request.rb', line 57

def path
  uri.path
end

#uriObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tickethub/request.rb', line 41

def uri
  return @uri if @uri

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

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

  if @uri.query
    @params.merge!(Helpers.from_param(@uri.query))
    @uri.query = nil
  end

  @uri
end