Skip to content
This repository was archived by the owner on May 9, 2018. It is now read-only.

Switch over to using Nokogiri instead of hpricot #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
source 'https://rubygems.org'

group :deployment do
gem 'hpricot'
gem 'nokogiri'
gem 'simplecov', :require => false #code coverage thingy
end

group :test, :development do
gem 'hpricot'
gem 'nokogiri'
gem 'simplecov', :require => false #code coverage thingy
end

9 changes: 7 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ GEM
remote: https://rubygems.org/
specs:
docile (1.1.1)
hpricot (0.8.6)
mini_portile2 (2.0.0)
multi_json (1.8.2)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
simplecov (0.8.2)
docile (~> 1.1.0)
multi_json
Expand All @@ -14,5 +16,8 @@ PLATFORMS
ruby

DEPENDENCIES
hpricot
nokogiri
simplecov

BUNDLED WITH
1.11.2
114 changes: 57 additions & 57 deletions lib/reve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#++

begin
require 'hpricot'
require 'nokogiri'
rescue LoadError
require 'rubygems'
require 'hpricot'
require 'nokogiri'
rescue LoadError
require 'bundler'
require 'nokogiri'
end
require 'net/https'
require 'uri'
Expand Down Expand Up @@ -176,8 +179,8 @@ def server_status(opts = {})
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
xml.search("/eveapi/result/serverOpen").first.text,
xml.search("/eveapi/result/onlinePlayers").first.text
)
end

