-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgw2_api.rb
171 lines (135 loc) · 3.85 KB
/
gw2_api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true
require 'hashie'
require 'httparty'
module GW2
module API
class Request
@@BASE_URL = "https://api.guildwars2.com/v2/"
def initialize(path:, params: {})
unless path.is_a?(Array) && path.all? { |x| x.is_a? String }
raise "Expected path segment array; received #{path}"
end
raise "Expected params hash; received #{params}" unless params.is_a? Hash
@path = path
@params = params
end
def execute
url = build_url
http_response = HTTParty.get url
raise "Failed to call #{url}: #{http_response}" unless http_response.success?
resp = http_response.parsed_response
if resp.is_a? Hash
Response.new(resp).symbolize_keys!
elsif resp.is_a? Array
resp.map! { |x| x.is_a?(Hash) ? Response.new(x).symbolize_keys! : x }
resp.extend Hashie::Extensions::DeepFind
resp.extend Hashie::Extensions::DeepLocate
else
resp
end
end
private
def build_url
params_str = params.map { |(k, v)| "#{k}=#{v}" } * '&'
url = @@BASE_URL + (path * '/')
url = "#{url}?#{params_str}" unless params_str.empty?
url
end
attr_reader :path, :params
end
class Response < Hash
include Hashie::Extensions::SymbolizeKeys
include Hashie::Extensions::MergeInitializer
include Hashie::Extensions::DeepFetch
include Hashie::Extensions::DeepFind
include Hashie::Extensions::DeepLocate
end
class Endpoint
class << self
def authorized
undef_method :"authorized?" if method_defined? :"authorized?"
define_method(:"authorized?") { true }
end
def segments(*segs)
undef_method :segments if method_defined? :segments
define_method(:segments) { segs }
end
def params(*ps)
undef_method :params if method_defined? :params
ps_hash = ps.map { |p| [p, p] }.to_h
define_method(:params) { ps_hash }
end
end
def authorized?
false
end
def segments
raise "No segments defined"
end
def params
{}
end
def path(context)
segments.map { |seg| seg.is_a?(Symbol) ? context.fetch(seg) : seg }
end
def full_params(context)
ps = params
ps.merge!({ access_token: :token}) if authorized?
ps.map { |(k, v)| [k, (v.is_a?(Symbol) ? context.fetch(v) : v)] }.to_h
end
def request(**context)
Request.new path: path(context), params: full_params(context)
end
def self.request(**context)
self.new.request(**context).execute
end
end
class Guild < Endpoint
segments 'guild', :id
end
class GuildDetails < Endpoint
authorized
segments 'guild', :id
end
class GuildSearch < Endpoint
segments 'guild', 'search'
params :name
end
class GuildUpgradeCatalogSingle < Endpoint
segments 'guild', 'upgrades', :id
end
class GuildUpgradeCatalogMany < Endpoint
segments 'guild', 'upgrades'
params :ids
end
class GuildTreasury < Endpoint
authorized
segments 'guild', :id, 'treasury'
end
class GuildUpgrades < Endpoint
authorized
segments 'guild', :id, 'upgrades'
end
class GuildLog < Endpoint
authorized
segments 'guild', :id, 'log'
end
class ItemCatalogSingle < Endpoint
segments 'items', :id
end
class ItemCatalogMany < Endpoint
segments 'items'
params :ids
end
class RenderedGuildLogo
def initialize(guild_name)
@guild_name = guild_name
end
def url
"https://guilds.gw2w2w.com/guilds/#{guild_name}/64.png"
end
private
attr_reader :guild_name
end
end
end