From e607a772c1c2d0aee5df7ba127a5734320a8d95c Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 17 Mar 2015 01:36:54 -0500 Subject: [PATCH] Refactor API logic out of User model. --- app/models/user.rb | 43 ++++++++++++++++--------------------------- lib/calefaction/eve.rb | 42 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index f8b516b..c0366da 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,48 +1,37 @@ -require 'eaal' +require 'calefaction/eve' class User < ActiveRecord::Base has_secure_password alias_attribute :admin?, :is_admin + def characters + ensure_api_user + chars = @api.characters + chars.nil? ? [] : chars + end + def name ensure_api_user - @api.scope = 'char' - begin - @api.CharacterSheet(characterID: userid).name - rescue EAAL::Exception::EAALError - '?' - end + sheet = @api.character_sheet(userid) + sheet.nil? ? '?' : sheet.name end - def in_corp? - member_of? AdminSetting.get(:corp_id).to_i + def corp_id + ensure_api_user + sheet = @api.character_sheet(userid) + sheet.nil? ? 0 : sheet.corporationID.to_i end def member_of?(corp) corp_id == corp end - def corp_id - ensure_api_user - @api.scope = 'char' - begin - @api.CharacterSheet(characterID: userid).corporationID.to_i - rescue EAAL::Exception::EAALError - 0 - end - end - - def characters - ensure_api_user - begin - @api.Characters.characters - rescue EAAL::Exception::EAALError - [] - end + def in_corp? + member_of? AdminSetting.get(:corp_id).to_i end private def ensure_api_user - @api ||= EAAL::API.new(api_key, api_verify) + @api ||= Calefaction::EVE::APIUser.new(api_key, api_verify) end end diff --git a/lib/calefaction/eve.rb b/lib/calefaction/eve.rb index 4abc23e..b3609e1 100644 --- a/lib/calefaction/eve.rb +++ b/lib/calefaction/eve.rb @@ -1,28 +1,54 @@ +require 'eaal' + module Calefaction::EVE extend self + class APIUser + def initialize(key_id, vcode) + @api = EAAL::API.new(key_id, vcode) + end + + def character_sheet(char_id) + @api.scope = 'char' + begin + @api.CharacterSheet(characterID: char_id) + rescue EAAL::Exception::EAALError + nil + end + end + + def characters + @api.scope = 'account' + begin + @api.Characters.characters + rescue EAAL::Exception::EAALError + nil + end + end + end + def corp_ticker(corp_id) cache_key = "calefaction/eve/corp_ticker/#{corp_id}" existing = Rails.cache.read(cache_key) return existing unless existing.nil? - ticker = get_corp_ticker_from_api(corp_id) - return '?' if ticker.nil? - Rails.cache.write(cache_key, ticker) - ticker + sheet = corporation_sheet(corp_id) + return '?' if sheet.nil? + Rails.cache.write(cache_key, sheet.ticker) + sheet.ticker end private - def get_corp_ticker_from_api(corp_id) - ensure_api + def corporation_sheet(corp_id) + ensure_basic_api @@api.scope = 'corp' begin - @@api.CorporationSheet(corporationID: corp_id).ticker + @@api.CorporationSheet(corporationID: corp_id) rescue EAAL::Exception::EAALError nil end end - def ensure_api + def ensure_basic_api @@api ||= EAAL::API.new(nil, nil) end end