doxie 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +116 -0
  4. data/doxie.gemspec +18 -0
  5. data/lib/doxie.rb +87 -3
  6. data/spec/doxie_spec.rb +90 -0
  7. metadata +5 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e336a766a3199d5e6702c2035fc9413c7ba0d9a
4
- data.tar.gz: 0163ca246b0e2df35f580858416b4d07367e5682
3
+ metadata.gz: 51d0c17e145193ebfec7231b9c9113799c384305
4
+ data.tar.gz: 7fae9d4100c33a0eedfd83fe7637ea1b878a1284
5
5
  SHA512:
6
- metadata.gz: 601ad9ce03000092f90d0681ffa851c017713cb37e284af7d66e2b94a65abd05921b4643e046b3cf80718fc8d97b656d16211850fdfeb4d660f7b068a7816dca
7
- data.tar.gz: 3122dbd27a93d9fe3e070c526b160645f63b17d9a7ca109e1a126a988ca385fe700e44777c99e79c856941e23bd5dabe6204f047d302bf2e61cdb45c495fc96c
6
+ metadata.gz: e8be18b7473ac8eeba04d4d297e0389ce12357ca328639c1113094880c6d560acbc9131bc50e808698f951c3ce4f005baf83164adcd7a1d922f9e40188e0e055
7
+ data.tar.gz: 3c3188c4e7d0e2c45b333a6c42d4436baf40aaf4b2ebba6c93bccb46a53bb310d85446e24f495c55988ce87d01098f5f1ba6a83e182ad4f5377c6893d1211466
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Cristiano Betta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,116 @@
1
+ # Doxie Ruby API Wrapper
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/doxie.svg)](https://badge.fury.io/rb/doxie) [![Build Status](https://travis-ci.org/cbetta/doxie.svg?branch=master)](https://travis-ci.org/cbetta/doxie)
4
+
5
+ A wrapper for the Doxie Go Wifi API. Specification as per the [developer documentation](http://help.getdoxie.com/content/doxiego/05-advanced/03-wifi/04-api/Doxie-API-Developer-Guide.pdf).
6
+
7
+ ## Installation
8
+
9
+ Either install directly or via bundler.
10
+
11
+ ```rb
12
+ gem 'doxie'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Client
18
+
19
+ The client accepts an `ip` and `password`. You can omit the `password` if your Doxie has non set.
20
+
21
+ ```rb
22
+ require 'doxie'
23
+ client = Doxie::Client.new(ip: '192.168.1.2', password: 'test')
24
+ ```
25
+
26
+ ### GET /hello.json
27
+
28
+ ```rb
29
+ client.hello
30
+ => {
31
+ "model" => "DX250",
32
+ "name" => "Doxie_062300",
33
+ "firmwareWiFi" => "1.29",
34
+ "hasPassword" => true,
35
+ "MAC" => "00:11:11:11:11:00",
36
+ "mode" => "Client",
37
+ "network" => "YourWifi",
38
+ "ip" => "192.168.1.2"
39
+ }
40
+ ```
41
+
42
+ ### GET /hello_extra.json
43
+
44
+ ```rb
45
+ client.hello_extra
46
+ => {
47
+ "firmware" => "0.26",
48
+ "connectedToExternalPower" => true
49
+ }
50
+ ```
51
+
52
+ ### GET /restart.json
53
+
54
+ ```rb
55
+ client.restart
56
+ => true
57
+ ```
58
+
59
+ ### GET /scans.json
60
+
61
+ ```rb
62
+ client.scans
63
+ => [
64
+ [0] {
65
+ "name" => "/DOXIE/JPEG/IMG_0001.JPG",
66
+ "size" => 900964,
67
+ "modified" => "2010-05-01 00:02:38"
68
+ }
69
+ ]
70
+ ```
71
+
72
+
73
+ ### GET /scans/recent.json
74
+
75
+ ```rb
76
+ client.recent_scans
77
+ => {
78
+ "path" => "/DOXIE/JPEG/IMG_0001.JPG"
79
+ }
80
+ ```
81
+
82
+ ### GET /scans/DOXIE/JPEG/IMG_XXXX.JPG
83
+
84
+ There are 2 ways to get a scan off your Doxie. The first is to get the raw binary content and then do something with it yourself.
85
+
86
+ ```rb
87
+ client.scan "/DOXIE/JPEG/IMG_0001.JPG"
88
+ => "...?]?1:Xt?????'A??}:<??13???z*???}?rT???????z!ESj?/?..."
89
+ ```
90
+
91
+ The other is to pass in a filename:
92
+
93
+ ```rb
94
+ client.scan "/DOXIE/JPEG/IMG_0001.JPG", 'test.jpg'
95
+ => true
96
+ ```
97
+
98
+ ## Contributing
99
+
100
+ 1. **Fork** the repo on GitHub
101
+ 2. **Clone** the project to your own machine
102
+ 3. **Commit** changes to your own branch
103
+ 4. **Push** your work back up to your fork
104
+ 5. Submit a **Pull request** so that we can review your changes
105
+
106
+ ### Development
107
+
108
+ * `bundle install` to get dependencies
109
+ * `rake` to run tests
110
+ * `rake console` to run a local console with the library loaded
111
+
112
+ ## Credits and License
113
+
114
+ Thanks to [@timcraft](https://github.com/timcraft) for the excellent [Nexmo Ruby livrary](https://github.com/Nexmo/nexmo-ruby) which helped me remember how to nicely wrap the Doxie API.
115
+
116
+ This library is released under the [MIT License](LICENSE).
@@ -0,0 +1,18 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.name = 'doxie'
4
+ s.version = '0.0.7'
5
+ s.summary = "Doxie API Wrapper for getting scans off your Doxie scanner"
6
+ s.description = "Doxie API Wrapper for getting scans off your Doxie scanner"
7
+ s.authors = ["Cristiano Betta"]
8
+ s.email = 'cbetta@gmail.com'
9
+ s.files = Dir.glob('{lib,spec}/**/*') + %w(LICENSE README.md doxie.gemspec)
10
+ s.homepage = 'https://github.com/cbetta/doxie'
11
+ s.license = 'MIT'
12
+ s.require_path = 'lib'
13
+
14
+ s.add_development_dependency('rake')
15
+ s.add_development_dependency('webmock')
16
+ s.add_development_dependency('minitest')
17
+ s.add_development_dependency('fakefs')
18
+ end
@@ -1,5 +1,89 @@
1
- class Doxie
1
+ require 'net/http'
2
+ require 'json'
2
3
 
3
- end
4
+ module Doxie
5
+ class Client
6
+ class Error < StandardError; end
7
+ class ClientError < Error; end
8
+ class ServerError < Error; end
9
+ class AuthenticationError < ClientError; end
10
+
11
+ USERNAME = 'doxie'.freeze
12
+
13
+ attr_accessor :ip, :password
14
+
15
+ def initialize options
16
+ @ip = options[:ip] || ''
17
+ @password = options[:password] || ''
18
+ end
19
+
20
+ def hello
21
+ get('/hello.json')
22
+ end
23
+
24
+ def hello_extra
25
+ get('/hello_extra.json')
26
+ end
27
+
28
+ def restart
29
+ get('/restart.json')
30
+ end
31
+
32
+ def scans
33
+ get('/scans.json')
34
+ end
4
35
 
5
- require "doxie/client"
36
+ def recent_scans
37
+ get('/scans/recent.json')
38
+ end
39
+
40
+ def scan scan_name, file_name = nil
41
+ file "/scans#{scan_name}", file_name
42
+ end
43
+
44
+ private
45
+
46
+ def get path
47
+ uri = URI("https://#{ip}:8080#{path}")
48
+ message = Net::HTTP::Get.new(uri.request_uri)
49
+ parse(request(uri, message))
50
+ end
51
+
52
+ def request(uri, message)
53
+ message.basic_auth USERNAME, password if password
54
+ http = Net::HTTP.new(uri.host, uri.port)
55
+ http.request(message)
56
+ end
57
+
58
+ def parse response
59
+ case response
60
+ when Net::HTTPNoContent
61
+ return nil
62
+ when Net::HTTPSuccess
63
+ if response['Content-Type'].split(';').first == 'application/json'
64
+ JSON.parse(response.body)
65
+ else
66
+ response.body
67
+ end
68
+ when Net::HTTPUnauthorized
69
+ raise AuthenticationError, "#{response.code} response from #{ip}"
70
+ when Net::HTTPClientError
71
+ raise ClientError, "#{response.code} response from #{ip}"
72
+ when Net::HTTPServerError
73
+ raise ServerError, "#{response.code} response from #{ip}"
74
+ else
75
+ raise Error, "#{response.code} response from #{ip}"
76
+ end
77
+ end
78
+
79
+ def file scan_name, file_name
80
+ body = get(scan_name)
81
+ if file_name
82
+ File.open(file_name, 'wb') { |file| file.write(body) }
83
+ true
84
+ else
85
+ body
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,90 @@
1
+ require 'minitest/autorun'
2
+ require 'webmock/minitest'
3
+ require 'fakefs'
4
+ require 'doxie'
5
+
6
+ describe 'Doxie::Client' do
7
+ def json_response_body(content)
8
+ {headers: {'Content-Type' => 'application/json;charset=utf-8'}, body: content}
9
+ end
10
+
11
+ before do
12
+ @json_response_object = {'key' => 'value'}
13
+ @json_response_body = json_response_body('{"key":"value"}')
14
+ @ip = '192.168.1.1'
15
+ @base_url = "http://#{@ip}:8080"
16
+ @client = Doxie::Client.new(ip: @ip)
17
+ end
18
+
19
+ describe 'get /hello.json' do
20
+ it 'should return the result' do
21
+ stub_request(:get, "#{@base_url}/hello.json")
22
+ .to_return(@json_response_body)
23
+ @client.hello.must_equal(@json_response_object)
24
+ end
25
+ end
26
+
27
+ describe 'get /hello_extra.json' do
28
+ it 'should return the result' do
29
+ stub_request(:get, "#{@base_url}/hello_extra.json")
30
+ .to_return(@json_response_body)
31
+ @client.hello_extra.must_equal(@json_response_object)
32
+ end
33
+ end
34
+
35
+ describe 'get /restart.json' do
36
+ it 'should return the result' do
37
+ stub_request(:get, "#{@base_url}/restart.json")
38
+ .to_return(status: 204)
39
+ @client.restart.must_equal(nil)
40
+ end
41
+ end
42
+
43
+ describe 'get /scans.json' do
44
+ it 'should return the result' do
45
+ stub_request(:get, "#{@base_url}/scans.json")
46
+ .to_return(@json_response_body)
47
+ @client.scans.must_equal(@json_response_object)
48
+ end
49
+ end
50
+
51
+ describe 'get /scans/recent.json' do
52
+ it 'should return the result' do
53
+ stub_request(:get, "#{@base_url}/scans/recent.json")
54
+ .to_return(@json_response_body)
55
+ @client.recent_scans.must_equal(@json_response_object)
56
+ end
57
+ end
58
+
59
+ describe 'get /scans/DOXIE/JPEG/IMG_0001.JPG' do
60
+ it 'should return the result' do
61
+ stub_request(:get, "#{@base_url}/scans/DOXIE/JPEG/IMG_0001.JPG")
62
+ .to_return(@json_response_body)
63
+ @client.scan('/DOXIE/JPEG/IMG_0001.JPG').must_equal(@json_response_object)
64
+ end
65
+
66
+ it 'should write to file' do
67
+ stub_request(:get, "#{@base_url}/scans/DOXIE/JPEG/IMG_0001.JPG")
68
+ .to_return(@json_response_body)
69
+ @client.scan('/DOXIE/JPEG/IMG_0001.JPG', 'test.jpg').must_equal(true)
70
+ end
71
+ end
72
+
73
+ it 'raises an authentication error exception if the response code is 401' do
74
+ stub_request(:get, "#{@base_url}/hello.json")
75
+ .to_return(status: 401)
76
+ proc { @client.hello }.must_raise(Doxie::Client::AuthenticationError)
77
+ end
78
+
79
+ it 'raises a client error exception if the response code is 4xx' do
80
+ stub_request(:get, "#{@base_url}/hello.json")
81
+ .to_return(status: 400)
82
+ proc { @client.hello }.must_raise(Doxie::Client::ClientError)
83
+ end
84
+
85
+ it 'raises a server error exception if the response code is 5xx' do
86
+ stub_request(:get, "#{@base_url}/hello.json")
87
+ .to_return(status: 500)
88
+ proc { @client.hello }.must_raise(Doxie::Client::ServerError)
89
+ end
90
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doxie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cristiano Betta
@@ -72,7 +72,11 @@ executables: []
72
72
  extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
+ - LICENSE
76
+ - README.md
77
+ - doxie.gemspec
75
78
  - lib/doxie.rb
79
+ - spec/doxie_spec.rb
76
80
  homepage: https://github.com/cbetta/doxie
77
81
  licenses:
78
82
  - MIT