box_view 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWQwZTBhYzY4NTBiMjhmZGM2ZTA5MjM2YzY3YTk3NGU0MmJmNDNkMA==
4
+ MDNiODM0YTdhMjdiMTk4ODg2ZDQ5ZjgzNjdhZTU2M2FmMWNiOTI5ZA==
5
5
  data.tar.gz: !binary |-
6
- YTAxMDM1NWQ2ZjIyZWVlZmRiYTVmOTMyOGVmY2MzOWFkYzI1ZDI2Mw==
6
+ NTRhYWVlNmE1MTEzODM0NmE1N2RkN2RhOWI2MDU3OGVjN2NiYWM4OA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- Zjg5M2EyMWJkMTYyNmE2ZjhhMzNhZjBhMDExOTg1Y2RjNjE2MTM5ZTI2Njdi
10
- MjcyNWRkMzg5OTNmY2M0OWZkYjA0NzMxMzA3N2NjMjdlMWM5OWJmNDM5M2E4
11
- NjVmMmUwYzVkMWJkOTc1Yjg0OTU2NDAxMzJkOWQzMGI1NjI5Nzk=
9
+ NTBmZjViZmI4YzQwOTI1OGVmNGRkOGY2NzRkM2JhYzZjYjE2MDRjOTEzMTk3
10
+ NjJmZjExNzIxYjM0NGM5NWQyYmFiZTQ2YTk2ZjU1YjhhZDg2NmY4ZWI3ZWFi
11
+ MDE3NDExMTg1NWNlODlhYTczZTgxNDg4ZGZiMDU1Y2JkYzJlMDA=
12
12
  data.tar.gz: !binary |-
13
- M2UyMGY0MWJlNTEyYTE2YWU0ZmNmZTBiMWMyMGEzY2YzNmZmNzBmZjRjMzU0
14
- OGIxYWUxNjc3ODI1ZDVhODUyMzM4YjA5YjU1NjIwMWIwMGY5MDVjYmJjMTY5
15
- YTg0MWVmY2NmZjZlZjRlM2ViZDIwMjgwMWMxOGE5YmZlNTc0YmU=
13
+ YWQ3MDljNjhlMDBlYWE1Y2JjNjg2Y2JlMDE4NGFjMWI3MjljZDA4NjJkMDEz
14
+ ZmMzNGJkMmY5NmU2MTQ4M2NlMzAyNGM5YWUxZDJmMTI0OTI0MTI0OTBjMzhj
15
+ Y2I2NGUyM2M5MGYzNzExNDc2NTY5ZjYwNGVhMzA5NzM5NjE5NTQ=
data/box_view.gemspec CHANGED
@@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = "https://github.com/reillyforshaw/box_view"
14
14
  spec.license = "MIT"
15
15
 
16
+ spec.required_ruby_version = '>= 1.9'
17
+
16
18
  spec.files = `git ls-files`.split($/)
17
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
@@ -20,4 +22,5 @@ Gem::Specification.new do |spec|
20
22
 
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
23
26
  end
data/lib/box_view/http.rb CHANGED
@@ -2,7 +2,19 @@ module BoxView
2
2
  module Http
3
3
 
4
4
  require 'time'
5
-
5
+
6
+ class Error < BoxView::Error; end
7
+
8
+ class BadRequestError < BoxView::Http::Error; end
9
+ class RetryNeededError < BoxView::Http::Error
10
+ attr_reader :retry_after
11
+
12
+ def initialize(msg, retry_after)
13
+ @retry_after = retry_after
14
+ super(msg)
15
+ end
16
+ end
17
+
6
18
  def base_uri(path, params = {})
7
19
  uri = URI.parse("https://view-api.box.com")
8
20
  uri.path = path
@@ -52,17 +64,32 @@ module BoxView
52
64
  res = n.start do |http|
53
65
  http.request(req)
54
66
  end
67
+ check_for_error(res)
55
68
  parse ? parse_response(res) : res.body
56
69
  end
57
70
 
58
- def parse_response(res)
59
- begin
60
- JSON.parse(res.body)
61
- rescue JSON::ParserError
62
- nil
71
+ def check_for_error(res)
72
+ case res
73
+ when Net::HTTPAccepted
74
+ if res['Retry-After']
75
+ raise BoxView::Http::RetryNeededError.new('Retry Needed', res['Retry-After'])
76
+ end
77
+ when Net::HTTPBadRequest
78
+ msg = 'Bad Request'
79
+ if err_dets = error_details(res)
80
+ msg += " (#{err_dets})"
81
+ end
82
+
83
+ raise BoxView::Http::BadRequestError.new(msg)
63
84
  end
64
85
  end
65
86
 
