<commit_before>#--
# Code copyright <NAME>, 2007-2010. www.crudvision.com
# Contributors at: http://github.com/lisa/reve/contributors
# This library is licensed under the terms of the MIT license. For full text
# see the LICENSE file distributed with this package.
# (Also, send Raquel Smith some ISK if you would like to show appreciation ;-)
#++

begin
  require 'hpricot'
rescue LoadError
  require 'rubygems'
  require 'hpricot'
end
require 'net/http'
require 'uri'
require 'cgi'
require 'digest'
require 'fileutils'

$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'reve/exceptions'
require 'reve/extensions'
require 'reve/classes'


module Reve
  # API Class.
  # Basic Usage:
  # api = Reve::API.new('my_UserID', 'my_apiKey')
  # alliances = api.alliances # Returns an array of Reve::Classes::Alliance
  #
  # api.personal_wallet_blanace(:characterid => 892008733) # Returns an array of
  # Reve::Classes::WalletBalance. Note that the CharacterID Number is required
  # here.
  #
  # api.sovereignty :just_hash => true # Returns the hash for this call with no
  # Alliance data with it.
  #
  # As of Revision 22 (28 August 2007) all API calls take a parameter, 
  # :just_hash, to just get the hash that represents that particular API call;
  # No data related to the call is returned if :just_hash is present
  # 
  # All API methods have the functionality to read XML from an arbitrary location. This could be another webserver, or a XML file on disk.
  # To use this pass the hash option :url => +location+ where +location+ is a String or URI class. See format_url_request documentation for more details.
  class API

    @@alliances_url                = 'http://api.eve-online.com/eve/AllianceList.xml.aspx'
    @@sovereignty_url              = 'http://api.eve-online.com/map/Sovereignty.xml.aspx'
    @@reftypes_url                 = 'http://api.eve-online.com/eve/RefTypes.xml.aspx'
    @@skill_tree_url               = 'http://api.eve-online.com/eve/SkillTree.xml.aspx'
    @@member_tracking_url          = 'http://api.eve-online.com/corp/MemberTracking.xml.aspx'
    @@corporate_wallet_balance_url = 'http://api.eve-online.com/corp/AccountBalance.xml.aspx'
    @@personal_wallet_balance_url  = 'http://api.eve-online.com/char/AccountBalance.xml.aspx'
    @@corporate_wallet_trans_url   = 'http://api.eve-online.com/corp/WalletTransactions.xml.aspx'
    @@personal_wallet_trans_url    = 'http://api.eve-online.com/char/WalletTransactions.xml.aspx'
    @@corporate_wallet_journal_url = 'http://api.eve-online.com/corp/WalletJournal.xml.aspx'
    @@personal_wallet_journal_url  = 'http://api.eve-online.com/char/WalletJournal.xml.aspx'
    @@characters_url               = 'http://api.eve-online.com/account/Characters.xml.aspx'
    @@training_skill_url           = 'http://api.eve-online.com/char/SkillInTraining.xml.aspx'
    @@skill_queue_url              = 'http://api.eve-online.com/char/SkillQueue.xml.aspx'
    @@character_sheet_url          = 'http://api.eve-online.com/char/CharacterSheet.xml.aspx'
    @@starbases_url                = 'http://api.eve-online.com/corp/StarbaseList.xml.aspx'
    @@starbasedetail_url           = 'http://api.eve-online.com/corp/StarbaseDetail.xml.aspx'
    @@conqurable_outposts_url      = 'http://api.eve-online.com/eve/ConquerableStationList.xml.aspx'
    @@corporation_sheet_url        = 'http://api.eve-online.com/corp/CorporationSheet.xml.aspx'
    @@corporation_member_security_url = 'http://api.eve-online.com/corp/MemberSecurity.xml.aspx'
    @@errors_url                   = 'http://api.eve-online.com/eve/ErrorList.xml.aspx'
    @@map_jumps_url                = 'http://api.eve-online.com/map/Jumps.xml.aspx'
    @@map_kills_url                = 'http://api.eve-online.com/map/Kills.xml.aspx'
    @@personal_market_orders_url   = 'http://api.eve-online.com/char/MarketOrders.xml.aspx'
    @@corporate_market_orders_url  = 'http://api.eve-online.com/corp/MarketOrders.xml.aspx'
    @@personal_industry_jobs_url   = 'http://api.eve-online.com/char/IndustryJobs.xml.aspx'
    @@corporate_industry_jobs_url  = 'http://api.eve-online.com/corp/IndustryJobs.xml.aspx'
    @@personal_assets_url          = 'http://api.eve-online.com/char/AssetList.xml.aspx'
    @@corporate_assets_url         = 'http://api.eve-online.com/corp/AssetList.xml.aspx'
    @@personal_kills_url           = 'http://api.eve-online.com/char/KillLog.xml.aspx'
    @@corporate_kills_url          = 'http://api.eve-online.com/corp/KillLog.xml.aspx'
    @@character_id_url             = 'http://api.eve-online.com/eve/CharacterID.xml.aspx'   # ?names=CCP%20Garthagk,Raquel%20Smith
    @@character_name_url           = 'http://api.eve-online.com/eve/CharacterName.xml.aspx' # ?ids=797400947,892008733
    @@personal_faction_war_stats_url= 'http://api.eve-online.com/char/FacWarStats.xml.aspx'
    @@corporate_faction_war_stats_url= 'http://api.eve-online.com/corp/FacWarStats.xml.aspx'
    @@general_faction_war_stats_url= 'http://api.eve-online.com/eve/FacWarStats.xml.aspx'
    @@top_faction_war_stats_url    = 'http://api.eve-online.com/eve/FacWarTopStats.xml.aspx'
    @@faction_war_occupancy_url    = 'http://api.eve-online.com/map/FacWarSystems.xml.aspx'
    @@certificate_tree_url         = 'http://api.eve-online.com/eve/CertificateTree.xml.aspx'
    @@character_medals_url         = 'http://api.eve-online.com/char/Medals.xml.aspx'
    @@corporate_medals_url         = 'http://api.eve-online.com/corp/Medals.xml.aspx'
    @@corp_member_medals_url       = 'http://api.eve-online.com/corp/MemberMedals.xml.aspx'
    @@server_status_url            = 'http://api.eve-online.com/Server/ServerStatus.xml.aspx'
    @@research_url                 = 'http://api.eve-online.com/char/Research.xml.aspx'
    @@personal_notification_url = 'http://api.eve-online.com/char/Notifications.xml.aspx'
    @@personal_mailing_lists_url = 'http://api.eve-online.com/char/mailinglists.xml.aspx'
    @@personal_mail_messages_url = 'http://api.eve-online.com/char/MailMessages.xml.aspx'
    
    cattr_accessor :character_sheet_url, :training_skill_url, :characters_url, :personal_wallet_journal_url,
                   :corporate_wallet_journal_url, :personal_wallet_trans_url, :corporate_wallet_trans_url,
                   :personal_wallet_balance_url, :corporate_wallet_balance_url, :member_tracking_url,
                   :skill_tree_url, :reftypes_url, :sovereignty_url, :alliances_url, :starbases_url,
                   :starbasedetail_url, :conqurable_outposts_url, :corporation_sheet_url, :map_jumps_url,
                   :map_kills_url, :personal_market_orders_url, :corporate_market_orders_url,
                   :personal_industry_jobs_url, :corporate_industry_jobs_url, :personal_assets_url,
                   :corporate_assets_url, :personal_kills_url, :corporate_kills_url,
                   :personal_faction_war_stats_url, :corporate_faction_war_stats_url,
                   :general_faction_war_stats_url, :top_faction_war_stats_url, :faction_war_occupancy_url,
                   :certificate_tree_url, :character_medals_url, :corporate_medals_url, 
                   :corp_member_medals_url, :server_status_url, :skill_queue_url, :corporation_member_security_url,
                   :personal_notification_url, :personal_mailing_lists_url, :personal_mail_messages_url,
                   :research_url


    attr_accessor :key, :userid, :charid
    attr_accessor :http_user_agent, :save_path, :timeout
    attr_reader :current_time, :cached_until, :last_hash, :reve_version
    
    # Create a new API instance.
    # current_time and cached_until are meaningful only for the LAST call made.
    # Expects:
    # * userid ( Integer | String ) - Your API userID
    # * key ( String ) - Your API key (Full key or restricted key)
    # * charid ( Integer | String ) - Default characterID for calls requiring it.
    # NOTE: All values passed to the constructor are typecasted to a String for safety.
    def initialize(userid = "", key = "", charid = "")
      @userid = (userid || "").to_s
      @key    = (key    || "").to_s
      @charid = (charid || "").to_s
      
      @save_path = nil
      
      @max_tries = 3
      @timeout = 20

      @current_time = nil
      @cached_until = nil
      @last_hash = nil
      @reve_version = File.read(File.join(File.dirname(__FILE__),'../','VERSION'))
      @http_user_agent = "Reve v#{@reve_version}; http://github.com/lisa/reve"
    end
    # Save XML to this directory with the format:
    # :save_path/:userid/:method/:expires_at_in_unixtime.xml
    # eg: ./xml/12345/characters/1200228878.xml
    # or: ./xml/alliances/1200228878.xml
    # If @save_path is nil then XML is not saved.
    def save_path=(p)
      @save_path = p
    end
    
    # Get the server status of Tranquility as a Reve::Classes::ServerStatus 
    # object.
    # See Also: Reve::Classes::ServerStatus
    def server_status(opts = {})
      args = postfields(opts)
      h = compute_hash(  opts.merge(:url => @@server_status_url) )
      return h if h
      xml = process_query(nil,opts[:url] || @@server_status_url,true,opts)
      Reve::Classes::ServerStatus.new(
        xml.search("/eveapi/result/serverOpen/").first.to_s,
        xml.search("/eveapi/result/onlinePlayers/").first.to_s
      )
    end
    
    # Convert a list of names to their ids.
    # Expects a Hash as a parameter with these keys:
    # * names ( Array ) - An Array of Names to fetch the IDs of.<commit_msg>Fix a bug where the version wasn't chomping off the \n implicit in the VERSION file which caused a problem with the user agent string.<commit_after>#--