Expand Down Expand Up @@ -359,29 +362,25 @@ def skill_tree(opts = {})
return h if h
doc = process_query(nil,opts[:url] || @@skill_tree_url,true)
skills = []
(doc/'rowset[@name=skills]/row').each do |skill|
(doc/"rowset[@name='skills']/row").each do |skill|
name = skill['typeName']
type_id = skill['typeID']
group_id = skill['groupID']
rank = (skill/:rank).inner_html
desc = (skill/:description).inner_html
rank = skill.search('rank').text
desc = skill.search('description').text
required_skills = []
reqs = (skill/'rowset@name=[requiredskills]/row')
reqs = skill.search('rowset[@name=requiredSkills]/row')
reqs.each do |required|
next if required.kind_of? Hpricot::Text # why is this needed? Why is this returned? How can I only get stuff with typeid and skilllevel?
required_skills << Reve::Classes::SkillRequirement.new(required) if required['typeID'] && required['skillLevel']
end
required_attribs = []
(skill/'requiredAttributes').each do |req|
pri = doc.at(req.xpath + "/primaryAttribute")
sec = doc.at(req.xpath + "/secondaryAttribute")
required_attribs << Reve::Classes::PrimaryAttribute.new(pri.inner_html)
required_attribs << Reve::Classes::SecondaryAttribute.new(sec.inner_html)
end
pri = skill.search('requiredAttributes/primaryAttribute')
sec = skill.search('requiredAttributes/secondaryAttribute')
required_attribs << Reve::Classes::PrimaryAttribute.new(pri.text)
required_attribs << Reve::Classes::SecondaryAttribute.new(sec.text)
bonuses = []
res = (skill/'rowset@name=[skillBonusCollection]/row')
res = skill.search("rowset[@name=skillBonusCollection]/row")
res.each do |bonus|
next if bonus.kind_of? Hpricot::Text
bonuses << Reve::Classes::SkillBonus.new(bonus) if bonus['bonusType'] && bonus['bonusValue']
end
skills << Reve::Classes::SkillTree.new(name,type_id,group_id,desc,rank,required_attribs,required_skills,bonuses)
Expand Down Expand Up @@ -541,10 +540,10 @@ def character_medals(opts = { :characterid => nil })
h = compute_hash(args.merge(:url => @@character_medals_url))
return h if h
xml = process_query(nil,opts[:url] || @@character_medals_url,true,args)
current = xml.search("/eveapi/result/rowset[@name=currentCorporation]/row").inject([]) do |cur,elem|
current = xml.search("/eveapi/result/rowset[@name='currentCorporation']/row").inject([]) do |cur,elem|
cur << Reve::Classes::CharacterMedal.new(elem)
end
other = xml.search("/eveapi/result/rowset[@name=otherCorporations]/row").inject([]) do |cur,elem|
other = xml.search("/eveapi/result/rowset[@name='otherCorporations']/row").inject([]) do |cur,elem|
cur << Reve::Classes::CharacterMedal.new(elem)
end
Reve::Classes::CharacterMedals.new(current,other)
Expand All @@ -563,7 +562,7 @@ def personal_faction_war_stats(opts = { :characterid => nil })
[ :factionID, :factionName, :enlisted, :currentRank, :highestRank,
:killsYesterday, :killsLastWeek, :killsTotal, :victoryPointsYesterday,
:victoryPointsLastWeek, :victoryPointsTotal ].each do |elem|
elems[elem.to_s] = xml.search("/eveapi/result/" + elem.to_s).first.inner_html
elems[elem.to_s] = xml.at("/eveapi/result/#{elem.to_s}").text
end
Reve::Classes::PersonalFactionWarParticpant.new(elems)
end
Expand All @@ -581,7 +580,7 @@ def corporate_faction_war_stats(opts = { :characterid => nil })
[ :factionID, :factionName, :enlisted, :pilots,
:killsYesterday, :killsLastWeek, :killsTotal, :victoryPointsYesterday,
:victoryPointsLastWeek, :victoryPointsTotal ].each do |elem|
elems[elem.to_s] = xml.search("/eveapi/result/" + elem.to_s).first.inner_html
elems[elem.to_s] = xml.at("/eveapi/result/#{elem.to_s}").text
end
Reve::Classes::CorporateFactionWarParticpant.new(elems)
end
Expand All @@ -603,7 +602,7 @@ def faction_war_stats(opts = {} )
totals = {}
[ :killsYesterday, :killsLastWeek, :killsTotal, :victoryPointsYesterday,
:victoryPointsLastWeek, :victoryPointsTotal ].each do |elem|
totals[elem.to_s] = xml.search("/eveapi/result/totals/" + elem.to_s).first.inner_html
totals[elem.to_s] = xml.at("/eveapi/result/totals/#{elem.to_s}").text
end
Reve::Classes::EveFactionWarStat.new(totals, wars, participants)
end
Expand Down Expand Up @@ -689,7 +688,7 @@ def skill_in_training(opts = {:characterid => nil})
xml = process_query(nil,opts[:url] || @@training_skill_url,true,args)
xml.search("//result").each do |elem|
for field in [ 'currentTQTime', 'trainingEndTime','trainingStartTime','trainingTypeID','trainingStartSP','trainingDestinationSP','trainingToLevel','skillInTraining' ]
h[field] = (elem/field.intern).inner_html
h[field] = (elem/field.intern).text
end
end
Reve::Classes::SkillInTraining.new(h)
Expand Down Expand Up @@ -732,9 +731,9 @@ def starbase_details(opts = { :characterid => nil, :starbaseid => nil })
return h if h
xml = process_query(Reve::Classes::StarbaseDetails,opts[:url] || @@starbasedetail_url, true, args)

state = xml.search("/eveapi/result/state").inner_text
state_timestamp = xml.search("/eveapi/result/stateTimestamp").inner_text
online_timestamp = xml.search("/eveapi/result/onlineTimestamp").inner_text
state = xml.at("/eveapi/result/state").text
state_timestamp = xml.at("/eveapi/result/stateTimestamp").text
online_timestamp = xml.at("/eveapi/result/onlineTimestamp").text

h = {'usageFlags' => 0, 'deployFlags' => 0, 'allowCorporationMembers' => 0, 'allowAllianceMembers' => 0, 'claimSovereignty' => 0}
h.keys.each {|k| h[k] = xml.search("/eveapi/result/generalSettings/#{k}").inner_text }
Expand All @@ -751,7 +750,7 @@ def starbase_details(opts = { :characterid => nil, :starbaseid => nil })

res = Hash.new
{ :state => :state, :stateTimestamp => :state_timestamp, :onlineTimestamp => :online_timestamp }.each do |k,v|
res[v] = xml.search("/eveapi/result/#{k.to_s}/").first.to_s.strip
res[v] = xml.at("/eveapi/result/#{k.to_s}").text
end

Reve::Classes::StarbaseDetails.new res, general_settings, combat_settings, fuel
Expand Down Expand Up @@ -829,24 +828,25 @@ def corporation_sheet(opts = { :characterid => nil })
return h if h
xml = process_query(nil,opts[:url] || @@corporation_sheet_url,true,args)

h = { 'graphicid' => 0, 'shape1' => 0, 'shape2' => 0, 'shape3' => 0, 'color1' => 0, 'color2' => 0, 'color3' => 0, }
h.keys.each { |k| h[k] = xml.search("//result/logo/" + k + "/").to_s.to_i }
h = { 'graphicID' => 0, 'shape1' => 0, 'shape2' => 0, 'shape3' => 0, 'color1' => 0, 'color2' => 0, 'color3' => 0, }
h.keys.each { |k| h[k] = xml.search("/eveapi/result/logo/#{k}").text.to_i }
corporate_logo = Reve::Classes::CorporateLogo.new h

wallet_divisions = xml.search("//result/rowset[@name='walletDivisions']/").collect { |k| k if k.kind_of? Hpricot::Elem } - [ nil ]
divisions = xml.search("//result/rowset[@name='divisions']/").collect { |k| k if k.kind_of? Hpricot::Elem } - [ nil ]
# Take each wallet division out of the NodeSet so we're working with only the XML::Element
wallet_divisions = xml.search("/eveapi/result/rowset[@name='walletDivisions']/row").collect { |node| node }
divisions = xml.search("/eveapi/result/rowset[@name='divisions']/row").collect { |node| node }
divisions.collect! { |d| Reve::Classes::CorporateDivision.new(d) }
wallet_divisions.collect! { |w| Reve::Classes::WalletDivision.new(w) }

# Map the XML names to our own names and assign them to the temporary
# hash +res+ to pass to Reve::Classes::CorporationSheet#new
res = Hash.new
{ :corporationid => :id, :corporationname => :name, :ticker => :ticker, :ceoid => :ceo_id,
:ceoname => :ceo_name, :stationid => :station_id, :stationname => :station_name,
:description => :description, :url => :url, :allianceid => :alliance_id,
:alliancename => :alliance_name, :taxrate => :tax_rate, :membercount => :member_count,
:memberlimit => :member_limit, :shares => :shares }.each do |k,v|
res[v] = xml.search("//result/#{k.to_s}/").first.to_s.strip
{ :corporationID => :id, :corporationName => :name, :ticker => :ticker, :ceoID => :ceo_id,
:ceoName => :ceo_name, :stationID => :station_id, :stationName => :station_name,
:description => :description, :url => :url, :allianceID => :alliance_id,
:allianceName => :alliance_name, :taxRate => :tax_rate, :memberCount => :member_count,
:memberLimit => :member_limit, :shares => :shares }.each do |k,v|
res[v] = xml.at("/eveapi/result/#{k.to_s}").text.to_s.strip rescue nil
end

Reve::Classes::CorporationSheet.new res, divisions, wallet_divisions, corporate_logo
Expand All @@ -859,15 +859,15 @@ def corporate_member_security(opts = { :characterid => nil })
xml = process_query(nil,opts[:url] || @@corporation_member_security_url,true,args)

cmc = Reve::Classes::CorporationMemberSecurity.new
xml.search("/eveapi/result/rowset[@name=members]/row").each do |member|
xml.search("/eveapi/result/rowset[@name='members']/row").each do |member|
mem = Reve::Classes::CorporationMember.new(member)
cmc.members << mem
[:roles, :grantableRoles, :rolesAtHQ, :grantableRolesAtHQ, :rolesAtBase, :grantableRolesAtBase, :rolesAtOther, :grantableRolesAtOther].each do |rowset|
member.search("/rowset[@name=#{rowset.to_s}]/row").each do |row|
member.search("/rowset[@name='#{rowset.to_s}']/row").each do |row|
mem.rsend(["#{rowset}"], [:push,Reve::Classes::CorporateRole.new(row)])
end
end
member.search("/rowset[@name=titles]/row").each do |row|
member.search("/rowset[@name='titles']/row").each do |row|
mem.rsend([:titles], [:push,Reve::Classes::CorporateTitle.new(row)])
end
end
Expand All @@ -885,16 +885,16 @@ def certificate_tree(opts = {})
xml = process_query(nil,opts[:url] || @@certificate_tree_url,true,args)

