Created
December 28, 2008 03:28
-
-
Save JamesHarrison/40360 to your computer and use it in GitHub Desktop.
This file contains 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
# A corporation in EVE. | |
class Corporation < ActiveRecord::Base | |
has_many :characters | |
has_many :stations | |
has_many :kills | |
has_many :kill_attackers | |
has_many :fittings | |
has_many :reimbursement_requests | |
has_one :ceo, :class_name => "Character", :foreign_key => "ceo_id" | |
belongs_to :alliance | |
# Returns the corporate logo for this corporation, either by displaying a locally stored file or returning a reference to the logo on clg.eve-metrics.com and enqueuing a job to store it locally asynchronously | |
def logo(size) | |
if File.exists? "#{RAILS_ROOT}/public/system/corporations/#{self.id}_#{size}.png" | |
return "/system/corporations/#{self.id}_#{size}.png" | |
else | |
Delayed::Job.enqueue GetCorporationLogoJob.new(self.id) | |
return "/images/no_logo_#{size}.png" | |
end | |
end | |
end |
This file contains 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
require 'net/http' | |
require 'RMagick' | |
class GetCorporationLogoJob < Struct.new(:corporation_id) | |
def perform | |
File.open("#{RAILS_ROOT}/public/system/corporations/#{corporation_id.to_i}_64.png","wb") do |f| | |
Net::HTTP.start("clg.eve-metrics.com") do |http| | |
resp = http.get("/#{corporation_id.to_i.to_s}.png") | |
return unless resp | |
if resp.response['Location'] then | |
unless resp.response['Location'].include?"not_in_alliance.png" | |
url = URI.parse(resp.response['Location']) | |
res2 = Net::HTTP.start(url.host, url.port) {|http2| | |
f << http2.get(url.path).body | |
} | |
end | |
end | |
end | |
end | |
# Now use RMagick to bake some 16/32 images. | |
if File.exists?("#{RAILS_ROOT}/public/system/corporations/#{corporation_id.to_i}_64.png") | |
Magick::Image::read("#{RAILS_ROOT}/public/system/corporations/#{corporation_id.to_i}_64.png")[0].scale(16,16).write("#{RAILS_ROOT}/public/system/corporations/#{corporation_id.to_i}_16.png") | |
Magick::Image::read("#{RAILS_ROOT}/public/system/corporations/#{corporation_id.to_i}_64.png")[0].scale(32,32).write("#{RAILS_ROOT}/public/system/corporations/#{corporation_id.to_i}_32.png") | |
end | |
end | |
def errors | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment