Class: Tickethub::Request

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

Constant Summary collapse

SAFE_TO_RETRY =
[408, 502, 503, 504].freeze
MAXIMUM_RETRIES =
3.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.



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

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

  @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



13
14
15
# File 'lib/tickethub/request.rb', line 13

def auth_type
  @auth_type
end

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#passwordObject

Connection options



13
14
15
# File 'lib/tickethub/request.rb', line 13

def password
  @password
end

#proxyObject

Connection options



13
14
15
# File 'lib/tickethub/request.rb', line 13

def proxy
  @proxy
end

#ssl_optionsObject

Connection options



13
14
15
# File 'lib/tickethub/request.rb', line 13

def ssl_options
  @ssl_options
end

#timeoutObject

Connection options



13
14
15
# File 'lib/tickethub/request.rb', line 13

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#userObject

Connection options



13
14
15
# File 'lib/tickethub/request.rb', line 13

def user
  @user
end

Instance Method Details

#execute(retries = 0) ⇒ Object



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

def execute(retries = 0)
  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 => err
  raise if retries == MAXIMUM_RETRIES - 1
  execute retries + 1
rescue ConnectionError => err
  raise if ! SAFE_TO_RETRY.member?(err.response.code) || retries == MAXIMUM_RETRIES - 1
  execute retries + 1
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



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

def path
  uri.path
end

#uriObject



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

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