Created
March 18, 2018 09:19
-
-
Save armstnp/7d77715d2222ffc52581ab71f5d739f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.transform_values { |v| v.is_a?(Symbol) ? context.fetch(v) : v } | |
end | |
def request(**context) | |
Request.new path: path(context), params: full_params(context) | |
end | |
def self.request(**context) | |
self.new.request(**context) | |
end | |
end | |
class GuildUpgrade < Endpoint | |
segments 'guild', 'upgrades', :id | |
end | |
class GuildUpgrades < Endpoint | |
segments 'guild', 'upgrades' | |
params :ids | |
end | |
class GuildTreasury < Endpoint | |
authorized | |
segments 'guild', :id, 'treasury' | |
end | |
# Called as follows: | |
GuildUpgrade.request id: | |
GuildUpgrades.request ids: '38,55,58' | |
GuildTreasury.request id: 'my-guild-id', token: 'my-api-token' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment