Class: SC2Cli::Shared::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/sc2cli/shared/api.rb

Constant Summary collapse

@@console =
Console.instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, region: nil) ⇒ Api

Returns a new instance of Api.



32
33
34
35
# File 'lib/sc2cli/shared/api.rb', line 32

def initialize(configuration:, region: nil)
  @region = region || configuration.region
  @token  = Token.new(configuration: configuration, region: @region)
end

Instance Attribute Details

#regionObject (readonly)

Returns the value of attribute region.



28
29
30
# File 'lib/sc2cli/shared/api.rb', line 28

def region
  @region
end

Instance Method Details

#get(path: "/") ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sc2cli/shared/api.rb', line 39

def get(path: "/")
  @@console.fatal("Path for API request cannot be blank!") if path.empty?
  @@console.fatal("Path for API request: #{path} does NOT begin with '/'!") unless path.chars.first == "/"

  server = @region.api_server

  @token.refresh unless @token.check

  uri = URI("https://#{server}#{path}")

  @@console.info("Making API request to: #{uri.to_s}")

  request = Net::HTTP::Get.new(uri)
  request["Authorization"] = "Bearer #{@token.token}"

  http = Net::HTTP.new(uri.hostname, uri.port)
  http.use_ssl = true

  response = http.request(request)

  @@console.fatal("Blizzard API returned: #{response.code}, not 200 for reqest!") if response.code != "200"
  @@console.fatal("Response body from Blizzard API is not permitted!")            if not response.class.body_permitted?
  @@console.fatal("Response body from Blizzard API is empty!")                    if response.body.nil?

  body = JSON.parse(response.body)
  return body
end