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
29
30
31
# 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

Instance Attribute Details

#auth_typeObject

Returns the value of attribute auth_type.



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

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

#follow_redirectionObject

Returns the value of attribute follow_redirection.



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

def follow_redirection
  @follow_redirection
end

#formatObject

Returns the value of attribute format.



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

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

#max_attemptsObject

Returns the value of attribute max_attempts.



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

def max_attempts
  @max_attempts
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.



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

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

Returns the value of attribute password.



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

def password
  @password
end

#proxyObject

Returns the value of attribute proxy.



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

def proxy
  @proxy
end

#retriesObject

Returns the value of attribute retries.



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

def retries
  @retries
end

#ssl_optionsObject

Returns the value of attribute ssl_options.



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

def ssl_options
  @ssl_options
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#encodedObject



79
80
81
# File 'lib/tickethub/request.rb', line 79

def encoded
  params.any? ? format.encode(params) : body
end

#encoded?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/tickethub/request.rb', line 75

def encoded?
  [:post, :patch].include?(method)
end

#executeObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

#pathObject



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

def path
  uri.path
end

#query_pathObject



63
64
65
66
67
68
69
70
71
72
73
# 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

#uriObject



44
45
46
47
48
49
50
51
52
53
# 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_paramsObject



55
56
57
# File 'lib/tickethub/request.rb', line 55

def uri_params
  uri.query ? Helpers.from_param(uri.query) : {}
end