bookscan_client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e80c696dea04315b45a82d625d144baea979194f
4
+ data.tar.gz: c2b7930302400b35f5d9c29efa08e0d55d8b69da
5
+ SHA512:
6
+ metadata.gz: 9d22b93bbfd8bdb6179a5a643b5d956550fb971cc94dd9907ca58a6f63f432dd484aad1ca52301915a5b5dbbc1a635b4d78826e64cb27cc12f1ad5199f05a997
7
+ data.tar.gz: c9acf428a24ad4b60fcf7c5b8b566dc789be28163b93d1723dfd4e6913d7b05ef064f7bfaf1836787c5d364a67be18c43dc559751defa4b147de5031c9274179
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bookscan_client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
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,39 @@
1
+ # BookscanClient
2
+
3
+ Unofficial bookscan client library for ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bookscan_client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bookscan_client
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "bookscan_client"
25
+
26
+ client = BookscanClient.new
27
+ client.login("user@example.com", "password")
28
+
29
+ books = client.books
30
+ ...
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/hogelog/bookscan-client-ruby/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = "test/*_test.rb"
7
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bookscan_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bookscan_client"
8
+ spec.version = BookscanClient::VERSION
9
+ spec.authors = ["hogelog"]
10
+ spec.email = ["konbu.komuro@gmail.com"]
11
+ spec.summary = %q{Unofficial bookscan client.}
12
+ spec.description = %q{Unofficial bookscan client library for ruby}
13
+ spec.homepage = "https://github.com/hogelog/bookscan-client-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_dependency "mechanize", "2.7.2"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "webmock"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "byebug"
28
+ spec.add_development_dependency "pry-byebug"
29
+ spec.add_development_dependency "minitest"
30
+ end
@@ -0,0 +1,32 @@
1
+ class BookscanClient
2
+ module Model
3
+ class Book
4
+ attr_accessor :filename, :hash, :digest, :image_url
5
+
6
+ def initialize(filename: nil, hash: nil, digest: nil, image_url: nil)
7
+ @filename = filename
8
+ @hash = hash
9
+ @digest = digest
10
+ @image_url = image_url
11
+ end
12
+
13
+ def self.parse_books(page)
14
+ page.search("#sortable_box .showbook").map{|book_link|
15
+ url = "#{BookscanClient::URL::ROOT}#{book_link.attr("href")}"
16
+ params = CGI.parse(URI.parse(url).query)
17
+ next unless %w[f h d].all?{|key| params.keys.include?(key) }
18
+ image = book_link.search("img")
19
+
20
+ book = Book.new(filename: params["f"][0], hash: params["h"][0], digest: params["d"][0])
21
+ book.image_url = image[0].attr("data-original") unless image.empty?
22
+
23
+ book
24
+ }
25
+ end
26
+
27
+ def download_url
28
+ "#{BookscanClient::URL::DOWNLOAD}?d=#{digest}&f=#{CGI.escape(filename)}"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1 @@
1
+ require "bookscan_client/model/book"
@@ -0,0 +1,3 @@
1
+ class BookscanClient
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,47 @@
1
+ require "bookscan_client/version"
2
+ require "bookscan_client/model"
3
+
4
+ require "uri"
5
+ require "cgi/util"
6
+ require "mechanize"
7
+ require "logger"
8
+
9
+ class BookscanClient
10
+ module URL
11
+ ROOT = "https://system.bookscan.co.jp/"
12
+ LOGIN = "https://system.bookscan.co.jp/login.php"
13
+ MYPAGE = "https://system.bookscan.co.jp/bookshelf_all_cover.php"
14
+ DOWNLOAD = "https://system.bookscan.co.jp/download.php"
15
+ end
16
+
17
+ def initialize(logger: Logger.new(STDOUT), sleep: 0.5)
18
+ @mechanize = Mechanize.new
19
+ @logger = logger
20
+ @sleep = sleep
21
+ end
22
+
23
+ def login(email, password)
24
+ @mechanize.post(URL::LOGIN, "email" => email, "password" => password)
25
+ end
26
+
27
+ def login?
28
+ names = cookies.map(&:name)
29
+ names.include?("email") && names.include?("password")
30
+ end
31
+
32
+ def books
33
+ page = fetch(URL::MYPAGE)
34
+ BookscanClient::Model::Book.parse_books(page)
35
+ end
36
+
37
+ def cookies
38
+ @mechanize.cookies
39
+ end
40
+
41
+ private
42
+
43
+ def fetch(url)
44
+ @logger.info("GET #{url}...")
45
+ @mechanize.get(url)
46
+ end
47
+ end
@@ -0,0 +1,20 @@
1
+ require_relative "./test_helper"
2
+
3
+ describe BookscanClient do
4
+ before do
5
+ @client = BookscanClient.new
6
+ end
7
+
8
+ describe "#books" do
9
+ it "returns book model array" do
10
+ books = @client.books
11
+ assert_equal books.size, 2
12
+ assert_equal books[0].filename, "filename1"
13
+ assert_equal books[0].hash, "hash1"
14
+ assert_equal books[0].digest, "digest1"
15
+ assert_equal books[0].image_url, nil
16
+ assert_equal books[1].filename, "filename2"
17
+ assert_equal books[1].image_url, "http://example.com/hoge.jpg"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head lang="en">
4
+ <meta charset="UTF-8">
5
+ <title></title>
6
+ </head>
7
+ <body>
8
+
9
+
10
+ <div id="sortable_box">
11
+ <div>
12
+ <a href="showbook.php?h=hash1&d=digest1&f=filename1" class="download showbook">book 1</a>
13
+ </div>
14
+ <div>
15
+ <a href="showbook.php?h=hash2&d=digest2&f=filename2" class="download showbook">
16
+ <img src="/images/loading.gif" data-original="http://example.com/hoge.jpg">
17
+ </a>
18
+ </div>
19
+ </div>
20
+ </body>
21
+ </html>
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require "minitest/autorun"
4
+ require "pry"
5
+ require "bookscan_client"
6
+
7
+ require "webmock/minitest"
8
+ WEBMOCK_DATADIR = File.expand_path('../data', __FILE__)
9
+ WEBMOCK_PAGES = {
10
+ BookscanClient::URL::MYPAGE => "mypage.html",
11
+ }
12
+ WEBMOCK_PAGES.each do |url, filename|
13
+ path = File.expand_path(filename, WEBMOCK_DATADIR)
14
+ WebMock.
15
+ stub_request(:get, url).
16
+ to_return(body: File.new(path), status: 200, headers: { 'Content-Type' => 'text/html' })
17
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bookscan_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hogelog
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mechanize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Unofficial bookscan client library for ruby
126
+ email:
127
+ - konbu.komuro@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - bookscan_client.gemspec
138
+ - lib/bookscan_client.rb
139
+ - lib/bookscan_client/model.rb
140
+ - lib/bookscan_client/model/book.rb
141
+ - lib/bookscan_client/version.rb
142
+ - test/bookscan_client_test.rb
143
+ - test/data/mypage.html
144
+ - test/test_helper.rb
145
+ homepage: https://github.com/hogelog/bookscan-client-ruby
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.4.5
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Unofficial bookscan client.
169
+ test_files:
170
+ - test/bookscan_client_test.rb
171
+ - test/data/mypage.html
172
+ - test/test_helper.rb