Class: XIVAPI::Client
- Inherits:
-
Object
- Object
- XIVAPI::Client
- Defined in:
- lib/xivapi.rb
Overview
An asynchronous Ruby wrapper for XIVAPI.
Instance Attribute Summary collapse
-
#language ⇒ String
The supported by the gata data format.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize(language = "en", version = "latest", verbose = false) ⇒ Client
constructor
Initialize a new client for making API requests.
-
#request(path, params = {}) ⇒ Hash, String
Make a raw request to the API at the specified path with the specified parameters.
-
#search(params = {}) ⇒ Hash
Fetch information about rows and their related data that match the provided search query.
Constructor Details
#initialize(language = "en", version = "latest", verbose = false) ⇒ Client
Initialize a new client for making API requests.
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
#language ⇒ String
Returns The supported by the gata data format.
32 33 34 |
# File 'lib/xivapi.rb', line 32 def language @language end |
#logger ⇒ Object
Returns the value of attribute logger.
19 20 21 |
# File 'lib/xivapi.rb', line 19 def logger @logger end |
#verbose ⇒ Object
Returns the value of attribute verbose.
19 20 21 |
# File 'lib/xivapi.rb', line 19 def verbose @verbose end |
#version ⇒ Object
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.
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.
46 47 48 |
# File 'lib/xivapi.rb', line 46 def search(params = {}) request("search", params) end |