croudia 1.0.9 → 1.0.10

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: 35e3879bf61ffee0163b39e54634e1a8d9991813
4
- data.tar.gz: cf39c427d33d8f07d54a50b1cf9622499f08ce40
3
+ metadata.gz: 68c388cfc2c0d3d69defbc84ffb59e370c2977c3
4
+ data.tar.gz: 1497af451bd3a78bc8642492bf1a6f090700698f
5
5
  SHA512:
6
- metadata.gz: c88dc315377489afa9fafa63cf8041c5df7e1559b254bfa7e68e6e34d30ac6dd6caa438353abd3a3786afe235855f87549132188cdd68a7d735cc5f97ad80c94
7
- data.tar.gz: 51e113458a23c95d517f85b67dd5cce947713ec1db00acf5300e323a063e2fb4756b2c7bb6894e6c6b29aab0ca6844b30263c38d75275b3568d0898995b48d8b
6
+ metadata.gz: e9863275f9486fc8de0e50aab75482aa4859ad2c2fd8946022b29782cd25e709e32d7419719e9ed5f6341bab570a300702704279c756d999a33bda0cabdfc90c
7
+ data.tar.gz: 885898faeab2a439c97b864be90aa7f1ccec545b9357bc67e85725d120cabc305290f2d65bcf5229075788297dfa322c353f420f3a85a37cb6d95aa771018406
@@ -2,6 +2,9 @@ require 'faraday'
2
2
  require 'faraday_middleware'
3
3
  require 'croudia/configurable'
4
4
  require 'croudia/request/multipart_with_file'
5
+ require 'croudia/request/raise_error'
6
+ require 'croudia/response/parse_json'
7
+ require 'croudia/response/raise_error'
5
8
  require 'croudia/version'
6
9
 
7
10
  module Croudia
@@ -24,9 +27,10 @@ module Croudia
24
27
  builder.request :multipart_with_file
25
28
  builder.request :multipart
26
29
  builder.request :url_encoded
30
+ builder.request :raise_error
27
31
 
28
- builder.response :json
29
32
  builder.response :raise_error
33
+ builder.response :parse_json
30
34
 
31
35
  builder.adapter Faraday.default_adapter
32
36
  end unless defined? Croudia::Default::MIDDLEWARE
@@ -0,0 +1,17 @@
1
+ require 'croudia/base'
2
+ require 'croudia/entity/media'
3
+
4
+ module Croudia
5
+ class Entities < Croudia::Base
6
+ KEYS = [
7
+ :media,
8
+ ]
9
+ attr_reader(*KEYS)
10
+
11
+ def initialize(attrs)
12
+ media = attrs.delete('media')
13
+ super(attrs)
14
+ @attrs['media'] = Croudia::Entity::Media.new(media) if media
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require 'croudia/base'
2
+
3
+ module Croudia
4
+ module Entity
5
+ class Media < Croudia::Base
6
+ KEYS = [
7
+ :media_url_https,
8
+ :type,
9
+ ]
10
+ attr_reader(*KEYS)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,48 @@
1
+ module Croudia
2
+ class Error < StandardError
3
+
4
+ # Error connecting to server
5
+ class ConnectionFailed < Error
6
+ attr_reader :wrapped_exception
7
+
8
+ def initialize(e)
9
+ @wrapped_exception = e
10
+ super(e.respond_to?(:message) ? e.message : e.to_s)
11
+ end
12
+
13
+ def backtrace
14
+ if @wrapped_exception.respond_to?(:backtrace)
15
+ @wrapped_exception.backtrace
16
+ else
17
+ super
18
+ end
19
+ end
20
+ end
21
+
22
+ class Timeout < ConnectionFailed; end
23
+
24
+ # Error connected to server
25
+ class ConnectionError < Error
26
+ attr_reader :code, :env, :error
27
+
28
+ def initialize(env)
29
+ @code = env[:status]
30
+ @error = env[:body]['error'] rescue nil
31
+ @env = env
32
+ super(@error)
33
+ end
34
+ end
35
+
36
+ # 4xx: Client Errors
37
+ class ClientError < ConnectionError; end
38
+ class BadRequest < ClientError; end
39
+ class Unauthorized < ClientError; end
40
+ class Forbidden < ClientError; end
41
+ class NotFound < ClientError; end
42
+
43
+ # 5xx: Server Errors
44
+ class ServerError < ConnectionError; end
45
+ class BadGateway < ServerError; end
46
+ class Unavailable < ServerError; end
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ require 'croudia/error'
2
+ require 'faraday'
3
+
4
+ module Croudia
5
+ module Request
6
+ class RaiseError < Faraday::Middleware
7
+ def call(env)
8
+ @app.call(env)
9
+ rescue Faraday::Error::TimeoutError => e
10
+ raise Croudia::Error::Timeout.new(e)
11
+ rescue Faraday::Error::ConnectionFailed => e
12
+ raise Croudia::Error::ConnectionFailed.new(e)
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ Faraday.register_middleware :request,
19
+ raise_error: lambda { Croudia::Request::RaiseError }
@@ -0,0 +1,23 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Croudia
5
+ module Response
6
+ class ParseJSON < Faraday::Response::Middleware
7
+ def on_complete(env)
8
+ case env[:body]
9
+ when /\A\s*\z/, nil
10
+ env[:body] = nil
11
+ else
12
+ env[:body] = JSON.parse(env[:body])
13
+ end
14
+ rescue JSON::ParseError => e
15
+ raise e if env[:status] == 200
16
+ env[:body] = nil
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ Faraday.register_middleware :response,
23
+ parse_json: Croudia::Response::ParseJSON
@@ -0,0 +1,36 @@
1
+ require 'croudia/error'
2
+ require 'faraday'
3
+
4
+ module Croudia
5
+ module Response
6
+ class RaiseError < Faraday::Response::Middleware
7
+ def on_complete(env)
8
+ error_class = case env[:status]
9
+ when 400
10
+ Croudia::Error::BadRequest
11
+ when 401
12
+ Croudia::Error::Unauthorized
13
+ when 403
14
+ Croudia::Error::Forbidden
15
+ when 404
16
+ Croudia::Error::NotFound
17
+ when 400 .. 499
18
+ Croudia::Error::ClientError
19
+ when 502
20
+ Croudia::Error::BadGateway
21
+ when 503
22
+ Croudia::Error::Unavailable
23
+ when 500 .. 599
24
+ Croudia::Error::ServerError
25
+ else
26
+ return
27
+ end
28
+
29
+ raise error_class.new(env)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ Faraday.register_middleware :response,
36
+ raise_error: lambda { Croudia::Response::RaiseError }
@@ -1,4 +1,5 @@
1
1
  require 'croudia/creatable'
2
+ require 'croudia/entities'
2
3
  require 'croudia/identity'
3
4
  require 'croudia/source'
4
5
  require 'croudia/user'
@@ -9,6 +10,7 @@ module Croudia
9
10
 
10
11
  KEYS = [
11
12
  :id_str,
13
+ :entities,
12
14
  :favorited,
13
15
  :favorited_count,
14
16
  :in_reply_to_screen_name,
@@ -28,11 +30,13 @@ module Croudia
28
30
  attr_reader(*KEYS)
29
31
 
30
32
  def initialize(attrs={})
33
+ entities = attrs.delete('entities')
31
34
  user = attrs.delete('user')
32
35
  reply_status = attrs.delete('reply_status')
33
36
  spread_status = attrs.delete('spread_status')
34
37
  source = attrs.delete('source')
35
38
  super(attrs)
39
+ @attrs['entities'] = Croudia::Entities.new(entities) if entities
36
40
  @attrs['user'] = Croudia::User.new(user) if user
37
41
  @attrs['reply_status'] = Croudia::Status.new(reply_status) if reply_status
38
42
  @attrs['spread_status'] = Croudia::Status.new(spread_status) if spread_status
@@ -1,3 +1,3 @@
1
1
  module Croudia
2
- VERSION = '1.0.9'
2
+ VERSION = '1.0.10'
3
3
  end
@@ -0,0 +1,46 @@
1
+ require 'faraday'
2
+ require 'helper'
3
+
4
+ describe Croudia::Error do
5
+ before do
6
+ @client = Croudia::Client.new
7
+ end
8
+
9
+ context 'connection error' do
10
+ before do
11
+ stub_get('/account/verify_credentials.json').to_raise Faraday::Error::ConnectionFailed
12
+ end
13
+
14
+ it 'raises Croudia::Error::ConnectionFailed' do
15
+ expect { @client.verify_credentials }.to raise_error Croudia::Error::ConnectionFailed
16
+ end
17
+ end
18
+
19
+ context 'client error' do
20
+ before do
21
+ stub_get('/account/verify_credentials.json').to_return(
22
+ status: 401,
23
+ body: fixture(:error),
24
+ headers: { content_type: 'application/json; charset=utf-8' }
25
+ )
26
+ end
27
+
28
+ it 'raises Croudia::Error::ClientError' do
29
+ expect { @client.verify_credentials }.to raise_error Croudia::Error::ClientError
30
+ end
31
+ end
32
+
33
+ context 'server error' do
34
+ before do
35
+ stub_get('/account/verify_credentials.json').to_return(
36
+ status: 502,
37
+ body: fixture(:error),
38
+ headers: { content_type: 'application/json; charset=utf-8' }
39
+ )
40
+ end
41
+
42
+ it 'raises Croudia::Error::ServerError' do
43
+ expect { @client.verify_credentials }.to raise_error Croudia::Error::ServerError
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ {
2
+ "error": "Hello, I'm an error! How are you doing today?"
3
+ }
@@ -1 +1 @@
1
- {"id":1228014,"id_str":"1228014","text":"さっき誤字してた……","created_at":"Tue, 25 Jun 2013 23:58:43 +0900","favorited":false,"favorited_count":0,"spread":false,"spread_count":0,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"source":{"name":"source","url":"https://croudia.com"},"user":{"id":7987,"id_str":"7987","name":"wktk","screen_name":"wktk","profile_image_url_https":"https://croudia.com/testimages/download/9423","created_at":"Sun, 29 Jul 2012 19:21:21 +0900","description":"気軽に fav ります。Twitter でも @wktk です。 \r\nhttps://twitter.com/wktk\r\nhttps://github.com/wktk","favorites_count":68,"follow_request_sent":"underdevelopment","followers_count":12,"following":"underdevelopment","friends_count":11,"location":"Japan","statuses_count":26,"protected":false,"url":"http://wktk.jp/"}}
1
+ {"id":1228014,"id_str":"1228014","text":"さっき誤字してた……","created_at":"Tue, 25 Jun 2013 23:58:43 +0900","favorited":false,"favorited_count":0,"spread":false,"spread_count":0,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"source":{"name":"source","url":"https://croudia.com"},"user":{"id":7987,"id_str":"7987","name":"wktk","screen_name":"wktk","profile_image_url_https":"https://croudia.com/testimages/download/9423","created_at":"Sun, 29 Jul 2012 19:21:21 +0900","description":"気軽に fav ります。Twitter でも @wktk です。 \r\nhttps://twitter.com/wktk\r\nhttps://github.com/wktk","favorites_count":68,"follow_request_sent":"underdevelopment","followers_count":12,"following":"underdevelopment","friends_count":11,"location":"Japan","statuses_count":26,"protected":false,"url":"http://wktk.jp/"},"entities":{"media":{"media_url_https":"https://croudia.com/testimages/download/44983","type":"photo"}}}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: croudia
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.9
4
+ version: 1.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - wktk
@@ -124,10 +124,16 @@ files:
124
124
  - lib/croudia/configurable.rb
125
125
  - lib/croudia/creatable.rb
126
126
  - lib/croudia/default.rb
127
+ - lib/croudia/entities.rb
128
+ - lib/croudia/entity/media.rb
129
+ - lib/croudia/error.rb
127
130
  - lib/croudia/ext/openssl.rb
128
131
  - lib/croudia/identity.rb
129
132
  - lib/croudia/relationship.rb
130
133
  - lib/croudia/request/multipart_with_file.rb
134
+ - lib/croudia/request/raise_error.rb
135
+ - lib/croudia/response/parse_json.rb
136
+ - lib/croudia/response/raise_error.rb
131
137
  - lib/croudia/secret_mail.rb
132
138
  - lib/croudia/source.rb
133
139
  - lib/croudia/status.rb
@@ -143,6 +149,7 @@ files:
143
149
  - spec/croudia/api/users_spec.rb
144
150
  - spec/croudia/base_spec.rb
145
151
  - spec/croudia/client_spec.rb
152
+ - spec/croudia/error_spec.rb
146
153
  - spec/croudia/identity_spec.rb
147
154
  - spec/croudia/secret_mail_spec.rb
148
155
  - spec/croudia/source_spec.rb
@@ -150,6 +157,7 @@ files:
150
157
  - spec/croudia/user_spec.rb
151
158
  - spec/croudia_spec.rb
152
159
  - spec/fixtures/access_token.json
160
+ - spec/fixtures/error.json
153
161
  - spec/fixtures/friendship.json
154
162
  - spec/fixtures/friendships.json
155
163
  - spec/fixtures/image.jpg
@@ -195,6 +203,7 @@ test_files:
195
203
  - spec/croudia/api/users_spec.rb
196
204
  - spec/croudia/base_spec.rb
197
205
  - spec/croudia/client_spec.rb
206
+ - spec/croudia/error_spec.rb
198
207
  - spec/croudia/identity_spec.rb
199
208
  - spec/croudia/secret_mail_spec.rb
200
209
  - spec/croudia/source_spec.rb
@@ -202,6 +211,7 @@ test_files:
202
211
  - spec/croudia/user_spec.rb
203
212
  - spec/croudia_spec.rb
204
213
  - spec/fixtures/access_token.json
214
+ - spec/fixtures/error.json
205
215
  - spec/fixtures/friendship.json
206
216
  - spec/fixtures/friendships.json
207
217
  - spec/fixtures/image.jpg