tree = Reve::Classes::CertificateTree.new
xml.search("/eveapi/result/rowset[@name=categories]/row").each do |category|
xml.search("/eveapi/result/rowset[@name='categories']/row").each do |category|
cat = Reve::Classes::CertificateCategory.new(category)
category.search("rowset[@name=classes]/row").each do |klass|
category.search("rowset[@name='classes']/row").each do |klass|
kl = Reve::Classes::CertificateClass.new(klass)
klass.search("rowset[@name=certificates]/row").each do |certificate|
klass.search("rowset[@name='certificates']/row").each do |certificate|
cert = Reve::Classes::Certificate.new(certificate)
certificate.search("rowset[@name=requiredSkills]/row").each do |skill|
certificate.search("rowset[@name='requiredSkills']/row").each do |skill|
cert.required_skills << Reve::Classes::CertificateRequiredSkill.new(skill)
end
certificate.search("rowset[@name=requiredCertificates]/row").each do |requiredcert|
certificate.search("rowset[@name='requiredCertificates']/row").each do |requiredcert|
cert.required_certificates << Reve::Classes::CertificateRequiredCertificate.new(requiredcert)
end
kl.certificates << cert
Expand Down Expand Up @@ -923,25 +923,25 @@ def character_sheet(opts = { :characterid => nil })
Reve::Classes::PerceptionEnhancer, Reve::Classes::WillpowerEnhancer
].each do |klass|
xml_attr = klass.to_s.split("::").last.sub("Enhancer",'').downcase + "Bonus"
i = klass.new(xml.search("/eveapi/result/attributeEnhancers/#{xml_attr}").search("augmentatorName/").first.to_s,
xml.search("/eveapi/result/attributeEnhancers/#{xml_attr}").search("augmentatorValue/").first.to_s.to_i)
i = klass.new(xml.search("/eveapi/result/attributeEnhancers/#{xml_attr}").search("augmentatorName").first.text,
xml.search("/eveapi/result/attributeEnhancers/#{xml_attr}").search("augmentatorValue").first.text.to_i)
cs.enhancers << i
end

[ 'characterID', 'name', 'race', 'bloodLine', 'ancestry', 'dob', 'gender','corporationName',
'corporationID','balance', 'cloneName', 'cloneSkillPoints'
].each do |field|
cs.send("#{field.downcase}=",xml.search("/eveapi/result/#{field}/").first.to_s)
cs.send("#{field.downcase}=",xml.search("/eveapi/result/#{field}").first.to_s)
end

[ 'intelligence','memory','charisma','perception','willpower' ].each do |attrib|
cs.send("#{attrib}=",xml.search("/eveapi/result/attributes/#{attrib}/").first.to_s.to_i)
cs.send("#{attrib}=",xml.search("/eveapi/result/attributes/#{attrib}").first.to_s.to_i)
end
xml.search("rowset[@name=skills]/row").each do |elem|
xml.search("rowset[@name='skills']/row").each do |elem|
cs.skills << Reve::Classes::Skill.new(elem)
end

xml.search("rowset[@name=certificates]/row").each do |elem|
xml.search("rowset[@name='certificates']/row").each do |elem|
cs.certificate_ids << elem['certificateID'].to_i
end
[ :corporationRolesAtHQ, :corporationRoles, :corporationRolesAtBase, :corporationRolesAtOther ].each do |role_kind|
Expand All @@ -950,7 +950,7 @@ def character_sheet(opts = { :characterid => nil })
end
end

xml.search("rowset[@name=corporationTitles]/row").each do |elem|
xml.search("rowset[@name='corporationTitles']/row").each do |elem|
cs.corporate_titles << Reve::Classes::CorporateTitle.new(elem)
end

