imgurapi 2.0.1 → 2.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 +4 -4
- data/lib/imgur/communication.rb +19 -19
- data/lib/imgur/session.rb +5 -0
- data/lib/imgur/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c69afc57eef5284296e36c8b5836e69011b015fc
|
4
|
+
data.tar.gz: 7fb7e32ee6c569746ef56d29f80dadaead0ea1e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6977d3bb8ef0296b2ff178507b6f669fb0914dd47948bc6c95d9a7a590622fa4ec7ab297d4c8b2eca0604f73255b86c38e232e0d945560028a8ead62870443b
|
7
|
+
data.tar.gz: caed10efce8328a7ba2109ead917bb18af4045b503cbcaf45f06dcf68559e5130d12708039fe5c7604511c5deb8c6c926f68ee16485da15e76f817f1dda0508b
|
data/lib/imgur/communication.rb
CHANGED
@@ -2,6 +2,7 @@ module Imgur
|
|
2
2
|
Communication = Struct.new(:session) do
|
3
3
|
|
4
4
|
API_VERSION = 3
|
5
|
+
MAX_ATTEMPTS = 3
|
5
6
|
|
6
7
|
# RESTful network call
|
7
8
|
def call(method, endpoint, params = nil)
|
@@ -13,27 +14,26 @@ module Imgur
|
|
13
14
|
private
|
14
15
|
|
15
16
|
# Processes RESTful response according the status code
|
16
|
-
def request(&block)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
case response.status
|
21
|
-
when 200, 404
|
22
|
-
return parse_message(response.body)['data']
|
23
|
-
when 401, 500
|
24
|
-
error_message = parse_message response.body
|
25
|
-
raise "Unauthorized: #{error_message['error']['message']}"
|
26
|
-
when 403
|
27
|
-
get_new_and_reset_token
|
28
|
-
|
29
|
-
request &block # and retry the request
|
30
|
-
else
|
31
|
-
puts response.body
|
32
|
-
raise "Response code #{response.status} not recognized"
|
33
|
-
end
|
17
|
+
def request(attempt = 0, &block)
|
18
|
+
if attempt == MAX_ATTEMPTS
|
19
|
+
raise "Retried #{MAX_ATTEMPTS} times but could not get an access_token"
|
34
20
|
end
|
35
21
|
|
36
|
-
|
22
|
+
response = yield
|
23
|
+
|
24
|
+
case response.status
|
25
|
+
when 200, 404
|
26
|
+
return parse_message(response.body)['data']
|
27
|
+
when 401, 500
|
28
|
+
error_message = parse_message response.body
|
29
|
+
raise "Unauthorized: #{error_message['error']['message']}"
|
30
|
+
when 403
|
31
|
+
get_new_and_reset_token
|
32
|
+
|
33
|
+
request attempt + 1, &block # and retry the request once more
|
34
|
+
else
|
35
|
+
raise "Response code #{response.status} not recognized"
|
36
|
+
end
|
37
37
|
end
|
38
38
|
|
39
39
|
def get_new_and_reset_token
|
data/lib/imgur/session.rb
CHANGED
@@ -14,6 +14,11 @@ module Imgur
|
|
14
14
|
@refresh_token = options[:refresh_token]
|
15
15
|
end
|
16
16
|
|
17
|
+
# Singleton initializer to allow access_token reuse
|
18
|
+
def self.instance(options)
|
19
|
+
@@session ||= new(options)
|
20
|
+
end
|
21
|
+
|
17
22
|
%w(Account Image).each do |clazz|
|
18
23
|
define_method clazz.downcase do
|
19
24
|
Imgur::Api.const_get(clazz).new(self)
|
data/lib/imgur/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imgurapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Cruz Horts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|