Class: GameRocket::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/gamerocket/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Http

Returns a new instance of Http.



4
5
6
# File 'lib/gamerocket/http.rb', line 4

def initialize(config)
  @config = config
end

Instance Method Details

#_body(response) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/gamerocket/http.rb', line 89

def _body(response)
  if response.header["Content-Encoding"].nil?
    response.body
  elsif response.header["Content-Encoding"] == "gzip"
    Zlib::GzipReader.new(StringIO.new(response.body)).read
  else
    raise UnexpectedError, "expected a gzipped response"
  end
end

#_current_timeObject



99
100
101
# File 'lib/gamerocket/http.rb', line 99

def _current_time
  Time.now.utc.strftime("%d/%b/%Y %H:%M:%S %Z")
end

#_http_do(http_verb, path, body = nil) ⇒ Object



57
58
59
60
61
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
# File 'lib/gamerocket/http.rb', line 57

def _http_do(http_verb, path, body = nil)      
  connection = Net::HTTP.new(@config.server, @config.port)
  connection.read_timeout = 60
  if @config.ssl?
    connection.use_ssl= true
    connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
    connection.ca_file = @config.ca_file
    connection.verify_callback = proc { |preverify_ok, ssl_context| _verify_ssl_certificate(preverify_ok, ssl_context) }
  end
  connection.start do |http|
    request = http_verb.new("#{path}")
    request["Accept"] = "application/json"
    request["User-Agent"] = @config.user_agent
    request["Accept-Encoding"] = "gzip"
    request["X-ApiVersion"] = @config.api_version
    @config.logger.debug "[GameRocket] [#{_current_time}] #{request.method} #{path}"
    if body
      request.body = body
      @config.logger.debug body
    end
    response = http.request(request)
    @config.logger.info "[GameRocket] [#{_current_time}] #{request.method} #{path} #{response.code}"
    @config.logger.debug "[GameRocket] [#{_current_time}] #{response.code} #{response.message}"
    if @config.logger.level == Logger::DEBUG
      @config.logger.debug _body(response)
    end
    response
  end
rescue OpenSSL::SSL::SSLError
  raise GameRocket::SSLCertificateError
end

#_verify_ssl_certificate(preverify_ok, ssl_context) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/gamerocket/http.rb', line 103

def _verify_ssl_certificate(preverify_ok, ssl_context)
  if preverify_ok != true || ssl_context.error != 0
    err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
    @config.logger.error err_msg
    false
  else
    true
  end
end

#delete(path, params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gamerocket/http.rb', line 45

def delete(path, params = {})
  params[:signature] = GameRocket::Crypto::sign('DELETE', @config.baseUrl + path, params, @config.secretKey)
  qs = GameRocket::Util.hash_to_query_string(params)

  response = _http_do Net::HTTP::Delete, @config.baseUrl + path + "?" + qs
  if response.code.to_i == 200
    JSON.parse(_body(response))
  else
    Util.raise_exception_for_status_code(response.code)
  end
end

#get(path, params = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gamerocket/http.rb', line 8

def get(path, params = {})
  params[:signature] = GameRocket::Crypto::sign('GET', @config.baseUrl + path, params, @config.secretKey)
  qs = GameRocket::Util.hash_to_query_string(params)

  response = _http_do Net::HTTP::Get, @config.baseUrl + path + "?" + qs
  if response.code.to_i == 200 || response.code.to_i == 422
    JSON.parse(_body(response))
  else
    Util.raise_exception_for_status_code(response.code)
  end
end

#post(path, params = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gamerocket/http.rb', line 21

def post(path, params = {})
  params[:signature] = GameRocket::Crypto::sign('POST', @config.baseUrl + path, params, @config.secretKey)
  qs = GameRocket::Util.hash_to_query_string(params)
  
  response = _http_do Net::HTTP::Post, @config.baseUrl + path, qs
  if response.code.to_i == 200 || response.code.to_i == 201 || response.code.to_i == 422
    JSON.parse(_body(response))
  else
    Util.raise_exception_for_status_code(response.code)
  end
end

#put(path, params = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gamerocket/http.rb', line 33

def put(path, params = {})
  params[:signature] = GameRocket::Crypto::sign('PUT', @config.baseUrl + path, params, @config.secretKey)
  qs = GameRocket::Util.hash_to_query_string(params)
  
  response = _http_do Net::HTTP::Put, @config.baseUrl + path, qs
  if response.code.to_i == 200 || response.code.to_i == 201 || response.code.to_i == 422
    JSON.parse(_body(response))
  else
    Util.raise_exception_for_status_codes(response.code)
  end
end