From dfa4f062451617161bd1e190e1a1233bf5e801d5 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 17 Mar 2015 23:06:04 -0500 Subject: [PATCH] Refactor Calefaction::EVE. --- lib/calefaction/eve.rb | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/lib/calefaction/eve.rb b/lib/calefaction/eve.rb index 9086571..664af16 100644 --- a/lib/calefaction/eve.rb +++ b/lib/calefaction/eve.rb @@ -4,23 +4,27 @@ module Calefaction::EVE extend self class APIUser - def initialize(key_id, vcode) + def initialize(key_id=nil, vcode=nil) @api = EAAL::API.new(key_id, vcode) end + def characters + query('account') { @api.Characters.characters } + end + def character_sheet(char_id) - @api.scope = 'char' - begin - @api.CharacterSheet(characterID: char_id) - rescue EAAL::Exception::EAALError - nil - end + query('char') { @api.CharacterSheet(characterID: char_id) } end - def characters - @api.scope = 'account' + def corporation_sheet(corp_id) + query('corp') { @api.CorporationSheet(corporationID: corp_id) } + end + + private + def query(scope) + @api.scope = scope begin - @api.Characters.characters + yield rescue EAAL::Exception::EAALError nil end @@ -31,23 +35,14 @@ module Calefaction::EVE cache_key = "calefaction/eve/corp_ticker/#{corp_id}" existing = Rails.cache.read(cache_key) return existing unless existing.nil? - sheet = corporation_sheet(corp_id) + sheet = basic_api.corporation_sheet(corp_id) return '?' if sheet.nil? Rails.cache.write(cache_key, sheet.ticker) sheet.ticker end private - def corporation_sheet(corp_id) - basic_api.scope = 'corp' - begin - basic_api.CorporationSheet(corporationID: corp_id) - rescue EAAL::Exception::EAALError - nil - end - end - def basic_api - @@api ||= EAAL::API.new(nil, nil) + @@api ||= APIUser.new end end