barcoder-client 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'activesupport'
4
+ gem 'activemodel'
5
+ gem 'typhoeus'
6
+ gem 'yajl-ruby'
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "rspec"
12
+ gem "rdoc"
13
+ gem "bundler"
14
+ gem "jeweler"
15
+ gem "simplecov"
16
+ end
@@ -0,0 +1,57 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.12)
5
+ activesupport (= 3.2.12)
6
+ builder (~> 3.0.0)
7
+ activesupport (3.2.12)
8
+ i18n (~> 0.6)
9
+ multi_json (~> 1.0)
10
+ builder (3.0.4)
11
+ diff-lcs (1.1.3)
12
+ ethon (0.5.9)
13
+ ffi (~> 1.2.0)
14
+ mime-types (~> 1.18)
15
+ ffi (1.2.1)
16
+ git (1.2.5)
17
+ i18n (0.6.1)
18
+ jeweler (1.8.4)
19
+ bundler (~> 1.0)
20
+ git (>= 1.2.5)
21
+ rake
22
+ rdoc
23
+ json (1.7.7)
24
+ mime-types (1.21)
25
+ multi_json (1.6.1)
26
+ rake (10.0.3)
27
+ rdoc (3.12.1)
28
+ json (~> 1.4)
29
+ rspec (2.12.0)
30
+ rspec-core (~> 2.12.0)
31
+ rspec-expectations (~> 2.12.0)
32
+ rspec-mocks (~> 2.12.0)
33
+ rspec-core (2.12.2)
34
+ rspec-expectations (2.12.1)
35
+ diff-lcs (~> 1.1.3)
36
+ rspec-mocks (2.12.2)
37
+ simplecov (0.7.1)
38
+ multi_json (~> 1.0)
39
+ simplecov-html (~> 0.7.1)
40
+ simplecov-html (0.7.1)
41
+ typhoeus (0.6.1)
42
+ ethon (~> 0.5.9)
43
+ yajl-ruby (1.1.0)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ activemodel
50
+ activesupport
51
+ bundler
52
+ jeweler
53
+ rdoc
54
+ rspec
55
+ simplecov
56
+ typhoeus
57
+ yajl-ruby
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Ryan Winograd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = barcoder-client
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to barcoder-client
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Ryan Winograd. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "barcoder-client"
18
+ gem.homepage = "http://github.com/rylwin/barcoder-client"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Ruby client to access the Barcoder}
21
+ gem.description = %Q{Ruby client to access the Barcoder}
22
+ gem.email = "ryan@thewinograds.com"
23
+ gem.authors = ["Ryan Winograd"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "barcoder-client #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,86 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "barcoder-client"
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Winograd"]
12
+ s.date = "2013-02-19"
13
+ s.description = "Ruby client to access the Barcoder"
14
+ s.email = "ryan@thewinograds.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "barcoder-client.gemspec",
29
+ "lib/barcoder-client.rb",
30
+ "lib/barcoder/base.rb",
31
+ "lib/barcoder/bucket.rb",
32
+ "lib/barcoder/config.rb",
33
+ "lib/barcoder/page.rb",
34
+ "lib/barcoder/pdf.rb",
35
+ "spec/assets/barcoded-1.pdf",
36
+ "spec/assets/unbarcoded-1.pdf",
37
+ "spec/assets/unbarcoded-2.pdf",
38
+ "spec/assets/unbarcoded.pdf",
39
+ "spec/bucket_spec.rb",
40
+ "spec/page_spec.rb",
41
+ "spec/pdf_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+ s.homepage = "http://github.com/rylwin/barcoder-client"
45
+ s.licenses = ["MIT"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = "1.8.10"
48
+ s.summary = "Ruby client to access the Barcoder"
49
+
50
+ if s.respond_to? :specification_version then
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
55
+ s.add_runtime_dependency(%q<activemodel>, [">= 0"])
56
+ s.add_runtime_dependency(%q<typhoeus>, [">= 0"])
57
+ s.add_runtime_dependency(%q<yajl-ruby>, [">= 0"])
58
+ s.add_development_dependency(%q<rspec>, [">= 0"])
59
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
60
+ s.add_development_dependency(%q<bundler>, [">= 0"])
61
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
62
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
63
+ else
64
+ s.add_dependency(%q<activesupport>, [">= 0"])
65
+ s.add_dependency(%q<activemodel>, [">= 0"])
66
+ s.add_dependency(%q<typhoeus>, [">= 0"])
67
+ s.add_dependency(%q<yajl-ruby>, [">= 0"])
68
+ s.add_dependency(%q<rspec>, [">= 0"])
69
+ s.add_dependency(%q<rdoc>, [">= 0"])
70
+ s.add_dependency(%q<bundler>, [">= 0"])
71
+ s.add_dependency(%q<jeweler>, [">= 0"])
72
+ s.add_dependency(%q<simplecov>, [">= 0"])
73
+ end
74
+ else
75
+ s.add_dependency(%q<activesupport>, [">= 0"])
76
+ s.add_dependency(%q<activemodel>, [">= 0"])
77
+ s.add_dependency(%q<typhoeus>, [">= 0"])
78
+ s.add_dependency(%q<yajl-ruby>, [">= 0"])
79
+ s.add_dependency(%q<rspec>, [">= 0"])
80
+ s.add_dependency(%q<rdoc>, [">= 0"])
81
+ s.add_dependency(%q<bundler>, [">= 0"])
82
+ s.add_dependency(%q<jeweler>, [">= 0"])
83
+ s.add_dependency(%q<simplecov>, [">= 0"])
84
+ end
85
+ end
86
+
@@ -0,0 +1,15 @@
1
+ require 'yajl'
2
+ require 'active_support/core_ext/hash/indifferent_access'
3
+ require 'active_support/concern'
4
+ require 'active_model'
5
+ require 'typhoeus'
6
+
7
+ require 'base64'
8
+
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+
11
+ require 'barcoder/config'
12
+ require 'barcoder/base'
13
+ require 'barcoder/bucket'
14
+ require 'barcoder/pdf'
15
+ require 'barcoder/page'
@@ -0,0 +1,51 @@
1
+ class Barcoder::Base
2
+ include ActiveModel::Serializers::JSON
3
+ include ActiveModel::Validations
4
+
5
+ def initialize(attributes_or_json={})
6
+ self.attributes = attributes_or_json
7
+ end
8
+
9
+ def attributes=(attributes_or_json)
10
+ if attributes_or_json.is_a? String
11
+ from_json(attributes_or_json)
12
+ attributes = attributes.with_indifferent_access
13
+ else
14
+ attributes = attributes_or_json.with_indifferent_access
15
+ end
16
+
17
+ attributes.each do |field, val|
18
+ send("#{field}=", val)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def process_save_response(response)
25
+ data = response_to_attr(response)
26
+ self.attributes = data
27
+ end
28
+
29
+ # Instance-level conversion of response to attributes
30
+ def response_to_attr(response)
31
+ data = self.class.response_to_attr(response)
32
+ data.merge! persisted: true if respond_to?(:persisted=)
33
+ data
34
+ end
35
+
36
+ # Class-level conversion of response to attributes
37
+ def self.response_to_attr(response)
38
+ data = response_to_json(response).with_indifferent_access
39
+ data[:pages] = data[:pages].map {|p| Barcoder::Page.new(p)} if data[:pages]
40
+ data
41
+ end
42
+
43
+ def self.response_to_json(response)
44
+ Yajl::Parser.parse(response.body)
45
+ end
46
+
47
+ def response_to_json(response)
48
+ self.class.response_to_json response
49
+ end
50
+
51
+ end
@@ -0,0 +1,155 @@
1
+ class Barcoder::Bucket < Barcoder::Base
2
+
3
+ attr_accessor :name, :token, :import_hook, :persisted
4
+
5
+ validates :name, :name, presence: true
6
+
7
+ # Returns an array of all Buckets belonging to the account identified by the
8
+ # account_token (see Barcoder::Config).
9
+ def self.all(&block)
10
+ response = Typhoeus::Request.new(buckets_uri).run
11
+
12
+ buckets_data = response_to_json(response)
13
+
14
+ buckets = buckets_data.map do |data|
15
+ new(data.merge(persisted: true))
16
+ end
17
+
18
+ buckets
19
+ end
20
+
21
+ def self.buckets_uri
22
+ "#{Barcoder::Config.host}/buckets?token=#{Barcoder::Config.account_token}"
23
+ end
24
+
25
+ def self.bucket_uri(bucket)
26
+ "#{Barcoder::Config.host}/bucket"
27
+ end
28
+
29
+ def decode_from_file(filename)
30
+ return false unless token.present?
31
+ Barcoder::PDF.decode_from_file(self, filename)
32
+ end
33
+
34
+ def decode_from_string(string)
35
+ return false unless token.present?
36
+ Barcoder::PDF.decode_from_string(self, string)
37
+ end
38
+
39
+ # Fetches unmatched_pages.
40
+ #
41
+ # This method is also queueable by calling #queue_unmatched_pages.
42
+ def unmatched_pages(use_cache=true)
43
+ (use_cache && @unmatched_pages) || begin
44
+ unmatched_pages_request.run
45
+ end
46
+
47
+ @unmatched_pages
48
+ end
49
+
50
+ # Fetches recent_imports.
51
+ #
52
+ # This method is also queueable by calling #queue_recent_imports.
53
+ def recent_imports(since, use_cache=true)
54
+ (use_cache && @recent_imports) || begin
55
+ recent_imports_request(since).run
56
+ end
57
+
58
+ @recent_imports
59
+ end
60
+
61
+ # Fetches partial_imports.
62
+ #
63
+ # This method is also queueable by calling #queue_partial_imports.
64
+ def partial_imports(use_cache=true)
65
+ (use_cache && @partial_imports) || begin
66
+ partial_imports_request.run
67
+ end
68
+
69
+ @partial_imports
70
+ end
71
+
72
+ # Persists the Bucket.
73
+ def save
74
+ return false unless valid?
75
+
76
+ if !persisted
77
+ create_bucket!
78
+ else
79
+ update_bucket!
80
+ end
81
+
82
+ true
83
+ end
84
+
85
+ def method_missing(symbol, *args)
86
+ # e.g., if users calls queue_unmatched_pages, add unmatched_pages_request
87
+ # to hydra queue
88
+ if symbol.to_s =~ /queue_(.*)/
89
+ request_method = "#{$1}_request"
90
+ Barcoder::Config.hydra.queue send(request_method, *args)
91
+ else
92
+ super
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def unmatched_pages_request
99
+ url = "#{Barcoder::Config.host}/unmatched_pages"
100
+ request = Typhoeus::Request.new(url, params: {token: token})
101
+
102
+ request.on_success do |response|
103
+ json = response_to_json(response)
104
+ @unmatched_pages = json.map { |j|
105
+ Barcoder::Page.new(j.with_indifferent_access) }
106
+ end
107
+
108
+ request
109
+ end
110
+
111
+ def recent_imports_request(since)
112
+ url = "#{Barcoder::Config.host}/recent_imports"
113
+ params = {token: token, since: since}
114
+ request = Typhoeus::Request.new(url, params: params)
115
+
116
+ request.on_success do |response|
117
+ json = response_to_json(response)
118
+ @recent_imports = json.map { |j|
119
+ Barcoder::PDF.new(j.with_indifferent_access) }
120
+ end
121
+
122
+ request
123
+ end
124
+
125
+ def partial_imports_request
126
+ url = "#{Barcoder::Config.host}/partial_imports"
127
+ params = {token: token}
128
+ request = Typhoeus::Request.new(url, params: params)
129
+
130
+ request.on_success do |response|
131
+ json = response_to_json(response)
132
+ @partial_imports = json.map { |j|
133
+ Barcoder::PDF.new(j.with_indifferent_access) }
134
+ end
135
+
136
+ request
137
+ end
138
+
139
+ def create_bucket!
140
+ opt = {body: save_attr, method: :post}
141
+ response = Typhoeus::Request.new(self.class.buckets_uri, opt).run
142
+ process_save_response response
143
+ end
144
+
145
+ def update_bucket!
146
+ opt = {params: save_attr.merge(token: token), method: :put}
147
+ response = Typhoeus::Request.new(self.class.bucket_uri(self), opt).run
148
+ process_save_response response
149
+ end
150
+
151
+ def save_attr
152
+ {name: name, import_hook: import_hook}
153
+ end
154
+
155
+ end