lightning-source 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3145b2168e60b61c0e2b8565498042dcf9624eeb
4
+ data.tar.gz: 65963bd238c41584925497255de155f4cff0af9c
5
+ SHA512:
6
+ metadata.gz: e4caefd7cfcb0cf1a1f24eff7c2fc85db1c87e159c94272b91a0843ebd4c39b71391f842d97fd9e137a68c30132f5d41ca97befe3eb78cf262c0ffd410bf897a
7
+ data.tar.gz: 30a7a54ffb8c689cc32f3222d8032c74e9436f470f6958ce7afd3067ede4312e114c58a7fb9ea7123865faf5091385218a21c76e7d1e69e2d92fbe4c9644a52c
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lightning-source.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Simon Cozens
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Lightning::Source
2
+
3
+ Ruby interface to the Lightning Source on-demand publishers' web site
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'lightning-source'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install lightning-source
18
+
19
+ ## Usage
20
+
21
+ At present, all the module does is log in and grab sales figures for
22
+ a given time period:
23
+
24
+ first = Date.today.ago(1.month).beginning_of_month.to_date
25
+ last = first.end_of_month
26
+
27
+ l = LightningSource.new(username, password)
28
+ us_comp = l.compensation(first: first, last: last, market: "US")
29
+ uk_comp = l.compensation(first: first, last: last, market: "UK")
30
+
31
+ This returns an array of hashes like so:
32
+
33
+ [{:ISBN=>"9781234567890",
34
+ :Title=>"A Book about Fish",
35
+ :Author=>"Author, Joe",
36
+ :ListPrice=>12.99,
37
+ :Disc=>0.2,
38
+ :WholesalePrice=>10.39,
39
+ :QtySold=>2.0,
40
+ :QtyReturn=>0.0,
41
+ :NetQty=>2.0,
42
+ :Sales=>20.78,
43
+ :Returns=>0.0,
44
+ :NetSales=>20.78,
45
+ :PrintCharge=>-5.34,
46
+ :SetupRecovery=>0.0,
47
+ :Adjust=>0.0,
48
+ :ReturnCharge=>0.0,
49
+ :NetPubComp=>15.44,
50
+ :RecoveryRemaining=>0.0},
51
+ ]
52
+
53
+ Soon it'll grab order and book statuses as well, and it may even allow
54
+ you to make web orders.
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
63
+ 6. Be nice - this is my first gem!
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,66 @@
1
+ require "mechanize"
2
+
3
+ class LightningSource
4
+ exceptions = %w[ Login Internal ]
5
+
6
+ exceptions.each { |e| const_set(e+"Error", Class.new(StandardError)) }
7
+
8
+ VERSION = "1.0"
9
+ attr_accessor :agent
10
+
11
+ def initialize(login, password)
12
+ @agent = Mechanize.new
13
+ @agent.user_agent_alias = 'Mac Safari'
14
+ login_page = @agent.get("https://www.lightningsource.com/LSISecure/Login.aspx")
15
+ f = login_page.form
16
+ f.field_with(:name => "login:txtLogin").value = login
17
+ f.field_with(:name => "login:txtPassword").value = password
18
+ page = agent.submit(f,f.buttons.first)
19
+ if page.search(".formError").count > 0
20
+ raise LightningSource::LoginError
21
+ end
22
+ if !@agent.cookies.find {|x| x.name == "LSISecureSessionID" }
23
+ raise LightningSource::InternalError
24
+ end
25
+ end
26
+
27
+ def compensation(options = {})
28
+ market = options[:market] == "UK" ? 1 : 0
29
+ currency = market == 0 ? 2 : 0
30
+ page = @agent.get("https://www.lightningsource.com/LSISecure/FinancialReports/PubCompReportCriteria.aspx")
31
+ f = page.form
32
+ f.field_with(:name => "_ctl0:MainContent:PeriodEntry:txtDate1").value = options[:first].strftime("%d-%b-%y")
33
+ f.field_with(:name => "_ctl0:MainContent:PeriodEntry:txtDate2").value = options[:last].strftime("%d-%b-%y")
34
+ f.checkbox_with(:name => "_ctl0:MainContent:optOrgID:"+market.to_s).check
35
+ f.checkbox_with(:name => "_ctl0:MainContent:optCurrency:"+currency.to_s).check
36
+ f.checkbox_with(:name => "_ctl0:MainContent:optCompensationType:0").check
37
+ report = agent.submit(f, f.buttons.first)
38
+ # Do some checks here
39
+ table = reformat_table(report)
40
+ table.shift
41
+ headers = table.shift
42
+ # Convert to an array of hashes
43
+ table.map {|row|
44
+ h = Hash[*headers.map{|x|x.gsub(/\s+/,"").to_sym}.zip(row).flatten]
45
+ nh = {}
46
+ h.each do |k,v|
47
+ if k == :ISBN or k == :Title or k == :Author
48
+ nh[k] = v
49
+ elsif k == :Disc
50
+ nh[k] = v.to_f/100
51
+ else
52
+ nh[k] = v.to_f
53
+ end
54
+ end
55
+ nh
56
+ }
57
+ end
58
+
59
+ private
60
+
61
+ def reformat_table(page)
62
+ return page.search("tr").map {|t|
63
+ t.search("td").map{|x| x.content}
64
+ }
65
+ end
66
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lightning-source'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lightning-source"
8
+ spec.version = LightningSource::VERSION
9
+ spec.authors = ["Simon Cozens"]
10
+ spec.email = ["simon@simon-cozens.org"]
11
+ spec.description = "Ruby interface to the Lightning Source on-demand publishers' web site"
12
+ spec.summary = "Provide book sales data, make orders etc. for Lightning Source publishers"
13
+ spec.homepage = "https://github.com/simoncozens/lightning-source"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "mechanize"
24
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightning-source
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Simon Cozens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mechanize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Ruby interface to the Lightning Source on-demand publishers' web site
56
+ email:
57
+ - simon@simon-cozens.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/lightning-source.rb
68
+ - lightning-source.gemspec
69
+ homepage: https://github.com/simoncozens/lightning-source
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.0
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Provide book sales data, make orders etc. for Lightning Source publishers
93
+ test_files: []