uploadcare-ruby 1.0.1.rc2 → 1.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc5991ac44cf2404d37ae2d374481b9f8bc2c0cc
4
- data.tar.gz: 9873a042e14e976d083a6da3f03bfdb3036bd0f1
3
+ metadata.gz: af26486fc47d098d6568263844e15b5d6f6a4fca
4
+ data.tar.gz: fc9b11f7d79f0ee2b95f3e88e2f7588972f1df2b
5
5
  SHA512:
6
- metadata.gz: d2efd43898961c03cb37287ffef0fd82b89b80a9d0fcfc496fb6876a11524d653d27c21929c08a6ddf05453f3a9304895d77e5d1f8b53df491615d9e9f5e3e00
7
- data.tar.gz: cac80a83f934fc8fd21c09057af376712aa2d47c7766c1f10f8bf6f4ac5bcc37f7a396bdc79100dcee760314e9e997b5c69330b825ce8249f8ac87ea0ba6979f
6
+ metadata.gz: 9fb9293930f8af75f06ee7c3cce438c01dc8115634a0b2634a79ef796c1c9c2816642a6d06f61c1b8f3e99e98ab5bd5a5d981480a922e162f485b14a1b942f89
7
+ data.tar.gz: ce565ece9f629a809d5142380b7704ece6b0175f9257a894e92ee0ed59141ef4d602f2e2299be12dec12257b26afe890d44281b3601070e890a99a1aaeb6f383
data/.DS_Store CHANGED
Binary file
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
1
  Gemfile.lock
2
+ .ruby-gemset
3
+ .ruby-version
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in uploadcare-ruby.gemspec
4
+ gem 'uploadcare-ruby', path: "/Users/romanonthego/Code/uploadcare/uploadcare-ruby/"
4
5
  gemspec
data/README.md CHANGED
@@ -371,6 +371,58 @@ New groups created by :create_group method is loaded by default.
371
371
  # #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
372
372
  ```
373
373
 
374
+ ## Errors handling
375
+ From version 1.0.2 we have a custom exceptions which will raise when Uploadcare service return something with 4xx or 5xx HTTP status.
376
+
377
+ List of custom errors:
378
+
379
+ ```ruby
380
+ 400 => Uploadcare::Error::RequestError::BadRequest,
381
+ 401 => Uploadcare::Error::RequestError::Unauthorized,
382
+ 403 => Uploadcare::Error::RequestError::Forbidden,
383
+ 404 => Uploadcare::Error::RequestError::NotFound,
384
+ 406 => Uploadcare::Error::RequestError::NotAcceptable,
385
+ 408 => Uploadcare::Error::RequestError::RequestTimeout,
386
+ 422 => Uploadcare::Error::RequestError::UnprocessableEntity,
387
+ 429 => Uploadcare::Error::RequestError::TooManyRequests,
388
+ 500 => Uploadcare::Error::ServerError::InternalServerError,
389
+ 502 => Uploadcare::Error::ServerError::BadGateway,
390
+ 503 => Uploadcare::Error::ServerError::ServiceUnavailable,
391
+ 504 => Uploadcare::Error::ServerError::GatewayTimeout
392
+ ```
393
+
394
+ so now you could escape particular error (in that case 404: Not Found error):
395
+
396
+ ```ruby
397
+ begin
398
+ @connection.send :get, '/random_url/', {}
399
+ rescue Uploadcare::Error::RequestError::NotFound => e
400
+ nil
401
+ end
402
+ ```
403
+
404
+ ... any request error (covers all 4xx status codes):
405
+
406
+ ```ruby
407
+ begin
408
+ @connection.send :get, '/random_url/', {}
409
+ rescue Uploadcare::Error::RequestError => e
410
+ nil
411
+ end
412
+ ```
413
+
414
+ ...and actually any Uploadcare service errors:
415
+
416
+ ```ruby
417
+ begin
418
+ @connection.send :get, '/random_url/', {}
419
+ rescue Uploadcare::Error => e
420
+ nil
421
+ end
422
+ ```
423
+
424
+ Please note what almost all actions depends on Uploadcare servers and it will be wise of you to expect that servers will return error code (at least some times).
425
+
374
426
  ## Testing
375
427
 
376
428
  Run `bundle exec rspec`.
data/lib/uploadcare.rb CHANGED
@@ -26,18 +26,4 @@ module Uploadcare
26
26
  def self.user_agent
27
27
  USER_AGENT
28
28
  end
29
-
30
- UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
31
-
32
- GROUP_UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}~(?<count>\d+)$/
33
-
34
- CDN_URL_FILE_REGEX = /
35
- (?<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})
36
- (?:\/-\/(?<operations>.*?))?\/?$
37
- /ix
38
-
39
- CDN_URL_GROUP_REGEX = /
40
- (?<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}~(?<count>\d+))
41
- (?:\/-\/(?<operations>.*?))?\/?$
42
- /ix
43
29
  end
@@ -1,6 +1,9 @@
1
1
  require 'json'
2
2
  require 'ostruct'
3
-
3
+ Dir[File.dirname(__FILE__) + '/utils/*.rb'].each {|file| require file }
4
+ Dir[File.dirname(__FILE__) + '/errors/*.rb'].each {|file| require file }
5
+ Dir[File.dirname(__FILE__) + '/rest/middlewares/*.rb'].each {|file| require file }
6
+ Dir[File.dirname(__FILE__) + '/rest/connections/*.rb'].each {|file| require file }
4
7
  Dir[File.dirname(__FILE__) + '/api/*.rb'].each {|file| require file }
5
8
  Dir[File.dirname(__FILE__) + '/resources/*.rb'].each {|file| require file }
6
9
 
@@ -1,5 +1,3 @@
1
- require "uri"
2
-
3
1
  module Uploadcare
4
2
  module FileApi
5
3
  def file uuid_or_cdn_url
@@ -31,8 +31,8 @@ module Uploadcare
31
31
  }
32
32
 
33
33
  data.merge! files
34
- post = parse(upload_request :post, "/group/", data)
35
- group = Uploadcare::Api::Group.new self, post["id"], post
34
+ post = @upload_connection.send :post, "/group/", data
35
+ group = Uploadcare::Api::Group.new self, post.body["id"], post.body
36
36
  end
37
37
  end
38
38
  end
@@ -1,19 +1,19 @@
1
1
  require 'json'
2
2
 
3
- require 'uploadcare/api/connections'
4
-
5
3
  module Uploadcare
6
4
  module RawApi
7
5
 
8
6
  def initialize options={}
9
- @options = Uploadcare::default_settings.merge(options)
7
+ @options = Uploadcare::default_settings.merge(options)
8
+ @api_connection = Uploadcare::Connections::ApiConnection.new(@options)
9
+ @upload_connection = Uploadcare::Connections::UploadConnection.new(@options)
10
10
  end
11
11
 
12
12
 
13
13
  # basic request method
14
14
  def request method = :get, path = "/files/", params = {}
15
- response = send_request(method, path, params)
16
- parse(response)
15
+ response = @api_connection.send method, path, params
16
+ response.body
17
17
  end
18
18
  alias_method :api_request, :request
19
19
 
@@ -29,6 +29,7 @@ module Uploadcare
29
29
  request :post, path, params
30
30
  end
31
31
 
32
+
32
33
  # request with PUT verb
33
34
  def put path= "/files/", params={}
34
35
  request :put, path, params
@@ -39,35 +40,5 @@ module Uploadcare
39
40
  def delete path= "/files/", params={}
40
41
  request :delete, path, params
41
42
  end
42
-
43
-
44
- protected
45
- def send_request method, path, params={}
46
- connection = Uploadcare::Connections.api_connection(@options)
47
- response = connection.send method, path, params
48
- end
49
-
50
-
51
- def parse response
52
- begin
53
- object = JSON.parse(response.body)
54
- rescue JSON::ParserError
55
- object = false
56
- end
57
-
58
- # and returning the object (file actually) or raise new error
59
- if response.status < 300
60
- object
61
- else
62
- message = "HTTP code #{response.status}"
63
- if object # add active_support god damn it
64
- message += ": #{object["detail"]}"
65
- else
66
- message += ": unknown error occured."
67
- end
68
-
69
- raise ArgumentError.new(message)
70
- end
71
- end
72
43
  end
73
44
  end
@@ -24,39 +24,33 @@ module Uploadcare
24
24
 
25
25
 
26
26
  def upload_files files
27
- if files.select {|f| !f.kind_of?(File)}.any?
28
- raise ArgumentError.new "one or more of given files is not actually files"
29
- else
30
- data = {
31
- UPLOADCARE_PUB_KEY: @options[:public_key],
32
- }
33
-
34
- files.each_with_index do |f, i|
35
- data["file[#{i}]"] = Faraday::UploadIO.new(f.path, extract_mime_type(f))
36
- end
27
+ raise ArgumentError.new "one or more of given files is not actually files" if files.select {|f| !f.kind_of?(File)}.any?
37
28
 
38
- response = upload_request :post, '/base/', data
39
- uuids = upload_parse(response)
29
+ data = {UPLOADCARE_PUB_KEY: @options[:public_key]}
40
30
 
41
- files = uuids.values.map! {|f| Uploadcare::Api::File.new self, f }
31
+ files.each_with_index do |file, i|
32
+ data["file[#{i}]"] = build_upload_io(file)
42
33
  end
34
+
35
+ response = @upload_connection.send :post, '/base/', data
36
+ uuids = response.body
37
+
38
+ files = uuids.values.map! {|f| Uploadcare::Api::File.new self, f }
43
39
  end
44
40
 
45
41
 
46
42
  # upload file to servise
47
43
  def upload_file file
48
- if file.kind_of?(File)
49
- mime_type = extract_mime_type(file)
50
-
51
- response = upload_request :post, '/base/', {
52
- UPLOADCARE_PUB_KEY: @options[:public_key],
53
- file: Faraday::UploadIO.new(file.path, mime_type)
54
- }
55
- uuid = upload_parse(response)["file"]
56
- Uploadcare::Api::File.new self, uuid
57
- else
58
- raise ArgumentError.new 'expecting File object'
59
- end
44
+ raise ArgumentError.new 'expecting File object' unless file.kind_of?(File)
45
+
46
+ response = @upload_connection.send :post, '/base/', {
47
+ UPLOADCARE_PUB_KEY: @options[:public_key],
48
+ file: build_upload_io(file)
49
+ }
50
+
51
+ uuid = response.body["file"]
52
+
53
+ Uploadcare::Api::File.new self, uuid
60
54
  end
61
55
 
62
56
  # create file is the same as uplaod file
@@ -66,20 +60,19 @@ module Uploadcare
66
60
  #upload from url
67
61
  def upload_url url
68
62
  uri = URI.parse(url)
63
+
64
+ raise ArgumentError.new 'invalid url was given' unless uri.kind_of?(URI::HTTP)
69
65
 
70
- if uri.kind_of?(URI::HTTP) # works both for HTTP and HTTPS as HTTPS inherits from HTTP
71
- token = get_token(url)
72
-
73
- while (response = get_status_response(token))['status'] == 'unknown'
74
- sleep 0.5
75
- end
76
-
77
- raise ArgumentError.new(response['error']) if response['status'] == 'error'
78
- uuid = response['file_id']
79
- Uploadcare::Api::File.new self, uuid
80
- else
81
- raise ArgumentError.new 'invalid url was given'
66
+ token = get_token(url)
67
+
68
+ while (response = get_status_response(token))['status'] == 'unknown'
69
+ sleep 0.5
82
70
  end
71
+
72
+ raise ArgumentError.new(response['error']) if response['status'] == 'error'
73
+
74
+ uuid = response['file_id']
75
+ Uploadcare::Api::File.new self, uuid
83
76
  end
84
77
  alias_method :upload_from_url, :upload_url
85
78
 
@@ -87,33 +80,29 @@ module Uploadcare
87
80
 
88
81
 
89
82
  protected
83
+ # DEPRECATRED but still works
90
84
  def upload_request method, path, params = {}
91
- connection = Uploadcare::Connections.upload_connection(@options)
92
- response = connection.send method, path, params
85
+ response = @upload_connection.send method, path, params
93
86
  end
94
87
 
95
-
96
- def upload_parse response
97
- raise ArgumentError.new(response.body) if response.status > 200
98
- begin
99
- JSON.parse(response.body)
100
- rescue JSON::ParserError
101
- response.body
102
- end
88
+ def build_upload_io file
89
+ Faraday::UploadIO.new file.path, extract_mime_type(file)
103
90
  end
104
91
 
105
92
 
106
93
  private
107
94
  def get_status_response token
108
- upload_parse(upload_request(:post, '/from_url/status/', {token: token}))
95
+ response = @upload_connection.send :post, '/from_url/status/', {token: token}
96
+ response.body
109
97
  end
110
98
 
111
99
 
112
100
  def get_token url
113
- response = upload_request :post, '/from_url/', { source_url: url, pub_key: @options[:public_key] }
114
- token = upload_parse(response)["token"]
101
+ response = @upload_connection.send :post, '/from_url/', { source_url: url, pub_key: @options[:public_key] }
102
+ token = response.body["token"]
115
103
  end
116
104
 
105
+
117
106
  def extract_mime_type file
118
107
  types = MIME::Types.of(file.path)
119
108
  types[0].content_type
@@ -0,0 +1,64 @@
1
+ module Uploadcare
2
+ class Error < ::StandardError
3
+
4
+ def self.define_error code, klass, message
5
+ class_eval <<-EOD
6
+ class #{klass} < self
7
+ def initialize( message = nil )
8
+ super( message || "HTTP #{code} - #{message}" )
9
+ end
10
+ end
11
+ EOD
12
+ end
13
+
14
+ def self.errors
15
+ @errors ||= {
16
+ 400 => Uploadcare::Error::RequestError::BadRequest,
17
+ 401 => Uploadcare::Error::RequestError::Unauthorized,
18
+ 403 => Uploadcare::Error::RequestError::Forbidden,
19
+ 404 => Uploadcare::Error::RequestError::NotFound,
20
+ 406 => Uploadcare::Error::RequestError::NotAcceptable,
21
+ 408 => Uploadcare::Error::RequestError::RequestTimeout,
22
+ 422 => Uploadcare::Error::RequestError::UnprocessableEntity,
23
+ 429 => Uploadcare::Error::RequestError::TooManyRequests,
24
+ 500 => Uploadcare::Error::ServerError::InternalServerError,
25
+ 502 => Uploadcare::Error::ServerError::BadGateway,
26
+ 503 => Uploadcare::Error::ServerError::ServiceUnavailable,
27
+ 504 => Uploadcare::Error::ServerError::GatewayTimeout,
28
+ }
29
+ end
30
+
31
+
32
+ # Overall service error so you could escape it no matter what code is return
33
+
34
+ # all 4xx error
35
+ class RequestError < self;
36
+ def initialize( message = nil )
37
+ super( message || "HTTP 4xx - a request error occured." )
38
+ end
39
+
40
+ define_error 400, "BadRequest", "the request cannot be fulfilled due to bad syntax."
41
+ define_error 401, "Unauthorized", "authentication is required and has failed or has not yet been provided."
42
+ define_error 403, "Forbidden", "the request was a valid request, but the server is refusing to respond to it."
43
+ define_error 404, "NotFound", "the requested resource could not be found."
44
+ define_error 406, "NotAcceptable", "the requested resource is only capable of generating content"
45
+ define_error 408, "RequestTimeout", "the server timed out waiting for the request."
46
+ define_error 422, "UnprocessableEntity", "the request was well-formed but was unable to be followed due to semantic errors."
47
+ define_error 429, "TooManyRequests", "too many requests in a given amount of time."
48
+ end
49
+
50
+ # all 5xx error
51
+ class ServerError < self;
52
+ def initialize( message = nil )
53
+ super( message || "HTTP 5xx - a server error occured." )
54
+ end
55
+
56
+ define_error 500, "InternalServerError", "some error occured on server."
57
+ define_error 502, "BadGateway", "received an invalid response from the upstream server."
58
+ define_error 503, "ServiceUnavailable", "the server is currently unavailable."
59
+ define_error 504, "GatewayTimeout", "the server did not receive a timely response from the upstream server."
60
+ end
61
+
62
+ # specific error
63
+ end
64
+ end
@@ -4,15 +4,7 @@ module Uploadcare
4
4
  class Api
5
5
  class File < OpenStruct
6
6
  def initialize api, uuid_or_cdn_url, data=nil
7
- result = Uploadcare::Parser.parse(uuid_or_cdn_url)
8
-
9
- unless result.is_a?(Uploadcare::Parser::File)
10
- msg = "invalid CDN URL or UUID was given for file: #{uuid_or_cdn_url}."
11
- if result.is_a?(Uploadcare::Parser::Group)
12
- msg = msg + "\n Group UUID was given. Try call @api.group if it is what you intended."
13
- end
14
- raise msg
15
- end
7
+ result = Uploadcare::Parser.parse_file_string uuid_or_cdn_url
16
8
 
17
9
  file = {uuid: result["uuid"], operations: result["operations"]}
18
10
 
@@ -4,18 +4,9 @@ module Uploadcare
4
4
  class Api
5
5
  class Group < OpenStruct
6
6
  def initialize api, uuid_or_cdn_url, data=nil
7
- result = Uploadcare::Parser.parse(uuid_or_cdn_url)
8
-
9
- unless result.is_a?(Uploadcare::Parser::Group)
10
- msg = "invalid CDN URL or UUID was given for group: #{uuid_or_cdn_url}."
11
- if result.is_a?(Uploadcare::Parser::File)
12
- msg = msg + "\n File UUID was given. Try call @api.file if it is what you intended."
13
- end
14
- raise msg
15
- end
7
+ result = Uploadcare::Parser.parse_group_string(uuid_or_cdn_url)
16
8
 
17
9
  @api = api
18
- # self.files_count = result["count"]
19
10
  group = {uuid: result["uuid"], files_count: result["count"]}
20
11
  super group
21
12
 
@@ -0,0 +1,21 @@
1
+ require 'faraday'
2
+ require "faraday_middleware"
3
+ module Uploadcare
4
+ module Connections
5
+ class ApiConnection < Faraday::Connection
6
+ def initialize options
7
+ super options[:api_url_base] do |frd|
8
+ frd.request :url_encoded
9
+ frd.use ::FaradayMiddleware::FollowRedirects, limit: 3
10
+ frd.adapter :net_http # actually, default adapter, just to be clear
11
+ frd.headers['Authorization'] = "Uploadcare.Simple #{options[:public_key]}:#{options[:private_key]}"
12
+ frd.headers['Accept'] = "application/vnd.uploadcare-v#{options[:api_version]}+json"
13
+ frd.headers['User-Agent'] = Uploadcare::user_agent
14
+
15
+ frd.response :raise_error
16
+ frd.response :parse_json
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require "faraday"
2
+
3
+ module Uploadcare
4
+ module Connections
5
+ class UploadConnection < Faraday::Connection
6
+ def initialize options
7
+ ca_path = '/etc/ssl/certs' if File.exists?('/etc/ssl/certs')
8
+
9
+ super ssl: { ca_path: ca_path }, url: options[:upload_url_base] do |frd|
10
+ frd.request :multipart
11
+ frd.request :url_encoded
12
+ frd.adapter :net_http
13
+ frd.headers['User-Agent'] = Uploadcare::user_agent
14
+
15
+ frd.response :raise_error
16
+ frd.response :parse_json
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Uploadcare
5
+ module Connections
6
+ module Response
7
+ class ParseJson < Faraday::Response::Middleware
8
+ WHITESPACE_REGEX = /\A^\s*$\z/
9
+
10
+ def parse(body)
11
+ case body
12
+ when WHITESPACE_REGEX, nil
13
+ nil
14
+ else
15
+ JSON.parse(body)
16
+ end
17
+ end
18
+
19
+ def on_complete(response)
20
+ response[:body] = parse(response[:body]) if respond_to?(:parse) && !(response[:status] > 200) #!unparsable_status_codes.include?(response.status)
21
+ end
22
+
23
+ def unparsable_status_codes
24
+ [204, 301, 302, 304]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ Faraday::Response.register_middleware :parse_json => Uploadcare::Connections::Response::ParseJson
@@ -0,0 +1,21 @@
1
+ require 'faraday'
2
+
3
+ module Uploadcare
4
+ module Connections
5
+ module Response
6
+ class RaiseError < Faraday::Response::Middleware
7
+ def on_complete(response)
8
+ @error_codes = Uploadcare::Error.errors.keys
9
+ @status = response[:status]
10
+
11
+ if @error_codes.include?(@status)
12
+ error = Uploadcare::Error.errors[@status].new
13
+ fail(error)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Faraday::Response.register_middleware :raise_error => Uploadcare::Connections::Response::RaiseError
@@ -9,6 +9,35 @@ module Uploadcare
9
9
  (?:\/-\/(?<operations>.*?))?\/?$ # optional operations
10
10
  /ix
11
11
 
12
+ def self.parse_file_string string
13
+ result = Uploadcare::Parser.parse(string)
14
+
15
+ unless result.is_a?(Uploadcare::Parser::File)
16
+ msg = "invalid CDN URL or UUID was given for file: #{uuid_or_cdn_url}."
17
+ if result.is_a?(Uploadcare::Parser::Group)
18
+ msg = msg + "\n Group UUID was given. Try call @api.group if it is what you intended."
19
+ end
20
+ raise msg
21
+ end
22
+
23
+ result
24
+ end
25
+
26
+
27
+ def self.parse_group_string string
28
+ result = Uploadcare::Parser.parse(string)
29
+
30
+ unless result.is_a?(Uploadcare::Parser::Group)
31
+ msg = "invalid CDN URL or UUID was given for group: #{uuid_or_cdn_url}."
32
+ if result.is_a?(Uploadcare::Parser::File)
33
+ msg = msg + "\n File UUID was given. Try call @api.file if it is what you intended."
34
+ end
35
+ raise msg
36
+ end
37
+
38
+ result
39
+ end
40
+
12
41
  def self.parse string
13
42
  matched = META_URL.match(string)
14
43
 
@@ -1,3 +1,3 @@
1
1
  module Uploadcare
2
- VERSION = "1.0.1.rc2"
2
+ VERSION = "1.0.2"
3
3
  end
data/spec/parser_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Uploadcare::Api::File do
4
- before :each do
3
+ describe Uploadcare::Parser do
4
+ before :all do
5
5
  # http://www.ucarecdn.com/be4e24fb-2cad-476f-9417-ba95e3fefbf2~3/-/crop/123/-/fromat/png/
6
6
  @uuid = "be4e24fb-2cad-476f-9417-ba95e3fefbf2"
7
7
  @count = "12"
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'uri'
3
+ require 'socket'
4
+
5
+ describe Uploadcare::Connections::ApiConnection do
6
+ before(:all) do
7
+ @settings = Uploadcare.default_settings
8
+ end
9
+
10
+ it 'should initialize api connection' do
11
+ expect {Uploadcare::Connections::ApiConnection.new(@settings)}.to_not raise_error
12
+ end
13
+
14
+ it 'should use ParseJson and RaiseError middlewares' do
15
+ connection = Uploadcare::Connections::ApiConnection.new(@settings)
16
+ connection.builder.handlers.include?(Uploadcare::Connections::Response::ParseJson).should == true
17
+ connection.builder.handlers.include?(Uploadcare::Connections::Response::RaiseError).should == true
18
+ end
19
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+ require 'uri'
3
+ require 'socket'
4
+
5
+ describe Uploadcare::Error do
6
+ before(:all) do
7
+ @settings = Uploadcare.default_settings
8
+ @codes = [400,401,403,404,406,408,422,429,500,502,503,504]
9
+ @connection = Uploadcare::Connections::ApiConnection.new(@settings)
10
+ end
11
+
12
+ it 'Error codes should be accesbile' do
13
+ Uploadcare::Error.errors.keys.should == @codes
14
+ end
15
+
16
+ it 'Errors should be kind of requested codes' do
17
+ not_found = Uploadcare::Error.errors[404]
18
+ not_found.new('File not found').should be_kind_of(Uploadcare::Error::RequestError::NotFound)
19
+ end
20
+
21
+ it 'errors should have meaningfull messages' do
22
+ not_found = Uploadcare::Error.errors[404]
23
+ error = not_found.new
24
+ error.message.should == "HTTP 404 - the requested resource could not be found."
25
+ end
26
+
27
+ it 'Should raise an error' do
28
+ error = Uploadcare::Error::RequestError::NotFound
29
+ expect{ @connection.send :get, '/random_url/', {} }.to raise_error(error)
30
+ end
31
+
32
+ it "should escape particular error" do
33
+ error = Uploadcare::Error::RequestError::NotFound
34
+ expect do
35
+ begin
36
+ @connection.send :get, '/random_url/', {}
37
+ rescue error => e
38
+ nil
39
+ end
40
+ end.to_not raise_error
41
+ end
42
+
43
+ it 'should escape common request error' do
44
+ error = Uploadcare::Error::RequestError
45
+ expect do
46
+ begin
47
+ @connection.send :get, '/random_url/', {}
48
+ rescue error => e
49
+ nil
50
+ end
51
+ end.to_not raise_error
52
+ end
53
+
54
+ it 'should escape generic uploadcare service error' do
55
+ error = Uploadcare::Error
56
+ expect do
57
+ begin
58
+ @connection.send :get, '/random_url/', {}
59
+ rescue error => e
60
+ nil
61
+ end
62
+ end.to_not raise_error
63
+ end
64
+
65
+ it 'should escape generic uploadcare service error' do
66
+ error = StandardError
67
+ expect do
68
+ begin
69
+ @connection.send :get, '/random_url/', {}
70
+ rescue error => e
71
+ nil
72
+ end
73
+ end.to_not raise_error
74
+ end
75
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'uri'
3
+ require 'socket'
4
+
5
+ describe Uploadcare::Connections::UploadConnection do
6
+ before(:all) do
7
+ @settings = Uploadcare.default_settings
8
+ end
9
+
10
+ it 'should initialize upload connection' do
11
+ expect {Uploadcare::Connections::UploadConnection.new(@settings)}.to_not raise_error
12
+ end
13
+
14
+ it 'should use ParseJson and RaiseError middleware' do
15
+ connection = Uploadcare::Connections::UploadConnection.new(@settings)
16
+ connection.builder.handlers.include?(Uploadcare::Connections::Response::ParseJson).should == true
17
+ connection.builder.handlers.include?(Uploadcare::Connections::Response::RaiseError).should == true
18
+ end
19
+ end
@@ -25,8 +25,8 @@ Gem::Specification.new do |gem|
25
25
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
26
26
  gem.require_paths = ["lib"]
27
27
  gem.version = Uploadcare::VERSION
28
- gem.add_runtime_dependency 'faraday'
29
- gem.add_runtime_dependency 'faraday_middleware'
28
+ gem.add_runtime_dependency 'faraday', '~> 0.8'
29
+ gem.add_runtime_dependency 'faraday_middleware', '~> 0.9'
30
30
  gem.add_runtime_dependency 'multipart-post'
31
31
  gem.add_runtime_dependency 'mime-types'
32
32
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uploadcare-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1.rc2
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - '@rastyagaev (Vadim Rastyagaev)'
@@ -10,36 +10,36 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-03-08 00:00:00.000000000 Z
13
+ date: 2014-04-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: '0.8'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - '>='
26
+ - - ~>
27
27
  - !ruby/object:Gem::Version
28
- version: '0'
28
+ version: '0.8'
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: faraday_middleware
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - '>='
33
+ - - ~>
34
34
  - !ruby/object:Gem::Version
35
- version: '0'
35
+ version: '0.9'
36
36
  type: :runtime
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - '>='
40
+ - - ~>
41
41
  - !ruby/object:Gem::Version
42
- version: '0'
42
+ version: '0.9'
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: multipart-post
45
45
  requirement: !ruby/object:Gem::Requirement
@@ -113,20 +113,24 @@ files:
113
113
  - Rakefile
114
114
  - lib/uploadcare.rb
115
115
  - lib/uploadcare/api.rb
116
- - lib/uploadcare/api/connections.rb
117
116
  - lib/uploadcare/api/file_api.rb
118
117
  - lib/uploadcare/api/file_list_api.rb
119
118
  - lib/uploadcare/api/group_api.rb
120
119
  - lib/uploadcare/api/group_list_api.rb
121
- - lib/uploadcare/api/parser.rb
122
120
  - lib/uploadcare/api/project_api.rb
123
121
  - lib/uploadcare/api/raw_api.rb
124
122
  - lib/uploadcare/api/uploading_api.rb
123
+ - lib/uploadcare/errors/errors.rb
125
124
  - lib/uploadcare/resources/file.rb
126
125
  - lib/uploadcare/resources/file_list.rb
127
126
  - lib/uploadcare/resources/group.rb
128
127
  - lib/uploadcare/resources/group_list.rb
129
128
  - lib/uploadcare/resources/project.rb
129
+ - lib/uploadcare/rest/connections/api_connection.rb
130
+ - lib/uploadcare/rest/connections/upload_connection.rb
131
+ - lib/uploadcare/rest/middlewares/parse_json_middleware.rb
132
+ - lib/uploadcare/rest/middlewares/raise_error_middleware.rb
133
+ - lib/uploadcare/utils/parser.rb
130
134
  - lib/uploadcare/version.rb
131
135
  - spec/file_list_spec.rb
132
136
  - spec/file_spec.rb
@@ -136,6 +140,9 @@ files:
136
140
  - spec/parser_spec.rb
137
141
  - spec/project_spec.rb
138
142
  - spec/raw_api_spec.rb
143
+ - spec/rest/api_connection_spec.rb
144
+ - spec/rest/errors_spec.rb
145
+ - spec/rest/upload_connection_spec.rb
139
146
  - spec/spec_helper.rb
140
147
  - spec/uploading_multiple_spec.rb
141
148
  - spec/uploading_spec.rb
@@ -159,9 +166,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
166
  version: '0'
160
167
  required_rubygems_version: !ruby/object:Gem::Requirement
161
168
  requirements:
162
- - - '>'
169
+ - - '>='
163
170
  - !ruby/object:Gem::Version
164
- version: 1.3.1
171
+ version: '0'
165
172
  requirements: []
166
173
  rubyforge_project:
167
174
  rubygems_version: 2.0.6
@@ -177,6 +184,9 @@ test_files:
177
184
  - spec/parser_spec.rb
178
185
  - spec/project_spec.rb
179
186
  - spec/raw_api_spec.rb
187
+ - spec/rest/api_connection_spec.rb
188
+ - spec/rest/errors_spec.rb
189
+ - spec/rest/upload_connection_spec.rb
180
190
  - spec/spec_helper.rb
181
191
  - spec/uploading_multiple_spec.rb
182
192
  - spec/uploading_spec.rb
@@ -1,32 +0,0 @@
1
- require 'faraday'
2
- require 'faraday_middleware'
3
-
4
- module Uploadcare
5
- class Connections
6
- def self.api_connection options
7
- connection = Faraday.new url: options[:api_url_base] do |frd|
8
- frd.request :url_encoded
9
- frd.use FaradayMiddleware::FollowRedirects, limit: 3
10
- frd.adapter :net_http # actually, default adapter, just to be clear
11
- frd.headers['Authorization'] = "Uploadcare.Simple #{options[:public_key]}:#{options[:private_key]}"
12
- frd.headers['Accept'] = "application/vnd.uploadcare-v#{options[:api_version]}+json"
13
- frd.headers['User-Agent'] = Uploadcare::user_agent
14
- end
15
-
16
- connection
17
- end
18
-
19
- def self.upload_connection options
20
- ca_path = '/etc/ssl/certs' if File.exists?('/etc/ssl/certs')
21
-
22
- connection = Faraday.new ssl: { ca_path: ca_path }, url: options[:upload_url_base] do |frd|
23
- frd.request :multipart
24
- frd.request :url_encoded
25
- frd.adapter Faraday.default_adapter
26
- frd.headers['User-Agent'] = Uploadcare::user_agent
27
- end
28
-
29
- connection
30
- end
31
- end
32
- end