barcoder-client 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +57 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/barcoder-client.gemspec +86 -0
- data/lib/barcoder-client.rb +15 -0
- data/lib/barcoder/base.rb +51 -0
- data/lib/barcoder/bucket.rb +155 -0
- data/lib/barcoder/config.rb +7 -0
- data/lib/barcoder/page.rb +14 -0
- data/lib/barcoder/pdf.rb +122 -0
- data/spec/assets/barcoded-1.pdf +1565 -0
- data/spec/assets/unbarcoded-1.pdf +1451 -0
- data/spec/assets/unbarcoded-2.pdf +0 -0
- data/spec/assets/unbarcoded.pdf +1883 -0
- data/spec/bucket_spec.rb +148 -0
- data/spec/page_spec.rb +19 -0
- data/spec/pdf_spec.rb +115 -0
- data/spec/spec_helper.rb +18 -0
- metadata +172 -0
data/spec/bucket_spec.rb
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Barcoder::Bucket do
|
4
|
+
|
5
|
+
it "loads all buckets" do
|
6
|
+
buckets = Barcoder::Bucket.all
|
7
|
+
buckets.size.should be > 0
|
8
|
+
|
9
|
+
buckets.first.persisted.should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
context "an existing bucket" do
|
13
|
+
before(:each) do
|
14
|
+
@bucket = Barcoder::Bucket.all.first
|
15
|
+
end
|
16
|
+
|
17
|
+
context "#save" do
|
18
|
+
it "updates the existing bucket" do
|
19
|
+
token = @bucket.token # this is how we can ensure we get the same bucket
|
20
|
+
@bucket.name = 'updated name'
|
21
|
+
|
22
|
+
@bucket.save.should be_true
|
23
|
+
|
24
|
+
@bucket.name.should eq 'updated name'
|
25
|
+
@bucket.token.should eq token
|
26
|
+
@bucket.persisted.should be_true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "#decode_from_file" do
|
31
|
+
Barcoder::PDF.should_receive(:decode_from_file).
|
32
|
+
with(@bucket, '/test/file')
|
33
|
+
@bucket.decode_from_file('/test/file')
|
34
|
+
end
|
35
|
+
|
36
|
+
it "#decode_from_string" do
|
37
|
+
Barcoder::PDF.should_receive(:decode_from_string).
|
38
|
+
with(@bucket, 'content')
|
39
|
+
@bucket.decode_from_string('content')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "a new bucket" do
|
44
|
+
before(:each) do
|
45
|
+
@bucket = Barcoder::Bucket.new(name: 'new bucket')
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should not be persisted" do
|
49
|
+
@bucket.persisted.should be_false
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should be valid with a name" do
|
53
|
+
@bucket.valid?.should be_true
|
54
|
+
@bucket.name = nil
|
55
|
+
@bucket.valid?.should be_false
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should not allow decoding without a token" do
|
59
|
+
@bucket.decode_from_file('test').should be_false
|
60
|
+
@bucket.decode_from_string('test').should be_false
|
61
|
+
end
|
62
|
+
|
63
|
+
context "#save" do
|
64
|
+
it "returns false when invalid" do
|
65
|
+
@bucket.stub(:valid?).and_return(false)
|
66
|
+
@bucket.save.should be_false
|
67
|
+
end
|
68
|
+
|
69
|
+
it "persists and updates with new data from server when valid" do
|
70
|
+
@bucket.save.should be_true
|
71
|
+
|
72
|
+
@bucket.name.should eq 'new bucket'
|
73
|
+
@bucket.token.should_not be_nil
|
74
|
+
@bucket.persisted.should be_true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "#unmatched_pages" do
|
80
|
+
before(:each) do
|
81
|
+
@bucket = Barcoder::Bucket.all.first
|
82
|
+
@bucket.unmatched_pages
|
83
|
+
end
|
84
|
+
|
85
|
+
it "caches unmatched_pages" do
|
86
|
+
@bucket.should_not_receive(:unmatched_pages_request)
|
87
|
+
@bucket.unmatched_pages
|
88
|
+
end
|
89
|
+
|
90
|
+
it "breaks the cache when requested" do
|
91
|
+
@bucket.should_receive(:unmatched_pages_request).
|
92
|
+
and_return(mock(run: true))
|
93
|
+
@bucket.unmatched_pages(false)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "#recent_imports" do
|
98
|
+
before(:each) do
|
99
|
+
@bucket = Barcoder::Bucket.all.first
|
100
|
+
@bucket.recent_imports(Time.now)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "caches recent_imports" do
|
104
|
+
@bucket.should_not_receive(:recent_imports_request)
|
105
|
+
@bucket.recent_imports(Time.now)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "breaks the cache when requested" do
|
109
|
+
@bucket.should_receive(:recent_imports).
|
110
|
+
and_return(mock(run: true))
|
111
|
+
@bucket.recent_imports(Time.now, false)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "#partial_imports" do
|
116
|
+
before(:each) do
|
117
|
+
@bucket = Barcoder::Bucket.all.first
|
118
|
+
@bucket.partial_imports
|
119
|
+
end
|
120
|
+
|
121
|
+
it "caches partial_imports" do
|
122
|
+
@bucket.should_not_receive(:partial_imports_request)
|
123
|
+
@bucket.partial_imports
|
124
|
+
end
|
125
|
+
|
126
|
+
it "breaks the cache when requested" do
|
127
|
+
@bucket.should_receive(:partial_imports_request).
|
128
|
+
and_return(mock(run: true))
|
129
|
+
@bucket.partial_imports(false)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "#queue_" do
|
134
|
+
before(:each) do
|
135
|
+
@bucket = Barcoder::Bucket.all.first
|
136
|
+
@bucket.queue_unmatched_pages
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should not run the request yet" do
|
140
|
+
@bucket.instance_eval("@unmatched_pages").should be_nil
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should fetch the unmatched_pages when queue run" do
|
144
|
+
Barcoder::Config.hydra.run
|
145
|
+
@bucket.instance_eval("@unmatched_pages").should be_an Array
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
data/spec/page_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Barcoder::Page do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@page = Barcoder::Page.new(
|
7
|
+
id: '1', url: '/pdf_url_path.pdf',
|
8
|
+
thumbnails: {thumb: '/thumb_path.png', large: '/large_path.png'}
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "url" do
|
13
|
+
@page.url.should eq "#{Barcoder::Config.host}/pdf_url_path.pdf"
|
14
|
+
|
15
|
+
@page.url(:thumb).should eq "#{Barcoder::Config.host}/thumb_path.png"
|
16
|
+
@page.url(:large).should eq "#{Barcoder::Config.host}/large_path.png"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/spec/pdf_spec.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Barcoder::PDF do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@bucket = Barcoder::Bucket.new(token: '123testbucket')
|
7
|
+
@pdf = Barcoder::PDF.new pdf_content: 'content', token: 'token'
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be invalid without pdf_content" do
|
11
|
+
@pdf.pdf_content = ''
|
12
|
+
@pdf.valid?.should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should be invalid without token" do
|
16
|
+
@pdf.token = ''
|
17
|
+
@pdf.valid?.should_not be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should allow bucket to provide the token" do
|
21
|
+
@pdf.token = nil
|
22
|
+
@pdf.bucket = @bucket
|
23
|
+
@pdf.token.should eq '123testbucket'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "::new_from_file" do
|
27
|
+
unbarcoded = 'spec/assets/unbarcoded.pdf'
|
28
|
+
pdf = Barcoder::PDF.new_from_file(unbarcoded)
|
29
|
+
|
30
|
+
# Original file content
|
31
|
+
f = File.open(unbarcoded, 'r', encoding: 'ascii-8bit')
|
32
|
+
content = f.read
|
33
|
+
f.close
|
34
|
+
|
35
|
+
pdf.pdf_content.should eq content
|
36
|
+
end
|
37
|
+
|
38
|
+
context "#barcode!" do
|
39
|
+
before(:each) do
|
40
|
+
@pdf = Barcoder::PDF.new_from_file('spec/assets/unbarcoded.pdf')
|
41
|
+
@orig_pdf_content = @pdf.pdf_content
|
42
|
+
@pdf.token = '123testbucket'
|
43
|
+
@pdf.barcode!
|
44
|
+
end
|
45
|
+
it "updates with the barcoded version" do
|
46
|
+
@pdf.pdf_content.should_not eq @orig_pdf_content
|
47
|
+
# If we match the string PDF then we know we have properly performed
|
48
|
+
# decode64
|
49
|
+
@pdf.pdf_content.should =~ /PDF/
|
50
|
+
@pdf.messages.size.should eq 2
|
51
|
+
@pdf.id.present?.should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
context "::get" do
|
55
|
+
before(:each) do
|
56
|
+
@pdf = Barcoder::PDF.get(@bucket, @pdf.id)
|
57
|
+
end
|
58
|
+
it "fetches PDF with Pages" do
|
59
|
+
@pdf.pages.size.should eq 2
|
60
|
+
@pdf.pages.first.should be_a Barcoder::Page
|
61
|
+
|
62
|
+
@pdf.total_pages_count.should eq 2
|
63
|
+
@pdf.missing_pages_count.should eq 2
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "#queue_barcode" do
|
69
|
+
before(:each) do
|
70
|
+
opts = {token: '123testbucket'}
|
71
|
+
|
72
|
+
@pdf1 = Barcoder::PDF.new_from_file('spec/assets/unbarcoded-1.pdf', opts)
|
73
|
+
@pdf1.queue_barcode
|
74
|
+
@pdf2 = Barcoder::PDF.new_from_file('spec/assets/unbarcoded-2.pdf', opts)
|
75
|
+
@pdf2.queue_barcode
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should not run the requests yet" do
|
79
|
+
@pdf.id.should be_nil
|
80
|
+
end
|
81
|
+
|
82
|
+
it "updates with the barcoded version when we run the queued requests" do
|
83
|
+
Barcoder::Config.hydra.run
|
84
|
+
# If we match the string PDF then we know we have properly performed
|
85
|
+
# decode64
|
86
|
+
@pdf1.pdf_content.should =~ /PDF/
|
87
|
+
@pdf1.messages.size.should eq 1
|
88
|
+
@pdf1.id.present?.should be_true
|
89
|
+
|
90
|
+
@pdf2.pdf_content.should =~ /PDF/
|
91
|
+
@pdf2.messages.size.should eq 1
|
92
|
+
@pdf2.id.present?.should be_true
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "decoding" do
|
97
|
+
it "#decode_from_file" do
|
98
|
+
barcoded = 'spec/assets/barcoded-1.pdf'
|
99
|
+
Barcoder::PDF.should_receive(:decode_from_string).with(@bucket, File.read(barcoded))
|
100
|
+
Barcoder::PDF.decode_from_file(@bucket, barcoded)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "#decode_from_string" do
|
104
|
+
barcoded = 'spec/assets/barcoded-1.pdf'
|
105
|
+
res = Barcoder::PDF.decode_from_string(@bucket, File.read(barcoded))
|
106
|
+
res.should be_true
|
107
|
+
end
|
108
|
+
|
109
|
+
it "returns false when not a valid PDF" do
|
110
|
+
res = Barcoder::PDF.decode_from_string(@bucket, 'invalid PDF content')
|
111
|
+
res.should be_false
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'barcoder-client'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
config.before(:suite) do
|
13
|
+
Barcoder::Config.hydra = Typhoeus::Hydra.new
|
14
|
+
Barcoder::Config.host = 'http://localhost:3000'
|
15
|
+
Barcoder::Config.account_token = '123test'
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barcoder-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Winograd
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &9294620 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *9294620
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activemodel
|
27
|
+
requirement: &9292000 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *9292000
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: typhoeus
|
38
|
+
requirement: &9344820 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *9344820
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: yajl-ruby
|
49
|
+
requirement: &9342280 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *9342280
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rspec
|
60
|
+
requirement: &9341200 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *9341200
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rdoc
|
71
|
+
requirement: &9340360 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *9340360
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: bundler
|
82
|
+
requirement: &9396540 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *9396540
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: jeweler
|
93
|
+
requirement: &9394960 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *9394960
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: simplecov
|
104
|
+
requirement: &9394040 !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: *9394040
|
113
|
+
description: Ruby client to access the Barcoder
|
114
|
+
email: ryan@thewinograds.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files:
|
118
|
+
- LICENSE.txt
|
119
|
+
- README.rdoc
|
120
|
+
files:
|
121
|
+
- .document
|
122
|
+
- .rspec
|
123
|
+
- Gemfile
|
124
|
+
- Gemfile.lock
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.rdoc
|
127
|
+
- Rakefile
|
128
|
+
- VERSION
|
129
|
+
- barcoder-client.gemspec
|
130
|
+
- lib/barcoder-client.rb
|
131
|
+
- lib/barcoder/base.rb
|
132
|
+
- lib/barcoder/bucket.rb
|
133
|
+
- lib/barcoder/config.rb
|
134
|
+
- lib/barcoder/page.rb
|
135
|
+
- lib/barcoder/pdf.rb
|
136
|
+
- spec/assets/barcoded-1.pdf
|
137
|
+
- spec/assets/unbarcoded-1.pdf
|
138
|
+
- spec/assets/unbarcoded-2.pdf
|
139
|
+
- spec/assets/unbarcoded.pdf
|
140
|
+
- spec/bucket_spec.rb
|
141
|
+
- spec/page_spec.rb
|
142
|
+
- spec/pdf_spec.rb
|
143
|
+
- spec/spec_helper.rb
|
144
|
+
homepage: http://github.com/rylwin/barcoder-client
|
145
|
+
licenses:
|
146
|
+
- MIT
|
147
|
+
post_install_message:
|
148
|
+
rdoc_options: []
|
149
|
+
require_paths:
|
150
|
+
- lib
|
151
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ! '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
segments:
|
158
|
+
- 0
|
159
|
+
hash: -4441086539534065587
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
|
+
none: false
|
162
|
+
requirements:
|
163
|
+
- - ! '>='
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
requirements: []
|
167
|
+
rubyforge_project:
|
168
|
+
rubygems_version: 1.8.10
|
169
|
+
signing_key:
|
170
|
+
specification_version: 3
|
171
|
+
summary: Ruby client to access the Barcoder
|
172
|
+
test_files: []
|