lisbn 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
1
  require 'nori'
2
- require "lisbn/cache_method"
3
- require "lisbn/lisbn"
2
+ require 'yaml'
3
+ require_relative "lisbn/cache_method"
4
+ require_relative "lisbn/lisbn"
5
+ require_relative "lisbn/ranges"
@@ -116,20 +116,5 @@ private
116
116
  isbn[-1..-1] == isbn_13_checksum
117
117
  end
118
118
 
119
- def self.ranges
120
- rngs = Nori.new.parse(File.read(File.dirname(__FILE__) + '/../../data/RangeMessage.xml'))
121
- Array(rngs["ISBNRangeMessage"]["RegistrationGroups"]["Group"]).flatten.inject({}) do |memo, group|
122
- prefix = group["Prefix"].gsub(/-/, '')
123
- ranges = Array(group["Rules"]["Rule"]).flatten.map do |rule|
124
- length = rule["Length"].to_i
125
- next unless length > 0
126
-
127
- {:range => Range.new(*rule["Range"].split("-").map {|r| r[0..(length - 1)].to_i }), :length => length}
128
- end.compact
129
-
130
- memo.update(prefix => ranges)
131
- end
132
- end
133
-
134
- RANGES = ranges
119
+ RANGES = YAML::load_file(File.dirname(__FILE__) + "/../../data/ranges.yml")
135
120
  end
@@ -0,0 +1,31 @@
1
+ class Lisbn < String
2
+ module Ranges
3
+ def self.to_yaml
4
+ xml = Nori.new.parse(
5
+ File.read(File.dirname(__FILE__) + '/../../data/RangeMessage.xml')
6
+ )
7
+
8
+ ranges = xml["ISBNRangeMessage"]["RegistrationGroups"]["Group"]
9
+
10
+ hash = Array(ranges).flatten.inject({}) do |memo, group|
11
+ prefix = group["Prefix"].gsub(/-/, '')
12
+ ranges = Array(group["Rules"]["Rule"]).flatten.map do |rule|
13
+ length = rule["Length"].to_i
14
+ next unless length > 0
15
+
16
+ {:range => Range.new(*rule["Range"].split("-").map {|r| r[0..(length - 1)].to_i }), :length => length}
17
+ end.compact
18
+
19
+ memo.update(prefix => ranges)
20
+ end
21
+
22
+ YAML::dump(hash)
23
+ end
24
+
25
+ def self.save_to_yaml!
26
+ File.open(File.dirname(__FILE__) + "/../../data/ranges.yml", "w") do |f|
27
+ f.write(to_yaml)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
12
12
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
13
  gem.name = "lisbn"
14
14
  gem.require_paths = ["lib"]
15
- gem.version = "0.2.0"
15
+ gem.version = "0.2.1"
16
16
 
17
17
  gem.add_dependency "nori", "~> 2.0"
18
18
  gem.add_dependency "nokogiri"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lisbn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Ragalie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-15 00:00:00.000000000 Z
11
+ date: 2014-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nori
@@ -67,9 +67,11 @@ files:
67
67
  - README.md
68
68
  - Rakefile
69
69
  - data/RangeMessage.xml
70
+ - data/ranges.yml
70
71
  - lib/lisbn.rb
71
72
  - lib/lisbn/cache_method.rb
72
73
  - lib/lisbn/lisbn.rb
74
+ - lib/lisbn/ranges.rb
73
75
  - lisbn.gemspec
74
76
  - spec/cache_method_spec.rb
75
77
  - spec/lisbn_spec.rb