Expand Down Expand Up @@ -1005,8 +1005,9 @@ def personal_mail_messages(opts = { :characterid => nil })
end

# Gets the bodies for mail messages. NB this API call does not
# return objects. It returns a hash with messageID strings as the keys -
# suitable for merging into a Reve::Classes::MailMessage object
# return objects. It returns a hash with messageID strings as the keys
# and message body as the key value. This hash is suitable for merging
# into a Reve::Classes::MailMessage object
#
# Note from the Eve API docs:
# Bodies cannot be accessed if you have not called for their headers recently.
Expand All @@ -1018,11 +1019,10 @@ def personal_mail_message_bodies(opts = { :ids => [] })
args = postfields(opts)
h = compute_hash(args.merge(:url => @@personal_mail_message_bodies_url))
return h if h
just_xml = true
xml = process_query(Reve::Classes::MailMessage, opts[:url] || @@personal_mail_message_bodies_url,just_xml,args)
xml = process_query(Reve::Classes::MailMessage, opts[:url] || @@personal_mail_message_bodies_url,true,args)
results = {}
xml.search("//rowset/row").each do |el|
results[el.attributes['messageID']] = el.inner_text
results[el.attributes['messageID'].value] = el.text
end
results
end
Expand Down
10 changes: 5 additions & 5 deletions lib/reve/classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ def initialize(h, divisions = [],wallet_divisions = [], logo =Reve::Classes::Cor
@alliance_name = h[:alliance_name] rescue nil
@tax_rate = h[:tax_rate].to_f
@member_count = h[:member_count].to_i
@member_limit = h[:member_limit].to_i
@member_limit = h[:member_limit].to_i rescue 0
@shares = h[:shares].to_i
end
end
Expand Down Expand Up @@ -1404,10 +1404,10 @@ def initialize(elem) #:nodoc:
class StarbaseCombatSettings
attr_reader :on_standings_drop, :on_status_drop, :on_aggression, :on_corporation_war
def initialize(elem) #:nodoc:
@on_standings_drop = elem['onStandingDrop'].attr('standing').to_i
@on_status_drop = (elem['onStatusDrop'].attr('enabled') == '1' ? elem['onStatusDrop'].attr('standing').to_i : false)
@on_aggression = elem['onAggression'].attr('enabled') == '1'
@on_corporation_war = elem['onCorporationWar'].attr('enabled') == '1'
@on_standings_drop = elem['onStandingDrop'].attr('standing').value.to_i
@on_status_drop = (elem['onStatusDrop'].attr('enabled').value == '1' ? elem['onStatusDrop'].attr('standing').value.to_i : false)
@on_aggression = elem['onAggression'].attr('enabled').value == '1'
@on_corporation_war = elem['onCorporationWar'].attr('enabled').value == '1'
end
end

Expand Down
10 changes: 6 additions & 4 deletions lib/reve/processing_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module ProcessingHelpers
def recur_through_assets(rows)
assets = []
rows.each do |container|
unless container.empty?
unless container.children.empty?
asset_container = Reve::Classes::AssetContainer.new(container)
asset_container.assets = self.recur_through_assets(container.search("/rowset/row"))
asset_container.assets = recur_through_assets(container.search("#{container.path}/rowset/row"))
assets << asset_container
else
assets << Reve::Classes::Asset.new(container)
Expand Down Expand Up @@ -163,7 +163,8 @@ def get_xml(source,opts)
# Raises the proper exception (if there is one), otherwise it returns the
# XML response.
def check_exception(xml)
x = Hpricot::XML(xml)
raise ArgumentError.new("Got a nil XML document. What happened?") unless xml
x = Nokogiri::XML(xml)
begin
out = x.search("//error") # If this fails then there are some big problems with Hpricot#search ?
rescue Exception => e
Expand All @@ -181,7 +182,8 @@ def check_exception(xml)
def save_xml(xml)
path = build_save_filename
FileUtils.mkdir_p(File.dirname(path))
File.open(path,'w') { |f| f.print xml.to_original_html }
File.open(path,'w') { |f| xml.write_xml_to f}
# File.open(path,'w') { |f| f.print xml.to_xml }
end

def build_save_filename
Expand Down
Loading