Class: LightningSource
- Inherits:
-
Object
- Object
- LightningSource
- Defined in:
- lib/lightning-source.rb
Constant Summary collapse
- VERSION =
"1.2"
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
Instance Method Summary collapse
- #compensation(options = {}) ⇒ Object
-
#initialize(login, password) ⇒ LightningSource
constructor
A new instance of LightningSource.
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(login, password) @agent = Mechanize.new @agent.user_agent_alias = 'Mac Safari' login_page = @agent.get("https://www.lightningsource.com/LSISecure/Login.aspx") f = login_page.form f.field_with(:name => "login:txtLogin").value = login f.field_with(:name => "login:txtPassword").value = password page = agent.submit(f,f..first) if page.search(".formError").count > 0 raise LightningSource::LoginError end if !@agent..find {|x| x.name == "LSISecureSessionID" } raise LightningSource::InternalError end end |
Instance Attribute Details
#agent ⇒ Object
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( = {}) markets = { "US" => { :ou => 0, :currency => 3 }, "UK" => { :ou => 1, :currency => 1 }, "AU" => { :ou => 2, :currency => 0 } } if (!markets.has_key?([:market])) throw "Unknown market "+[:market]+" (:market should be "+markets.keys.join("/")+")" end market = markets[[:market]] page = @agent.get("https://www.lightningsource.com/LSISecure/FinancialReports/PubCompReportCriteria.aspx") f = page.form f.field_with(:name => /txtDate1/).value = [:first].strftime("%d-%b-%y") f.field_with(:name => /txtDate2/).value = [: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..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 |