Class: XIVAPI::Client

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

Overview

An asynchronous Ruby wrapper for XIVAPI.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language = "en", version = "latest", verbose = false) ⇒ Client

Initialize a new client for making API requests.

Parameters:

  • language (String) (defaults to: "en")

    The supported by the gata data format

  • version (String) (defaults to: "latest")

    The supported version of the game to use for the API.

  • verbose (true, false) (defaults to: false)

    Whether to enable verbose logging.



25
26
27
28
29
# File 'lib/xivapi.rb', line 25

def initialize(language = "en", version = "latest", verbose = false)
  self.language = language
  @version = version
  @verbose = verbose
end

Instance Attribute Details

#languageString

Returns The supported by the gata data format.

Returns:

  • (String)

    The supported by the gata data format



32
33
34
# File 'lib/xivapi.rb', line 32

def language
  @language
end

#loggerObject

Returns the value of attribute logger.



19
20
21
# File 'lib/xivapi.rb', line 19

def logger
  @logger
end

#verboseObject

Returns the value of attribute verbose.



19
20
21
# File 'lib/xivapi.rb', line 19

def verbose
  @verbose
end

#versionObject

Returns the value of attribute version.



19
20
21
# File 'lib/xivapi.rb', line 19

def version
  @version
end

Instance Method Details

#request(path, params = {}) ⇒ Hash, String

Make a raw request to the API at the specified path with the specified parameters.

Parameters:

  • path (String)

    The path to make the request to, relative to the base API URL.

  • params (Hash) (defaults to: {})

    The query parameters to include in the request.

Returns:

  • (Hash, String)

    The parsed JSON response or a bytestring of the response body if the content type is not JSON.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/xivapi.rb', line 54

def request(path, params = {})
  merged = {language: @language, version: @version}.merge(params)
  if merged.any? { |k, v| v.nil? }
    raise ArgumentError, "Nil values are not allowed in query parameters"
  elsif merged[:verbose] == true
    raise ArgumentError, "Verbose mode is not supported as a query parameter. Set verbose mode on the client instance instead."
  end

  uri = URI.join("https://v2.xivapi.com/api/", path)
  uri.query = URI.encode_www_form(merged.compact) unless merged.empty?
  puts "=> #{uri}" if @verbose
  response = Net::HTTP.get_response(uri)
  raise response.error! unless response.is_a?(Net::HTTPSuccess)

  if response.content_type&.include?("application/json")
    json = JSON.parse(response.body)

    raise json["error"].to_s if json.is_a?(Hash) && json["error"]

    json
  else
    response.body
  end
end

#search(params = {}) ⇒ Hash

Fetch information about rows and their related data that match the provided search query.

Parameters:

  • params (Hash) (defaults to: {})

    Query paramters accepted by the search endpoint.

Returns:

  • (Hash)

    Response structure for the search endpoint.



46
47
48
# File 'lib/xivapi.rb', line 46

def search(params = {})
  request("search", params)
end