87
+ def parse_response(res)
88
+ JSON.parse(res.body)
89
+ rescue JSON::ParserError
90
+ nil
91
+ end
92
+
66
93
  def convert_params(params)
67
94
  params.each_pair do |key, val|
68
95
  if [Date, Time, DateTime].include?(val.class)
@@ -74,6 +101,16 @@ module BoxView
74
101
 
75
102
  private
76
103
 
104
+ def error_details(res)
105
+ if resp_json = parse_response(res)
106
+ if details = resp_json['details']
107
+ details.map { |d| [d['field'], d['message']].join(': ') }.join(' ')
108
+ end
109
+ end
110
+ rescue
111
+ nil
112
+ end
113
+
77
114
  def api_prefix()
78
115
  "/1"
79
116
  end
@@ -1,9 +1,9 @@
1
1
  module BoxView
2
2
  module Models
3
3
 
4
- class ReadOnlyAttribute < Exception; end
5
- class ResourceNotSaved < Exception; end
6
-
4
+ class ReadOnlyAttribute < BoxView::Error; end
5
+ class ResourceNotSaved < BoxView::Error; end
6
+
7
7
  class Base
8
8
 
9
9
  require 'time'
@@ -83,4 +83,4 @@ module BoxView
83
83
 
84
84
  end
85
85
  end
86
- end
86
+ end
@@ -1,3 +1,3 @@
1
1
  module BoxView
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/box_view.rb CHANGED
@@ -4,6 +4,8 @@ require "net/http"
4
4
  require "json"
5
5
 
6
6
  module BoxView
7
+ class Error < StandardError; end
8
+
7
9
  {
8
10
  Session: 'session',
9
11
  Http: 'http'
data/spec/http_spec.rb CHANGED
@@ -5,11 +5,66 @@ describe BoxView::Http do
5
5
 
6
6
  describe "#parse_response" do
7
7
  context "when given ill-formed JSON" do
8
- let(:response) { response = double("Net::HTTPResponse", body: "foo") }
9
-
8
+ let(:response) { double("Net::HTTPResponse", body: "foo") }
9
+
10
10
  it "should return nil" do
11
11
  expect(http.parse_response(response)).to be_nil
12
12
  end
13
13
  end
14
14
  end
15
- end
15
+
16
+ describe '#check_for_error' do
17
+ let(:response) { }
18
+ subject { http.check_for_error(response) }
19
+
20
+ context 'given a 202 response' do
21
+ let(:response) { Net::HTTPAccepted.new('1.1', '202', 'ACCEPTED') }
22
+
23
+ context 'that does not have a Retry-After header' do
24
+ it 'should not raise an error' do
25
+ expect { subject }.not_to raise_error
26
+ end
27
+ end
28
+
29
+ context 'that does have a Retry-After header' do
30
+ let(:response) { super().tap { |r| r['Retry-After'] = '10' } }
31
+ it 'should raise a BoxView::Http:RetryNeeded' do
32
+ expect { subject }.to raise_error(BoxView::Http::RetryNeededError) do |error|
33
+ error.retry_after.should == '10'
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ context 'given a 400 response' do
40
+ let(:body) { }
41
+ let(:response) do
42
+ Net::HTTPBadRequest.new('1.1', '400', 'BAD REQUEST').tap do |r|
43
+ r.stub(:body).and_return(body)
44
+ end
45
+ end
46
+
47
+ context 'without a parseable error message' do
48
+ let(:body) { 'NOT JSON' }
49
+
50
+ it 'should raise a BoxView::Http:BadRequest' do
51
+ expect { subject }.to raise_error(BoxView::Http::BadRequestError)
52
+ end
53
+ end
54
+
55
+ context 'with a parseable error message' do
56
+ let(:body) do
57
+ '{"message": "Bad request",
58
+ "type": "error",
59
+ "details": [{"field": "height",
60
+ "message": "Ensure this value is less than or equal to 768."}],
61
+ "request_id": "999605a17b974850baaccbe2ae479c75"}'
62
+ end
63
+
64
+ it 'should raise a BoxView::Http:BadRequest with a message indicating the problem' do
65
+ expect { subject }.to raise_error(BoxView::Http::BadRequestError, /height/)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: box_view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reilly Forshaw
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-18 00:00:00.000000000 Z
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
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'
41
55
  description: API client for Box View
42
56
  email:
43
57
  - reilly.forshaw@goclio.com
@@ -88,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
102
  requirements:
89
103
  - - ! '>='
90
104
  - !ruby/object:Gem::Version
91
- version: '0'
105
+ version: '1.9'
92
106
  required_rubygems_version: !ruby/object:Gem::Requirement
93
107
  requirements:
94
108
  - - ! '>='