Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.81 KB | None | 0 0
  1. # == Schema Information
  2. #
  3. # Table name: api_keys
  4. #
  5. #  id                 :integer          not null, primary key
  6. #  verification_code  :string(255)
  7. #  user_id            :integer
  8. #  created_at         :datetime         not null
  9. #  updated_at         :datetime         not null
  10. #  eve_api_identifier :string(255)
  11. #
  12. require 'Nokogiri'
  13.  
  14. class ApiKey < ActiveRecord::Base
  15.   validates_presence_of :user_id, :verification_code, :eve_api_identifier
  16.  
  17.   belongs_to :user
  18.  
  19.   attr_accessible :id, :verification_code, :eve_api_identifier
  20.   attr_accessible :user_id
  21.  
  22.     def character_name
  23.       api = get_api_results_for("CharacterSheet")
  24.       character_name = get_character_name(api)
  25.     end
  26.  
  27.     def attributes
  28.       api = get_api_results_for("CharacterSheet")
  29.       attributes = get_attributes(api)
  30.     end
  31.  
  32.     def skill_in_training?
  33.       api = get_api_results_for("SkillInTraining")
  34.       is_skill_in_training(api)
  35.     end
  36.  
  37.     def name_of_skill_in_training
  38.       api = get_api_results_for("SkillInTraining")
  39.       if skill_in_training?
  40.         get_name_of_skill_in_training(api)
  41.       end
  42.     end
  43.  
  44.     private
  45.  
  46.         def get_api_results_for(specific_api)
  47.           api_reults = Nokogiri.XML(open("https://api.eveonline.com/char/#{specific_api}.xml.aspx?keyID=#{self.eve_api_identifier}&vCode=#{self.verification_code}"))    
  48.         end
  49.  
  50.         def get_character_name(api)
  51.           api.xpath("//name").inner_text
  52.         end
  53.  
  54.         def get_attributes(api)
  55.           api.at('attributes').children.each_with_object({}){ |o,h| h[o.name.to_sym] = o.text }
  56.         end
  57.  
  58.         def is_skill_in_training(api)
  59.           api.xpath("//skillInTraining").inner_text.to_bool
  60.         end
  61.  
  62.         def get_name_of_skill_in_training(api)
  63.           api.xpath("//trainingTypeID").inner_text
  64.         end
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement