franklin 0.2.2 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60b3b782b0df4bd93a1447868027d174ac5a409e
4
- data.tar.gz: 4ed253ddbaa8f65eebb2f106cc2e99edbb5e4fef
3
+ metadata.gz: 609ae9a3826ac3f2c416a293f8c714420b904646
4
+ data.tar.gz: 639c32c3fe2346aa6e358a237d52bcfaef8ad4b0
5
5
  SHA512:
6
- metadata.gz: f74fd76d5773419e171f91525295437db57a2cef6381fb11ad60ed10e960018ba68a46165093ef2cc6294d870d1c7ba317df2f23c42390cc511cff67980b53c1
7
- data.tar.gz: 0a2cd4d19d25c811bd35ba1a7421e57578729ef61edc29bb5a305a7165e48092d7ebbe65b65599374503d2e06ba60007bddeb93b468f149dc4689ff91c0190e8
6
+ metadata.gz: 7a7963ae9f09b9bb14aeaab91c76d2e0a5940849ad9d8e3622a2345e8391fd96b249b30d8706616e08bb1bddfc47a97f63514d0ebe8abbf73d4d57e5f3b3b09b
7
+ data.tar.gz: 49c25f0bfe6b9eb2604a2d417b5a755b9f4945e8cb2ac4dfb6537b4917fedd83f985a692075d56c1e912f290963807d0add70362d22b084d75a4dc395fdba4b6
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ # 0.3.0
2
+ - Accomodate changes in OverDrive. Public libraries now have a new format
@@ -1,16 +1,20 @@
1
1
  require "franklin/item"
2
2
  require "franklin/availability"
3
3
  require "mechanize"
4
+ require "json"
4
5
 
5
6
  module Franklin
6
7
  class Search
8
+ JS_VARIABLE = /window\.OverDrive\.mediaItems/
9
+
7
10
  def initialize(library)
8
11
  @library = library
9
12
  end
10
13
 
11
14
  def perform(search_term)
12
- results = search_library(search_term)
13
- parse_results(results)
15
+ results_page = search_library(search_term)
16
+ results_json = extract_json(page: results_page)
17
+ parse(json: results_json)
14
18
  end
15
19
 
16
20
  private
@@ -18,41 +22,35 @@ module Franklin
18
22
  attr_reader :library
19
23
 
20
24
  def search_library(search_term)
21
- form = prepare_form(search_term)
22
- form.submit
25
+ prepare_form(search_term).submit
23
26
  end
24
27
 
25
28
  def prepare_form(search_term)
26
- ::Mechanize.new.get(library.url).forms.first.tap { |form|
27
- form.FullTextCriteria = search_term
29
+ ::Mechanize.new.get(library.url).form_with(action: "/search") { |form|
30
+ form.query = search_term
28
31
  }
29
32
  end
30
33
 
31
- def parse_results(results)
32
- results.search("div.containAll").each_with_object({}) { |container, result|
33
- result[parse_item(container)] = parse_availability(container)
34
- }
34
+ def extract_json(page:)
35
+ script_tag = page.search("script").find { |script| script.text =~ JS_VARIABLE }
36
+ var_assignment_line = script_tag.text.lines.find { |line| line =~ JS_VARIABLE }
37
+ raw_javascript_object = var_assignment_line.scan(/{.*}/).first
38
+ JSON.parse(raw_javascript_object)
35
39
  end
36
40
 
37
- def parse_item(container)
38
- item_info = container.css("a.share-links").first.attributes
39
- id = item_info["data-sharecrid"].value
40
- title = item_info["data-sharetitle"].value
41
- author = item_info["data-sharecreator"].value
42
-
43
- format = container.css("span.tcc-icon-span").first.attributes["data-iconformat"].value
44
-
45
- Item.new(id, title, author, format)
41
+ def parse(json:)
42
+ json.each_with_object({}) { |raw, result|
43
+ id, data = raw
44
+ result[parse_item(id: id, data: data)] = parse_availability(data: data)
45
+ }
46
46
  end
47
47
 
48
- def parse_availability(container)
49
- copies_info = container.css("div.img-and-info-contain.title-data").first.attributes
50
-
51
- total_copies = copies_info["data-copiestotal"].value.to_i
52
- available_copies = copies_info["data-copiesavail"].value.to_i
53
- wait_list_size = copies_info["data-numwaiting"].value.to_i
48
+ def parse_item(id:, data:)
49
+ Item.new(id, data["title"], data["firstCreatorName"], data["type"]["name"])
50
+ end
54
51
 
55
- Availability.new(library, total_copies, available_copies, wait_list_size)
52
+ def parse_availability(data:)
53
+ Availability.new(library, data["ownedCopies"], data["availableCopies"], data["holdsCount"])
56
54
  end
57
55
  end
58
56
  end
@@ -1,3 +1,3 @@
1
1
  module Franklin
2
- VERSION = "0.2.2".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: franklin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ylan Segal
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-14 00:00:00.000000000 Z
11
+ date: 2016-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -118,6 +118,7 @@ extra_rdoc_files: []
118
118
  files:
119
119
  - ".gitignore"
120
120
  - ".travis.yml"
121
+ - CHANGELOG.md
121
122
  - Gemfile
122
123
  - LICENSE.txt
123
124
  - README.md
@@ -159,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
160
  version: '0'
160
161
  requirements: []
161
162
  rubyforge_project:
162
- rubygems_version: 2.5.1
163
+ rubygems_version: 2.5.2
163
164
  signing_key:
164
165
  specification_version: 4
165
166
  summary: CLI utility for searching Overdrive-powered public libraries