Commit fe7532e0 authored by Francis's avatar Francis
Browse files

Data endpoints refactory

parent 01b42e5b
PATH
remote: .
specs:
blizzard_api (0.2.0)
blizzard_api (0.2.1)
redis (~> 4.1, >= 4.1.0)
GEM
......
......@@ -7,6 +7,7 @@ module BlizzardApi
require_relative 'wow/game_data/generic_data_endpoint'
# WoW data api
require_relative 'wow/game_data/achievements'
require_relative 'wow/game_data/region'
require_relative 'wow/game_data/realm'
require_relative 'wow/game_data/connected_realm'
......@@ -16,6 +17,12 @@ module BlizzardApi
require_relative 'wow/game_data/power_type'
require_relative 'wow/game_data/mythic_keystone_affix'
##
# @return {Achievement}
def self.achievement
BlizzardApi::Wow::Achievement.new
end
##
# @return {Region}
def self.region
......@@ -65,7 +72,6 @@ module BlizzardApi
end
# Wow community api
require_relative 'wow/community/achievements'
require_relative 'wow/community/auction'
require_relative 'wow/community/boss'
require_relative 'wow/community/challenge'
......@@ -80,12 +86,6 @@ module BlizzardApi
require_relative 'wow/community/spell'
require_relative 'wow/community/zone'
##
# @return {Achievement}
def self.achievement
BlizzardApi::Wow::Achievement.new
end
##
# @return {Auction}
def self.auction
......
......@@ -5,24 +5,31 @@ module BlizzardApi
##
# This class allows access to World of Warcraft achievements
#
# @see https://develop.battle.net/documentation/api-reference/world-of-warcraft-community-api
# @see https://develop.battle.net/documentation/api-reference/world-of-warcraft-game-data-api
#
# You can get an instance of this class using the default region as follows:
# api_instance = BlizzardApi::Wow.achievements
class Achievement < Wow::Request
class Achievement < Wow::GenericDataEndpoint
##
# Return complete achievement data by achievement id
# Fetch all possible data for one of the items listed by the {#index} using its *id*
#
# @param id [Integer] One of the IDs returned by the {#index}
#
# @param id [Integer] Achievement id
# @!macro request_options
# @option options [Boolean] :use_community_endpoint If set to true, this method will call the community endpoint
# instead of the data endpoint https://develop.battle.net/documentation/api-reference/world-of-warcraft-community-api
#
# @!macro response
def get(id, options = {})
return super options unless options.include? :use_community_endpoint
api_request "#{base_url(:community)}/achievement/#{id}", { ttl: CACHE_TRIMESTER }.merge(options)
end
##
# Return a list of possible character achievements
# Return a list of possible character achievements.
# This endpoint uses a community endpoint. It is recommended to use the new data endpoint using {#index}.
# @see https://us.battle.net/forums/en/bnet/topic/20771546990
#
# @!macro request_options
#
......@@ -33,6 +40,8 @@ module BlizzardApi
##
# Return a list of possible guild achievements
# This endpoint uses a community endpoint. It is recommended to use the new data endpoint using {#index}.
# @see https://us.battle.net/forums/en/bnet/topic/20771546990
#
# @!macro request_options
#
......@@ -40,6 +49,15 @@ module BlizzardApi
def guild_achievement_index(options = {})
api_request "#{base_url(:community)}/data/guild/achievements", { ttl: CACHE_TRIMESTER }.merge(options)
end
protected
def endpoint_setup
@endpoint = 'achievement'
@namespace = endpoint_namespace :static
@collection = 'achievements'
@ttl = CACHE_TRIMESTER
end
end
end
end
......@@ -10,20 +10,6 @@ module BlizzardApi
# You can get an instance of this class using the default region as follows:
# api_instance = BlizzardApi::Wow.connected_realms
class ConnectedRealm < Wow::GenericDataEndpoint
##
# @!macro complete
def complete(options = {})
[].tap do |data|
index_data = index options
index_data[:connected_realms].each do |realm|
realm_id = %r{connected-realm/([0-9]+)}.match(realm[:href])[1]
realm_data = get realm_id, options
realm_data.delete :_links
data.push realm_data
end
end
end
protected
def endpoint_setup
......
......@@ -23,7 +23,7 @@ module BlizzardApi
end
##
# Fetch all possible data for one of items listed by the {#index} using its *id*
# Fetch all possible data for one of the items listed by the {#index} using its *id*
#
# @param id [Integer] One of the IDs returned by the {#index}
# @!macro request_options
......@@ -36,10 +36,14 @@ module BlizzardApi
##
# @!macro complete
def complete(options = {})
index_data = index options
index_data[@collection.to_sym].tap do |collection|
collection.each do |item|
item.delete 'key'
[].tap do |complete_data|
index_data = index options
index_data[@collection.to_sym].each do |item|
link = item.key?(:key) ? item[:key][:href] : item[:href]
item_data = request link
complete_data.push item_data
rescue
print item
end
end
end
......
......@@ -24,20 +24,6 @@ module BlizzardApi
api_request "#{base_url(:community)}/data/character/races", { ttl: CACHE_TRIMESTER }.merge(options)
end
##
# @!macro complete
def complete(options = {})
index_data = index options
[].tap do |races|
index_data[:races].each do |race|
race_id = %r{race/([0-9]+)}.match(race[:key].to_s)[1]
race_data = get race_id, options
race_data.delete :name
races.push race_data
end
end
end
protected
def endpoint_setup
......
......@@ -21,20 +21,6 @@ module BlizzardApi
api_request "#{base_url(:community)}/realm/status", { ttl: CACHE_MINUTE }.merge(options)
end
##
# @!macro complete
def complete(options = {})
index_data = index options
[].tap do |realms|
index_data[:realms].each do |realm|
realm_data = get realm[:id], options
realm_data.delete :_links
realm_data[:region].delete :key
realms.push realm_data
end
end
end
protected
def endpoint_setup
......
......@@ -10,19 +10,6 @@ module BlizzardApi
# You can get an instance of this class using the default region as follows:
# api_instance = BlizzardApi::Wow.region
class Region < Wow::GenericDataEndpoint
##
# @!macro complete
def complete(options = {})
[].tap do |data|
index_data = index options
index_data[:regions].each do |region|
region_data = request region[:href], options
region_data.delete :_links
data.push region_data
end
end
end
##
# Returns data about region battlegroups
#
......
......@@ -5,9 +5,18 @@ require 'test_helper'
module BlizzardApi
module Wow
class AchievementTest < Minitest::Test
def test_achievement_index
achievement = BlizzardApi::Wow::Achievement.new
achievement_data = achievement.index
assert_equal 4874, achievement_data[:achievements].count
end
def test_achievement_get
achievement = BlizzardApi::Wow::Achievement.new
achievement_data = achievement.get 2144
achievement_data = achievement.get 6
assert_equal 'Level 10', achievement_data[:name][:en_US]
achievement_data = achievement.get 2144, use_community_endpoint: true
assert_equal 50, achievement_data[:points]
end
......@@ -22,6 +31,13 @@ module BlizzardApi
achievement_data = achievement.guild_achievement_index
assert_equal 15_088, achievement_data[:achievements][0][:id]
end
def test_achievement_complete
assert_raise BlizzardApi::ApiException do
achievement = BlizzardApi::Wow::Achievement.new
achievement.complete
end
end
end
end
end
......@@ -8,7 +8,7 @@ module BlizzardApi
def test_race_index
race = BlizzardApi::Wow::Race.new
race_data = race.index
assert_equal 19, race_data[:races].count
assert_equal 21, race_data[:races].count
end
def test_race_get
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment