class REQUESTER

Makes requests for the client

Attributes

log[RW]

Public Class Methods

new() click to toggle source
# File lib/SWGOH/API/CLIENT/requester.rb, line 13
def initialize
  @log = LOG.new
end

Public Instance Methods

auth_request(username, password) click to toggle source

@param [String] username @param [String] password @return [JSON || nil]

# File lib/SWGOH/API/CLIENT/requester.rb, line 20
def auth_request(username, password)
  response = Net::HTTP.post_form(uri(SWGOH::API::PATH::AUTH_SIGNIN), auth_request_form(username, password))
  return @log.error(response) unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body)
end
request(access_token, path, data) click to toggle source

@param [String] access_token @param [PATH] path @param [Hash] data @return [JSON || nil]

# File lib/SWGOH/API/CLIENT/requester.rb, line 31
def request(access_token, path, data)
  response = Net::HTTP.post(uri(path), default_data.merge(data).to_json, request_headers(access_token))
  return @log.error(response) unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body)
end

Private Instance Methods

auth_request_form(username, password) click to toggle source

@param [String] username @param [String] password @return [Hash]

# File lib/SWGOH/API/CLIENT/requester.rb, line 48
def auth_request_form(username, password)
  {
    username: username,
    password: password,
    grant_type: 'password',
    client_id: 123,
    client_secret: 'abc'
  }
end
default_data() click to toggle source

@return [Hash]

# File lib/SWGOH/API/CLIENT/requester.rb, line 67
def default_data
  {
    language: SWGOH::API::LANGUAGE::ENG_US,
    enums: false,
    structure: false
  }
end
request_headers(access_token) click to toggle source

@return [Hash]

# File lib/SWGOH/API/CLIENT/requester.rb, line 59
def request_headers(access_token)
  {
    Authorization: 'Bearer ' + access_token,
    'Content-Type': 'application/json;charset=utf-8'
  }
end
uri(path) click to toggle source

@return [Module<URI>]

# File lib/SWGOH/API/CLIENT/requester.rb, line 41
def uri(path)
  URI("https://#{SWGOH::API::PATH::BASE}/#{path}")
end