Class: LightningSource

Inherits:
Object
  • Object
show all
Defined in:
lib/lightning-source.rb

Constant Summary collapse

VERSION =
"1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login, password) ⇒ LightningSource



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lightning-source.rb', line 11

def initialize(, password)
	@agent = Mechanize.new
	@agent.user_agent_alias = 'Mac Safari'
	 = @agent.get("https://www.lightningsource.com/LSISecure/Login.aspx")
	f = .form
	f.field_with(:name => "login:txtLogin").value = 
	f.field_with(:name => "login:txtPassword").value = password
	page = agent.submit(f,f.buttons.first)
	if page.search(".formError").count > 0
		raise LightningSource::LoginError
	end
	if !@agent.cookies.find {|x| x.name == "LSISecureSessionID" }
		raise LightningSource::InternalError
	end
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



9
10
11
# File 'lib/lightning-source.rb', line 9

def agent
  @agent
end

Instance Method Details

#compensation(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lightning-source.rb', line 27

def compensation(options = {})
  markets = { "US" => { :ou => 0, :currency => 3 },
              "UK" => { :ou => 1, :currency => 1 },
              "AU" => { :ou => 2, :currency => 0 } }
  if (!markets.has_key?(options[:market]))
      throw "Unknown market "+options[:market]+" (:market should be "+markets.keys.join("/")+")"
  end
	market = markets[options[:market]]
	page = @agent.get("https://www.lightningsource.com/LSISecure/FinancialReports/PubCompReportCriteria.aspx")
	f = page.form
		f.field_with(:name => /txtDate1/).value = options[:first].strftime("%d-%b-%y")
		f.field_with(:name => /txtDate2/).value = options[:last].strftime("%d-%b-%y")
		f.checkbox_with(:name => Regexp.new("optOrgID:"+market[:ou].to_s)).check
		f.checkbox_with(:name => Regexp.new("optCurrency:"+market[:currency].to_s)).check
		f.checkbox_with(:name => /optCompensationType:0/).check
	report = agent.submit(f, f.buttons.first)
	# Do some checks here
	table = reformat_table(report)
	table.shift
	headers = table.shift
	# Convert to an array of hashes
	table.map {|row|
		h = Hash[*headers.map{|x|x.gsub(/\s+/,"").to_sym}.zip(row).flatten]
    nh = {}
    h.each do |k,v|
      if k == :ISBN or k == :Title or k == :Author
        nh[k] = v
      elsif k == :Disc
        nh[k] = v.to_f/100
      else
        nh[k] = v.to_f
      end
    end
    nh
  }
end