# Code copyright <NAME>, 2007-2010. www.crudvision.com
# Contributors at: http://github.com/lisa/reve/contributors
# This library is licensed under the terms of the MIT license. For full text
# see the LICENSE file distributed with this package.
# (Also, send Raquel Smith some ISK if you would like to show appreciation ;-)
#++

begin
  require 'hpricot'
rescue LoadError
  require 'rubygems'
  require 'hpricot'
end
require 'net/http'
require 'uri'
require 'cgi'
require 'digest'
require 'fileutils'

$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'reve/exceptions'
require 'reve/extensions'
require 'reve/classes'


module Reve
  # API Class.
  # Basic Usage:
  # api = Reve::API.new('my_UserID', 'my_apiKey')
  # alliances = api.alliances # Returns an array of Reve::Classes::Alliance
  #
  # api.personal_wallet_blanace(:characterid => 892008733) # Returns an array of
  # Reve::Classes::WalletBalance. Note that the CharacterID Number is required
  # here.
  #
  # api.sovereignty :just_hash => true # Returns the hash for this call with no
  # Alliance data with it.
  #
  # As of Revision 22 (28 August 2007) all API calls take a parameter, 
  # :just_hash, to just get the hash that represents that particular API call;
  # No data related to the call is returned if :just_hash is present
  # 
  # All API methods have the functionality to read XML from an arbitrary location. This could be another webserver, or a XML file on disk.
  # To use this pass the hash option :url => +location+ where +location+ is a String or URI class. See format_url_request documentation for more details.
  class API

    @@alliances_url                = 'http://api.eve-online.com/eve/AllianceList.xml.aspx'
    @@sovereignty_url              = 'http://api.eve-online.com/map/Sovereignty.xml.aspx'
    @@reftypes_url                 = 'http://api.eve-online.com/eve/RefTypes.xml.aspx'
    @@skill_tree_url               = 'http://api.eve-online.com/eve/SkillTree.xml.aspx'
    @@member_tracking_url          = 'http://api.eve-online.com/corp/MemberTracking.xml.aspx'
    @@corporate_wallet_balance_url = 'http://api.eve-online.com/corp/AccountBalance.xml.aspx'
    @@personal_wallet_balance_url  = 'http://api.eve-online.com/char/AccountBalance.xml.aspx'
    @@corporate_wallet_trans_url   = 'http://api.eve-online.com/corp/WalletTransactions.xml.aspx'
    @@personal_wallet_trans_url    = 'http://api.eve-online.com/char/WalletTransactions.xml.aspx'
    @@corporate_wallet_journal_url = 'http://api.eve-online.com/corp/WalletJournal.xml.aspx'
    @@personal_wallet_journal_url  = 'http://api.eve-online.com/char/WalletJournal.xml.aspx'
    @@characters_url               = 'http://api.eve-online.com/account/Characters.xml.aspx'
    @@training_skill_url           = 'http://api.eve-online.com/char/SkillInTraining.xml.aspx'
    @@skill_queue_url              = 'http://api.eve-online.com/char/SkillQueue.xml.aspx'
    @@character_sheet_url          = 'http://api.eve-online.com/char/CharacterSheet.xml.aspx'
    @@starbases_url                = 'http://api.eve-online.com/corp/StarbaseList.xml.aspx'
    @@starbasedetail_url           = 'http://api.eve-online.com/corp/StarbaseDetail.xml.aspx'
    @@conqurable_outposts_url      = 'http://api.eve-online.com/eve/ConquerableStationList.xml.aspx'
    @@corporation_sheet_url        = 'http://api.eve-online.com/corp/CorporationSheet.xml.aspx'
    @@corporation_member_security_url = 'http://api.eve-online.com/corp/MemberSecurity.xml.aspx'
    @@errors_url                   = 'http://api.eve-online.com/eve/ErrorList.xml.aspx'
    @@map_jumps_url                = 'http://api.eve-online.com/map/Jumps.xml.aspx'
    @@map_kills_url                = 'http://api.eve-online.com/map/Kills.xml.aspx'
    @@personal_market_orders_url   = 'http://api.eve-online.com/char/MarketOrders.xml.aspx'
    @@corporate_market_orders_url  = 'http://api.eve-online.com/corp/MarketOrders.xml.aspx'
    @@personal_industry_jobs_url   = 'http://api.eve-online.com/char/IndustryJobs.xml.aspx'
    @@corporate_industry_jobs_url  = 'http://api.eve-online.com/corp/IndustryJobs.xml.aspx'
    @@personal_assets_url          = 'http://api.eve-online.com/char/AssetList.xml.aspx'
    @@corporate_assets_url         = 'http://api.eve-online.com/corp/AssetList.xml.aspx'
    @@personal_kills_url           = 'http://api.eve-online.com/char/KillLog.xml.aspx'
    @@corporate_kills_url          = 'http://api.eve-online.com/corp/KillLog.xml.aspx'
    @@character_id_url             = 'http://api.eve-online.com/eve/CharacterID.xml.aspx'   # ?names=CCP%20Garthagk,Raquel%20Smith
    @@character_name_url           = 'http://api.eve-online.com/eve/CharacterName.xml.aspx' # ?ids=797400947,892008733
    @@personal_faction_war_stats_url= 'http://api.eve-online.com/char/FacWarStats.xml.aspx'
    @@corporate_faction_war_stats_url= 'http://api.eve-online.com/corp/FacWarStats.xml.aspx'
    @@general_faction_war_stats_url= 'http://api.eve-online.com/eve/FacWarStats.xml.aspx'
    @@top_faction_war_stats_url    = 'http://api.eve-online.com/eve/FacWarTopStats.xml.aspx'
    @@faction_war_occupancy_url    = 'http://api.eve-online.com/map/FacWarSystems.xml.aspx'
    @@certificate_tree_url         = 'http://api.eve-online.com/eve/CertificateTree.xml.aspx'
    @@character_medals_url         = 'http://api.eve-online.com/char/Medals.xml.aspx'
    @@corporate_medals_url         = 'http://api.eve-online.com/corp/Medals.xml.aspx'
    @@corp_member_medals_url       = 'http://api.eve-online.com/corp/MemberMedals.xml.aspx'
    @@server_status_url            = 'http://api.eve-online.com/Server/ServerStatus.xml.aspx'
    @@research_url                 = 'http://api.eve-online.com/char/Research.xml.aspx'
    @@personal_notification_url = 'http://api.eve-online.com/char/Notifications.xml.aspx'
    @@personal_mailing_lists_url = 'http://api.eve-online.com/char/mailinglists.xml.aspx'
    @@personal_mail_messages_url = 'http://api.eve-online.com/char/MailMessages.xml.aspx'
    
    cattr_accessor :character_sheet_url, :training_skill_url, :characters_url, :personal_wallet_journal_url,
                   :corporate_wallet_journal_url, :personal_wallet_trans_url, :corporate_wallet_trans_url,
                   :personal_wallet_balance_url, :corporate_wallet_balance_url, :member_tracking_url,
                   :skill_tree_url, :reftypes_url, :sovereignty_url, :alliances_url, :starbases_url,
                   :starbasedetail_url, :conqurable_outposts_url, :corporation_sheet_url, :map_jumps_url,
                   :map_kills_url, :personal_market_orders_url, :corporate_market_orders_url,
                   :personal_industry_jobs_url, :corporate_industry_jobs_url, :personal_assets_url,
                   :corporate_assets_url, :personal_kills_url, :corporate_kills_url,
                   :personal_faction_war_stats_url, :corporate_faction_war_stats_url,
                   :general_faction_war_stats_url, :top_faction_war_stats_url, :faction_war_occupancy_url,
                   :certificate_tree_url, :character_medals_url, :corporate_medals_url, 
                   :corp_member_medals_url, :server_status_url, :skill_queue_url, :corporation_member_security_url,
                   :personal_notification_url, :personal_mailing_lists_url, :personal_mail_messages_url,
                   :research_url


    attr_accessor :key, :userid, :charid
    attr_accessor :http_user_agent, :save_path, :timeout
    attr_reader :current_time, :cached_until, :last_hash, :reve_version
    
    # Create a new API instance.
    # current_time and cached_until are meaningful only for the LAST call made.
    # Expects:
    # * userid ( Integer | String ) - Your API userID
    # * key ( String ) - Your API key (Full key or restricted key)
    # * charid ( Integer | String ) - Default characterID for calls requiring it.
    # NOTE: All values passed to the constructor are typecasted to a String for safety.
    def initialize(userid = "", key = "", charid = "")
      @userid = (userid || "").to_s
      @key    = (key    || "").to_s
      @charid = (charid || "").to_s
      
      @save_path = nil
      
      @max_tries = 3
      @timeout = 20

      @current_time = nil
      @cached_until = nil
      @last_hash = nil
      @reve_version = File.read(File.join(File.dirname(__FILE__),'../','VERSION')).chomp
      @http_user_agent = "Reve v#{@reve_version}; http://github.com/lisa/reve"
    end
    # Save XML to this directory with the format:
    # :save_path/:userid/:method/:expires_at_in_unixtime.xml
    # eg: ./xml/12345/characters/1200228878.xml
    # or: ./xml/alliances/1200228878.xml
    # If @save_path is nil then XML is not saved.
    def save_path=(p)
      @save_path = p
    end
    
    # Get the server status of Tranquility as a Reve::Classes::ServerStatus 
    # object.
    # See Also: Reve::Classes::ServerStatus
    def server_status(opts = {})
      args = postfields(opts)
      h = compute_hash(  opts.merge(:url => @@server_status_url) )
      return h if h
      xml = process_query(nil,opts[:url] || @@server_status_url,true,opts)
      Reve::Classes::ServerStatus.new(
        xml.search("/eveapi/result/serverOpen/").first.to_s,
        xml.search("/eveapi/result/onlinePlayers/").first.to_s
      )
    end
    
    # Convert a list of names to their ids.
    # Expects a Hash as a parameter with these keys:
    # * names ( Array ) - An Array of Names to fetch the IDs of.
{'fraction_non_alphanumeric': 0.12750455373406194, 'fraction_numerical': 0.0078064012490242, 'mean_word_length': 3.1503239740820734, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 110}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7002647', 'n_tokens_mistral': 6828, 'n_tokens_neox': 6366, 'n_words': 1093}