boxview.rb 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,144 @@
1
+ module BoxView
2
+ class Session
3
+
4
+ PATH = '/sessions'
5
+
6
+ class << self
7
+
8
+ attr_accessor :expiration_date, :duration, :retry_after, :is_downloadable
9
+
10
+ # Description:
11
+ # => The getter method for the duration that a session will last.
12
+ # No Params!
13
+ # Note:
14
+ # => Raises an error if the duration is nil when retrieved.
15
+ def duration
16
+ raise BoxView::Errors::DurationNotFound if @duration.nil?
17
+ @duration
18
+ end
19
+
20
+ # Description:
21
+ # => The getter method for the expiration date that a session will last.
22
+ # No Params!
23
+ # Note:
24
+ # => Raises an error if the expiration date is nil when retrieved.
25
+ def expiration_date
26
+ raise BoxView::Errors::ExpirationDateNotFound if @expiration_date.nil?
27
+ @expiration_date
28
+ end
29
+
30
+ ### BEGIN Session HTTP Request ###
31
+
32
+ # Description:
33
+ # => Creates a session for viewing the HTML ready document
34
+ # Note:
35
+ # => No Required Params!
36
+ # => params can be defined elsewhere.
37
+ # => document id must be defined.
38
+ def create(options = {})
39
+ BoxView.document_id = options[:document_id] if options[:document_id]
40
+ duration = options[:duration] if options[:duration]
41
+ expiration_date = options[:expiration_date] if options[:expiration_date]
42
+ is_downloadable = options[:is_downloadable] if options[:is_downloadable]
43
+ response = BoxView.post session_path, body: json_data, headers: BoxView.headers
44
+ response_handler response
45
+ return response
46
+ end
47
+
48
+ ### END Session HTTP Request ###
49
+
50
+ # Description:
51
+ # => A path that is used for all session related requests.
52
+ # No Params!
53
+ def session_path
54
+ "#{BoxView::BASE_PATH}#{PATH}"
55
+ end
56
+
57
+ # Description:
58
+ # => Convenience method to make the session last for a thousand years
59
+ # No Params!
60
+ def never_expire
61
+ expiration_date Time.now + (365.25 * 24 * 60 * 60 * 1000).to_i
62
+ end
63
+
64
+ # Description:
65
+ # => A convenience method to return the viewer url that can be used after generating a session.
66
+ # Note:
67
+ # => No Required Params!
68
+ # => Defaults to using a generated session_id.
69
+ def viewer_url(session_id = nil)
70
+ unless session_id then session_id = BoxView.session_id end
71
+ "#{session_path}/#{session_id}/view?theme=light"
72
+ end
73
+
74
+ # Description:
75
+ # => A convenience method to return the asset url that is used in viewerjs.
76
+ # Optional Params:
77
+ # => session_id
78
+ # Note:
79
+ # => No Required Params!
80
+ # => Defaults to using a generated session_id.
81
+ def viewerjs_url(session_id = nil)
82
+ unless session_id then session_id = BoxView.session_id end
83
+ "#{session_path}/#{session_id}/assets"
84
+ end
85
+
86
+ # Description:
87
+ # => Convenience method to open the viewer url in a default browser.
88
+ # Optional Params:
89
+ # => A url to open in a default browser.
90
+ # Note:
91
+ # => No Required Params!
92
+ # => A session must have been generated in order for a url to be generated.
93
+ def view(link = nil)
94
+ unless link then link = viewer_url end
95
+ if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
96
+ system "start #{link}"
97
+ elsif RbConfig::CONFIG['host_os'] =~ /darwin/
98
+ system "open #{link}"
99
+ elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
100
+ system "xdg-open #{link}"
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ # Description:
107
+ # => Response handler for the create request of a session.
108
+ # No Params!
109
+ def response_handler(response)
110
+ case response.code
111
+ when 201 # Done converting
112
+ parsed = JSON.parse response.body
113
+ session_id = parsed["id"]
114
+ BoxView.session_id = session_id
115
+ when 202 # Session not ready yet
116
+ retry_after response['Retry-After']
117
+ when 400 # An error occurred while converting the document or the document does not exist
118
+ raise BoxView::Errors::DocumentConversionFailed
119
+ else
120
+ raise BoxView::Errors::SessionNotGenerated
121
+ end
122
+ end
123
+
124
+ # Description:
125
+ # => The JSON data that is sent in the create request of a session.
126
+ # No Params!
127
+ def json_data # Does a duration or an expiration date need to exist?
128
+ data = {}
129
+ data[:document_id] = BoxView.document_id
130
+ data[:is_downloadable] = is_downloadable if is_downloadable
131
+ if !@duration.nil? && @expiration_date.nil? # duration
132
+ data[:duration] = duration
133
+ elsif @duration.nil? && !@expiration_date.nil? # expiration
134
+ data[:expires_at] = expiration_date
135
+ elsif !@duration.nil? && !@expiration_date.nil? # both, use expiration date
136
+ data[:expires_at] = expiration_date
137
+ else
138
+ # Default will be used
139
+ end
140
+ return data.to_json
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,3 @@
1
+ module BoxView
2
+ VERSION = '0.0.9'
3
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe BoxView::Document, '#document_id' do
4
+
5
+ it 'should raise error when document id is nil' do
6
+ expect{BoxView.document_id}.to raise_error(BoxView::Errors::DocumentIdNotFound)
7
+ end
8
+
9
+ end
10
+
11
+ describe BoxView::Document, '#url' do
12
+ it 'should raise error when url is nil' do
13
+ BoxView::Document.url = nil
14
+ expect{BoxView::Document.url}.to raise_error(BoxView::Errors::UrlNotFound)
15
+ end
16
+
17
+ it 'should return url when defined' do
18
+ BoxView::Document.url = 'http://imgur.com/cats.jpeg'
19
+ expect(BoxView::Document.url).not_to be_nil
20
+ end
21
+ end
22
+
23
+ describe BoxView::Document, '#type' do
24
+ it 'should raise error when setting with invalid type' do
25
+ expect{BoxView::Document.type = 'jpeg'}.to raise_error(BoxView::Errors::TypeNotFound)
26
+ end
27
+
28
+ it 'should return when type is valid' do
29
+ expect(BoxView::Document.type = 'pdf').not_to be_nil
30
+ end
31
+ end
32
+
33
+ describe BoxView::Document, '#dimensions' do
34
+
35
+ it 'should raise error when dimensions are nil' do
36
+ expect{BoxView::Document.dimensions}.to raise_error(BoxView::Errors::DimensionsNotFound)
37
+ end
38
+
39
+ it 'should raise error when height is nil, but width is defined' do
40
+ BoxView::Document.width = 100
41
+ BoxView::Document.height = nil
42
+ expect{BoxView::Document.dimensions}.to raise_error(BoxView::Errors::DimensionsNotFound)
43
+ end
44
+
45
+ it 'should raise error when width is nil, but height is defined' do
46
+ BoxView::Document.width = nil
47
+ BoxView::Document.height = 100
48
+ expect{BoxView::Document.dimensions}.to raise_error(BoxView::Errors::DimensionsNotFound)
49
+ end
50
+
51
+ end
52
+
53
+ describe BoxView::Document, '#thumbnail_params' do
54
+
55
+ it 'should raise when dimensions are nil' do
56
+ expect{BoxView::Document.thumbnail_params}.to raise_error(BoxView::Errors::DimensionsNotFound)
57
+ end
58
+
59
+ it 'should raise when height is nil, but width is defined' do
60
+ BoxView::Document.width = 100
61
+ BoxView::Document.height = nil
62
+ expect{BoxView::Document.thumbnail_params}.to raise_error(BoxView::Errors::DimensionsNotFound)
63
+ end
64
+
65
+ it 'should raise when width is nil, but height is defined' do
66
+ BoxView::Document.width = nil
67
+ BoxView::Document.height = 100
68
+ expect{BoxView::Document.thumbnail_params}.to raise_error(BoxView::Errors::DimensionsNotFound)
69
+ end
70
+ end
71
+ describe BoxView::Document, '#create' do
72
+ xit 'should raise when receiving a bad request' do
73
+ end
74
+
75
+ it 'should raise when receiving a bad api key' do
76
+ BoxView.api_key = 'somesortofbadapikey'
77
+ BoxView::Document.url = 'http://imgur.com/cats.jpeg'
78
+ expect{BoxView::Document.create}.to raise_error(BoxView::Errors::DocumentIdNotGenerated)
79
+ end
80
+
81
+ it 'should return the response when sent a good request (200..202)' do
82
+ BoxView.api_key = API_KEY
83
+ BoxView::Document.url = 'http://i.imgur.com/4RZkFbE.png'
84
+ response = BoxView::Document.create
85
+ expect(200..202).to cover(response.code)
86
+ end
87
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe BoxView::Errors do
4
+
5
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe BoxView::Session, '#expiration_date' do
4
+
5
+ it 'should raise error when expiration date is nil' do
6
+ BoxView::Session.expiration_date = nil
7
+ expect{BoxView::Session.expiration_date}.to raise_error(BoxView::Errors::ExpirationDateNotFound)
8
+ end
9
+
10
+ it 'should return when expiration date is defined' do
11
+ BoxView::Session.expiration_date = Time.now
12
+ expect(BoxView::Session.expiration_date).not_to be_nil
13
+ end
14
+
15
+ end
16
+
17
+ describe BoxView::Session, '#duration' do
18
+
19
+ it 'should raise error when duration is nil' do
20
+ BoxView::Session.duration = nil
21
+ expect{BoxView::Session.duration}.to raise_error(BoxView::Errors::DurationNotFound)
22
+ end
23
+
24
+ it 'should return when duration is defined' do
25
+ BoxView::Session.duration = 100
26
+ expect(BoxView::Session.duration).not_to be_nil
27
+ end
28
+
29
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe BoxView::VERSION do
4
+ it 'should not be nil' do
5
+ expect(BoxView::VERSION).to_not be_nil
6
+ end
7
+
8
+ it 'should be a string' do
9
+ expect(BoxView::VERSION).to be_an_instance_of(String)
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe BoxView, "#headers" do
4
+ it 'should raise error when api key is nil' do
5
+ BoxView.api_key = nil
6
+ expect{BoxView.headers}.to raise_error(BoxView::Errors::ApiKeyNotFound)
7
+ end
8
+ it 'should return headers when api key is defined' do
9
+ BoxView.api_key = "a1s2d9d3dg7d7s7"
10
+ expect(BoxView.headers).not_to be_nil
11
+ end
12
+ it 'should return when document id is defined' do
13
+ BoxView.document_id = "slkslkdf234234"
14
+ expect(BoxView.document_id).not_to be_nil
15
+ end
16
+ it 'should raise error when document id is nil' do
17
+ BoxView.document_id = nil
18
+ expect{BoxView.document_id}.to raise_error(BoxView::Errors::DocumentIdNotFound)
19
+ end
20
+ it 'should return when session id is defined' do
21
+ BoxView.session_id = "sfsdf23242332"
22
+ expect(BoxView.session_id).not_to be_nil
23
+ end
24
+ it 'should raise error when session id is nil' do
25
+ BoxView.session_id = nil
26
+ expect{BoxView.session_id}.to raise_error(BoxView::Errors::SessionIdNotFound)
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ require 'boxview'
2
+
3
+ API_KEY = ENV["BOXVIEW_APIKEY"]
4
+
5
+ # ToDo: Stub HTTParty Responses using https://github.com/bblimke/webmock/
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boxview.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9
5
+ platform: ruby
6
+ authors:
7
+ - Vincent Taverna
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httmultiparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.14
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.3.14
69
+ description: Box View converts PDF and Office documents to HTML thus enabling these
70
+ files to be easily embedded into web and mobile applications.
71
+ email:
72
+ - vinnymac@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .ruby-gemset
79
+ - .ruby-version
80
+ - .travis.yml
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - boxview.rb.gemspec
86
+ - lib/boxview.rb
87
+ - lib/boxview/document.rb
88
+ - lib/boxview/errors.rb
89
+ - lib/boxview/session.rb
90
+ - lib/boxview/version.rb
91
+ - spec/lib/boxview/document_spec.rb
92
+ - spec/lib/boxview/errors_spec.rb
93
+ - spec/lib/boxview/session_spec.rb
94
+ - spec/lib/boxview/version_spec.rb
95
+ - spec/lib/boxview_spec.rb
96
+ - spec/spec_helper.rb
97
+ homepage: http://developers.box.com/view/
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.2.2
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Wrapper for the BoxView API
121
+ test_files:
122
+ - spec/lib/boxview/document_spec.rb
123
+ - spec/lib/boxview/errors_spec.rb
124
+ - spec/lib/boxview/session_spec.rb
125
+ - spec/lib/boxview/version_spec.rb
126
+ - spec/lib/boxview_spec.rb
127
+ - spec/spec_helper.rb