Skip to content

Commit eb12fde

Browse files
committed
Finalize character endpoint
Two new parameter have been added to the character lookup: all_data and poll. The all_data parameter will toggle between the standard lookup and a lookup with all of the data flags. The poll parameter will continuously poll the API until the character has been cached. A new type of request has been created to handle this continuous polling. It also handles the various errors states that may be returned and throws a custom exception for each state. Adds the character update and delete endpoints.
1 parent bf9e64a commit eb12fde

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

lib/xivapi.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'xivapi/version'
22

3+
require 'xivapi/exceptions'
34
require 'xivapi/http'
45
require 'xivapi/page'
56
require 'xivapi/paginator'

lib/xivapi/exceptions.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module XIVAPI
2+
class RequestError < StandardError
3+
def initialize(response)
4+
if response.headers[:content_type] == 'application/problem+json'
5+
message = JSON.parse(response)['Message']
6+
else
7+
message = 'Error contacting the API'
8+
end
9+
10+
super(message)
11+
end
12+
end
13+
14+
class ContentNotAvailable < StandardError
15+
def initialize
16+
super('Content is not currently available on XIVAPI')
17+
end
18+
end
19+
20+
class ContentNotFound < StandardError
21+
def initialize
22+
super('Content does not exist on the Lodestone.')
23+
end
24+
end
25+
26+
class ContentBlacklisted < StandardError
27+
def initialize
28+
super('Content has been blacklisted.')
29+
end
30+
end
31+
32+
class ContentPrivate < StandardError
33+
def initialize
34+
super('Content is private.')
35+
end
36+
end
37+
end

lib/xivapi/http.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,35 @@ def request(client, endpoint, params = {})
1111
response = RestClient.get(url, params: query_params)
1212
body = JSON.parse(response.body)
1313
objectify(body)
14+
rescue RestClient::ExceptionWithResponse => e
15+
raise XIVAPI::RequestError.new(e.response)
1416
rescue RestClient::Exception => e
15-
raise e.response
17+
raise e
1618
end
1719
end
1820

21+
def request_cached(client, endpoint, key, params = {}, poll = false)
22+
response = request(client, endpoint, params)
23+
24+
case(response.info[key].state)
25+
when 0
26+
raise XIVAPI::ContentNotAvailable
27+
when 1
28+
if poll
29+
sleep(client.poll_rate)
30+
response = request_cached(client, endpoint, key, params, poll)
31+
end
32+
when 3
33+
raise XIVAPI::ContentNotFound
34+
when 4
35+
raise XIVAPI::ContentBlacklisted
36+
when 5
37+
raise XIVAPI::ContentPrivate
38+
end
39+
40+
response
41+
end
42+
1943
private
2044
def request_url(endpoint)
2145
"#{API_BASE}/#{endpoint}"

lib/xivapi/request.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module XIVAPI::Request
22
include XIVAPI::HTTP
33

44
LODESTONE_LIMIT = 50.freeze
5+
ALL_CHARACTER_DATA = 'AC,FR,FC,FCM,PVP'.freeze
56

67
def search(indexes: [], string: '', string_column: 'Name_en', string_algo: 'wildcard_plus',
78
sort_field: nil, sort_order: nil, limit: 100, filters: [], columns: [])
@@ -10,17 +11,26 @@ def search(indexes: [], string: '', string_column: 'Name_en', string_algo: 'wild
1011
XIVAPI::Paginator.new(self, params, 'search', limit)
1112
end
1213

13-
def character(id: nil, data: [], columns: [])
14-
params = { data: [*data].join(','), columns: [*columns].join(',') }
15-
request(self, "character/#{id}", params)
14+
def character(id: nil, all_data: false, poll: false, columns: [])
15+
params = { data: all_data ? ALL_CHARACTER_DATA : nil, columns: [*columns].join(',') }
16+
request_cached(self, "character/#{id}", :character, params, poll)
1617
end
1718

1819
def character_search(name: nil, server: nil)
1920
params = { name: name, server: server&.capitalize }
2021
XIVAPI::Paginator.new(self, params, 'character/search', LODESTONE_LIMIT)
2122
end
2223

23-
def character_verified?(id)
24+
def character_delete(id: nil, duplicate_id: nil)
25+
params = { duplicate: duplicate_id }
26+
!!request(self, "character/#{id}/delete", params)
27+
end
28+
29+
def character_update(id: nil)
30+
request(self, "character/#{id}/update") == 1
31+
end
32+
33+
def character_verified?(id: nil)
2434
request(self, "character/#{id}/verification").verification_token_pass
2535
end
2636

0 commit comments

Comments
 (0)