heathen-client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in heathen-client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 IFAD
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,87 @@
1
+ <pre>
2
+ ) ) )
3
+ ( /( ( * ) ( /( ( /(
4
+ )\()) ( )\ ` ) /( )\()) ( )\())
5
+ ((_)\ )\ ((((_)( ( )(_))((_)\ )\ ((_)\
6
+ _((_)((_) )\ _ )\ (_(_()) _((_)((_) _((_)
7
+ | || || __|(_)_\(_)|_ _| | || || __|| \| |
8
+ | __ || _| / _ \ | | | __ || _| | .` |
9
+ |_||_||___|/_/ \_\ |_| |_||_||___||_|\_|
10
+ </pre>
11
+ # Convert the heathens.
12
+ ***
13
+
14
+ # Heathen::Client
15
+
16
+ A Ruby client for the [Heathen](https://github.com/ifad/heathen) service.
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ gem 'heathen-client'
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install heathen-client
31
+
32
+ ## Usage
33
+
34
+ ### Set the Base URI
35
+
36
+ Before anything, you will need to set the url of the running Heathen service.
37
+ `Heathen::Client.client = Heathen::Client.new(base_uri: "http://<heathen url>")`
38
+
39
+ Without the above line, the client will attempt to make API calls against `http://localhost:9292`.
40
+
41
+ Heathen::Client::pdf takes an options hash with a `file` or `url` key. The `file` key can map to a filesystem path string, or an instance of `IO`. The method returns an instance of `Heathen::Client::Response`.
42
+
43
+ ### Convert a Word document to PDF
44
+
45
+ In one shot:
46
+
47
+ ```
48
+ Heathen::Client.pdf(file: "/path/to/word.doc").get do |data|
49
+ File.open("/path/to/saved.pdf", "wb") do |dest|
50
+ dest.write(data)
51
+ end
52
+ end
53
+ ```
54
+
55
+ For the lazy:
56
+
57
+ `response = Heathen::Client.pdf(file: "/path/to/word.doc")`
58
+
59
+ A little later:
60
+
61
+ ```
62
+ File.open("/path/to/saved.pdf", "wb") do |dest|
63
+ response.get { |data| dest.write(data) }
64
+ end
65
+ ```
66
+
67
+ ### Get the original
68
+
69
+ ```
70
+ File.open("/path/to/saved_original.doc", "wb") do |dest|
71
+ response.get(:original) { |data| dest.write(data) }
72
+ end
73
+ ```
74
+
75
+ ### Converting an image to PDF with searching/selection
76
+
77
+ `response = Heathen::Client.ocr(file: "/path/to/text_image.tiff")`
78
+
79
+ Downloading/saving is the same as shown above.
80
+
81
+ # License
82
+
83
+ MIT
84
+
85
+ # Copyright
86
+
87
+ &copy; IFAD 2013
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+
4
+ desc 'Launch the console'
5
+ task :console do
6
+ sh "irb -I lib -r heathen-client"
7
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'heathen/client/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "heathen-client"
8
+ gem.version = Heathen::Client::VERSION
9
+ gem.authors = [ "Peter Brindisi" ]
10
+ gem.email = [ "p.brindisi@ifad.org" ]
11
+ gem.description = %q{ A client for the Heathen server. Convert & download. }
12
+ gem.summary = %q{ A client for the Heathen server. }
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = [ "lib" ]
19
+
20
+ gem.add_dependency('yajl-ruby', '~> 1.1.0')
21
+ gem.add_dependency('rest-client', '~> 1.6.7')
22
+ end
@@ -0,0 +1,82 @@
1
+ require 'heathen/client/version'
2
+ require 'heathen/client/interface'
3
+ require 'heathen/client/response'
4
+
5
+ require 'uri'
6
+ require 'yajl'
7
+ require 'rest_client'
8
+ require 'pathname'
9
+
10
+ module Heathen
11
+ class Client
12
+
13
+ extend Interface
14
+
15
+ DEFAULT_BASE_URI = 'http://localhost:9292'
16
+
17
+ class << self
18
+ def client
19
+ @client ||= new
20
+ end
21
+
22
+ def client=(c)
23
+ @client = c
24
+ end
25
+ end
26
+
27
+ def initialize(options = { })
28
+ @base_uri = URI.parse(options.fetch(:base_uri, DEFAULT_BASE_URI))
29
+ end
30
+
31
+ def convert(action, options)
32
+
33
+ options.merge!(action: action)
34
+ headers = {
35
+ content_type: :json,
36
+ accept: :json
37
+ }
38
+
39
+ # in Rails, Hash has a read method, but this makes
40
+ # RestClient think that the hash is a File.
41
+ #
42
+ # this fixes compatitbility with Rails without
43
+ # needing to patch RestClient
44
+ class << options
45
+ def respond_to?(*args)
46
+ args.first.to_s == 'read' ? false : super
47
+ end
48
+ end
49
+
50
+ RestClient.post(@base_uri.to_s + '/convert', options, headers) do |response|
51
+ Response.new(self, response.body)
52
+ end
53
+ end
54
+
55
+ def download(url, options)
56
+ file = options.fetch(:file, nil)
57
+ dir = file ? nil : Pathname(options.fetch(:dir))
58
+
59
+ if dir && !dir.directory?
60
+ raise "Not a directory: #{dir}"
61
+
62
+ elsif !file || (file && !file.respond_to?(:write))
63
+ raise 'File or directory must be given.'
64
+ end
65
+
66
+ RestClient.get((@base_uri + URI.parse(url).path).to_s) do |response|
67
+ disposition = response.headers[:content_disposition]
68
+ filename = URI.decode(disposition.match(/filename\=\"(.+?)\"/)[1])
69
+
70
+ if dir
71
+ File.open(dir + filename, "wb") { |f| f.write(response.body) }
72
+ else
73
+ file.write(response.body)
74
+ end
75
+
76
+ if file
77
+ file.close
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,35 @@
1
+ module Heathen
2
+ class Client
3
+ module Interface
4
+
5
+ def pdf(args = { })
6
+ convert(:pdf, shared_args(args))
7
+ end
8
+
9
+ def ocr(args = { })
10
+ convert(:ocr, shared_args(args).merge(language: args.fetch(:language)))
11
+ end
12
+
13
+ def convert(action, options)
14
+ client.convert(action, options)
15
+ end
16
+
17
+ private
18
+
19
+ def shared_args(args)
20
+
21
+ shared = { }
22
+
23
+ if file = args[:file]
24
+ shared[:file] = file.is_a?(IO) ? file : File.new(file, "rb")
25
+ shared[:multipart] = true
26
+
27
+ elsif url = args[:url]
28
+ shared[:url] = URL.parse(url).to_s
29
+ end
30
+
31
+ shared
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,65 @@
1
+ module Heathen
2
+ class Client
3
+ class Response
4
+
5
+ attr_reader :data
6
+
7
+ def initialize(client, response)
8
+ @client = client
9
+ @status = response.code
10
+ begin
11
+ @data = Yajl::Parser.parse(response.body)
12
+ rescue Exception
13
+ @data = { 'error' => 'Internal Server Error' }
14
+ end
15
+ end
16
+
17
+ def original(file = nil)
18
+ url('original', file)
19
+ end
20
+
21
+ def converted(file = nil)
22
+ url('converted', file)
23
+ end
24
+
25
+ def error
26
+ data['error']
27
+ end
28
+
29
+ def success?
30
+ error.nil?
31
+ end
32
+
33
+ def error?
34
+ !error.nil?
35
+ end
36
+
37
+ # yields data to block
38
+ def get(which = :converted, &block)
39
+ unless error?
40
+ RestClient.get(send(which), &block)
41
+ end
42
+ end
43
+
44
+ # convenience method
45
+ def download(args = { })
46
+ File.open(args[:path], "wb") do |dest|
47
+ get(args.fetch(:which, :converted)) do |data|
48
+ dest.write(data)
49
+ end
50
+ dest.path
51
+ end
52
+ end
53
+
54
+ protected
55
+
56
+ def url(which, file = nil)
57
+ data[which.to_s].tap do |u|
58
+ if u && file
59
+ @client.download(u, file: file)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ module Heathen
2
+ class Client
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heathen-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Brindisi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yajl-ruby
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.7
46
+ description: ! ' A client for the Heathen server. Convert & download. '
47
+ email:
48
+ - p.brindisi@ifad.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - heathen-client.gemspec
59
+ - lib/heathen-client.rb
60
+ - lib/heathen/client/interface.rb
61
+ - lib/heathen/client/response.rb
62
+ - lib/heathen/client/version.rb
63
+ homepage: ''
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.24
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: A client for the Heathen server.
87
+ test_files: []
88
+ has_rdoc: