浏览代码

Refactor API logic out of User model.

old-ruby
Ben Kurtovic 9 年前
父节点
当前提交
e607a772c1
共有 2 个文件被更改,包括 50 次插入35 次删除
  1. +16
    -27
      app/models/user.rb
  2. +34
    -8
      lib/calefaction/eve.rb

+ 16
- 27
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

+ 34
- 8
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

正在加载...
取消
保存