deviantart 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 129907466eb6bebbef69c392e66f9fd291c26d29
4
- data.tar.gz: c99998038239822dd3a91a27c9162855d87459c0
3
+ metadata.gz: '0499ac49ed032c1497d0d60dfbd659f71aef9d2b'
4
+ data.tar.gz: 5dc456abe5fbab5a9dc470b9f44de697b75822da
5
5
  SHA512:
6
- metadata.gz: 471b20bcf89d8d7141ed57765b1886f2bce452639b7da88d654e1572b04b4b1e0867192c0cb660c73eaf29f532233afdef5428efa38a3cca37a259868c31615f
7
- data.tar.gz: f598852995d20b8ada5eb2fcb32ec33277524f7467db9e28858e7865ca286e134152bce1c7f583321e06a430b5b76afe374e08d0141e0caa4e24aaee68396dd8
6
+ metadata.gz: 2eb9a19cafdec3e4ca2f72d11c90d81021b86e1175277a15535bdf15a704e6b75eab918d63757229f444833201a74b4e29bea549e33118e99c475f1fc5b332b3
7
+ data.tar.gz: 85f33e7b9d7a68b1a7fe88b0fa05e44321634f9ce0a70f93b928080183cfdafcc522a5f94164ca9aa37653047f50c00f118c28136ecfa8b3d3ec3fcf34f40a78
data/Gemfile CHANGED
@@ -8,6 +8,7 @@ group :development do
8
8
  gem 'omniauth'
9
9
  gem 'omniauth-oauth2', '~> 1.3.1'
10
10
  gem 'omniauth-deviantart'
11
+ gem 'launchy'
11
12
  end
12
13
 
13
14
  # Specify your gem's dependencies in deviantart.gemspec
data/README.md CHANGED
@@ -7,14 +7,13 @@
7
7
  ## Usage
8
8
 
9
9
  ```ruby
10
- da = DeviantArt.new do |config|
10
+ da = DeviantArt::Client.new do |config|
11
11
  config.client_id = 9999
12
12
  config.client_secret = 'LMNOPQRSTUVWXYZZZZZZZZ9999999999'
13
13
  # auto refresh access_token with Client Credentials Grant when expired
14
14
  config.grant_type = :client_credentials
15
15
  config.access_token_auto_refresh = true
16
16
  end
17
- # da is DeviantArt::Client object
18
17
 
19
18
  deviation = da.get_deviation('F98C2XXX-C6A8-XXXX-08F9-57CCXXXXX187')
20
19
  deviation.title # => deviation's title
@@ -92,4 +91,3 @@ The gem is available as open source under the terms of the [MIT License](http://
92
91
  ## Badges
93
92
 
94
93
  [![Build Status](https://travis-ci.org/aycabta/deviantart.svg)](https://travis-ci.org/aycabta/deviantart)
95
-
data/Rakefile CHANGED
@@ -2,8 +2,8 @@ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
  require 'readline'
4
4
  require 'net/http'
5
+ require 'launchy'
5
6
 
6
- BROWSER_COMMAND_FILE = 'test/browser_command'
7
7
  AUTHORIZATION_CODE_FILE = 'test/fixtures/authorization_code.json'
8
8
  OUTPUT_PIPE = 'test/output_pipe'
9
9
 
@@ -15,18 +15,6 @@ Rake::TestTask.new(:test) do |t|
15
15
  t.test_files = test_pettern
16
16
  end
17
17
 
18
- def get_browser_command
19
- open(BROWSER_COMMAND_FILE, 'r').read
20
- end
21
-
22
- file BROWSER_COMMAND_FILE do
23
- browser_command = Readline.readline('Input your browser command> ')
24
- open(BROWSER_COMMAND_FILE, 'w') do |f|
25
- f.write(browser_command)
26
- end
27
- puts "Wrote \"#{browser_command}\" to #{BROWSER_COMMAND_FILE}"
28
- end
29
-
30
18
  def wait_oauth_consumer_booting
31
19
  http = Net::HTTP.new('localhost', 4567)
32
20
  http.open_timeout = 1
@@ -45,7 +33,7 @@ def wait_oauth_consumer_booting
45
33
  end
46
34
  end
47
35
 
48
- file AUTHORIZATION_CODE_FILE => BROWSER_COMMAND_FILE do
36
+ file AUTHORIZATION_CODE_FILE do
49
37
  if File.exists?(OUTPUT_PIPE)
50
38
  File.unlink(OUTPUT_PIPE)
51
39
  end
@@ -70,7 +58,7 @@ file AUTHORIZATION_CODE_FILE => BROWSER_COMMAND_FILE do
70
58
  cv.wait(mutex)
71
59
  end
72
60
  puts 'Open browser for authorization'
73
- system("#{get_browser_command} http://localhost:4567/auth/deviantart &")
61
+ Launchy.open('http://localhost:4567/auth/deviantart')
74
62
  is_browsed = true
75
63
  cv.signal
76
64
  }
@@ -7,6 +7,7 @@ require 'deviantart/status'
7
7
 
8
8
  module DeviantArt
9
9
  # Bypass args and block to DeviantArt::Client
10
+ # ...for backward compatibility
10
11
  def self.new(*args, &block)
11
12
  DeviantArt::Client.new(*args, &block)
12
13
  end
@@ -0,0 +1,7 @@
1
+ require 'deviantart/base'
2
+
3
+ module DeviantArt
4
+ class AuthorizationCode
5
+ end
6
+ end
7
+
@@ -0,0 +1,7 @@
1
+ require 'deviantart/base'
2
+
3
+ module DeviantArt
4
+ class AuthorizationCode::AccessToken < Base
5
+ attr_accessor :expires_in, :status, :access_token, :token_type, :refresh_token, :scope
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'deviantart/base'
2
+
3
+ module DeviantArt
4
+ class AuthorizationCode::RefreshToken < Base
5
+ attr_accessor :expires_in, :status, :access_token, :token_type, :refresh_token, :scope
6
+ end
7
+ end
@@ -6,6 +6,11 @@ require 'deviantart/client/data'
6
6
  require 'deviantart/client/feed'
7
7
  # TODO: comments, cured, messages, notes, stash, util
8
8
  require 'deviantart/error'
9
+ require 'deviantart/authorization_code'
10
+ require 'deviantart/authorization_code/access_token'
11
+ require 'deviantart/authorization_code/refresh_token'
12
+ require 'deviantart/client_credentials'
13
+ require 'deviantart/client_credentials/access_token'
9
14
  require 'net/http'
10
15
  require 'uri'
11
16
  require 'json'
@@ -52,8 +57,8 @@ module DeviantArt
52
57
  def initialize(options = {})
53
58
  @access_token = nil
54
59
  @host = @@default_host
55
- @on_refresh_access_token = nil
56
- @on_refresh_authorization_code = nil
60
+ @on_refresh_access_token = []
61
+ @on_refresh_authorization_code = []
57
62
  @access_token_auto_refresh = true
58
63
  @grant_type = nil
59
64
  @headers = {}
@@ -90,71 +95,80 @@ module DeviantArt
90
95
  refresh_access_token
91
96
  response = request(method, path, params)
92
97
  end
93
- case response.code
94
- when '200'
98
+ status_code = response.code.to_i
99
+ case status_code
100
+ when 200
95
101
  klass.new(response.json)
96
- when '400'
102
+ when 400
97
103
  # Request failed due to client error,
98
104
  # e.g. validation failed or User not found
99
- DeviantArt::Error.new(response.json, response.code.to_i)
100
- when '401'
105
+ DeviantArt::Error.new(response.json, status_code)
106
+ when 401
101
107
  # Invalid token
102
- DeviantArt::Error.new(response.json, response.code.to_i)
103
- when '429'
108
+ DeviantArt::Error.new(response.json, status_code)
109
+ when 429
104
110
  # Rate limit reached or service overloaded
105
- DeviantArt::Error.new(response.json, response.code.to_i)
106
- when '500'
111
+ DeviantArt::Error.new(response.json, status_code)
112
+ when 500
107
113
  # Our servers encountered an internal error, try again
108
- DeviantArt::Error.new(response.json, response.code.to_i)
109
- when '503'
114
+ DeviantArt::Error.new(response.json, status_code)
115
+ when 503
110
116
  # Our servers are currently unavailable, try again later.
111
117
  # This is normally due to planned or emergency maintenance.
112
- DeviantArt::Error.new(response.json, response.code.to_i)
118
+ DeviantArt::Error.new(response.json, status_code)
113
119
  else
114
- DeviantArt::Error.new(response.json, response.code.to_i)
120
+ DeviantArt::Error.new(response.json, status_code)
115
121
  end
116
122
  end
117
123
 
118
124
  # Call given block when authorization code is refreshed
119
125
  def on_refresh_authorization_code(&block)
120
- @on_refresh_authorization_code = block
126
+ @on_refresh_authorization_code << block
121
127
  end
122
128
 
123
129
  # Call given block when access token is refreshed
124
130
  def on_refresh_access_token(&block)
125
- @on_refresh_access_token = block
131
+ @on_refresh_access_token << block
132
+ end
133
+
134
+ def refresh_access_token
135
+ case @grant_type.to_sym
136
+ when :authorization_code
137
+ refresh_authorization_code
138
+ when :client_credentials
139
+ refresh_client_credentials
140
+ end
126
141
  end
127
142
 
128
143
  private
129
144
 
130
145
  def request(method, path, params = {})
131
146
  uri = URI.parse("https://#{@host}#{path}")
147
+ if params.any?{ |key, value| value.is_a?(Enumerable) }
148
+ converted_params = []
149
+ params.each do |key, value|
150
+ if value.is_a?(Enumerable)
151
+ value.each_index do |i|
152
+ converted_params << ["#{key}[#{i}]", value[i]]
153
+ end
154
+ else
155
+ converted_params << [key, value]
156
+ end
157
+ end
158
+ params = converted_params
159
+ end
132
160
  case method
133
161
  when :get
134
162
  uri.query = URI.encode_www_form(params)
135
163
  request = Net::HTTP::Get.new(uri)
136
164
  when :post
137
165
  request = Net::HTTP::Post.new(uri.path)
138
- if params.any?{ |key, value| value.is_a?(Enumerable) }
139
- converted_params = []
140
- params.each do |key, value|
141
- if value.is_a?(Enumerable)
142
- value.each_index do |i|
143
- converted_params << ["#{key}[#{i}]", value[i]]
144
- end
145
- else
146
- converted_params << [key, value]
147
- end
148
- end
149
- request.body = URI.encode_www_form(converted_params)
150
- else
151
- request.set_form_data(params)
152
- end
166
+ request.body = URI.encode_www_form(params)
153
167
  end
154
168
  request['Content-Type'] = 'application/x-www-form-urlencoded'
155
169
  request['User-Agent'] = user_agent
156
170
  if not @access_token.nil?
157
- request["Authorization"] = "Bearer #{@access_token}"
171
+ request['Authorization'] = "Bearer #{@access_token}"
158
172
  end
159
173
  @headers.each_pair do |key, value|
160
174
  request[key] = value
@@ -174,11 +188,18 @@ module DeviantArt
174
188
  :post, '/oauth2/token',
175
189
  { grant_type: 'client_credentials', client_id: @client_id, client_secret: @client_secret }
176
190
  )
177
- if response.code == '200'
191
+ status_code = response.code.to_i
192
+ if status_code == 200
178
193
  @access_token = response.json['access_token']
179
- @on_refresh_access_token.call(@access_token) if @on_refresh_access_token
194
+ if !@on_refresh_access_token.empty?
195
+ @on_refresh_access_token.each do |p|
196
+ p.call(@access_token)
197
+ end
198
+ end
199
+ ClientCredentials::AccessToken.new(response.json)
180
200
  else
181
201
  @access_token = nil
202
+ DeviantArt::Error.new(response.json, status_code)
182
203
  end
183
204
  end
184
205
 
@@ -187,21 +208,23 @@ module DeviantArt
187
208
  :post, '/oauth2/token',
188
209
  { grant_type: 'refresh_token', client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token }
189
210
  )
190
- if response.code == '200'
211
+ status_code = response.code.to_i
212
+ if status_code == 200
191
213
  @access_token = response.json['access_token']
192
- @on_refresh_access_token.call(@access_token) if @on_refresh_access_token
193
- @on_refresh_authorization_code.call(@access_token, response.json['refresh_token']) if @on_refresh_authorization_code
214
+ if !@on_refresh_access_token.empty?
215
+ @on_refresh_access_token.each do |p|
216
+ p.call(@access_token)
217
+ end
218
+ end
219
+ if !@on_refresh_authorization_code.empty?
220
+ @on_refresh_authorization_code.each do |p|
221
+ p.call(@access_token, response.json['refresh_token'])
222
+ end
223
+ end
224
+ AuthorizationCode::RefreshToken.new(response.json)
194
225
  else
195
226
  @access_token = nil
196
- end
197
- end
198
-
199
- def refresh_access_token
200
- case @grant_type.to_sym
201
- when :authorization_code
202
- refresh_authorization_code
203
- when :client_credentials
204
- refresh_client_credentials
227
+ DeviantArt::Error.new(response.json, status_code)
205
228
  end
206
229
  end
207
230
  end
@@ -2,6 +2,8 @@ require 'deviantart/deviation'
2
2
  require 'deviantart/deviation/content'
3
3
  require 'deviantart/deviation/whofaved'
4
4
  require 'deviantart/deviation/download'
5
+ require 'deviantart/deviation/embeddedcontent'
6
+ require 'deviantart/deviation/metadata'
5
7
 
6
8
  module DeviantArt
7
9
  class Client
@@ -16,6 +18,27 @@ module DeviantArt
16
18
  perform(DeviantArt::Deviation::Content, :get, '/api/v1/oauth2/deviation/content', { deviationid: deviationid })
17
19
  end
18
20
 
21
+ # Fetch content embedded in a deviation.
22
+ # Journal and literature deviations support embedding of deviations inside them.
23
+ def get_deviation_embeddedcontent(deviationid, offset_deviationid: nil, offset: 0, limit: 10)
24
+ params = { deviationid: deviationid }
25
+ params['offset_deviationid'] = offset_deviationid if offset_deviationid
26
+ params['offset'] = offset if offset != 0
27
+ params['limit'] = limit if limit != 10
28
+ perform(DeviantArt::Deviation::EmbeddedContent, :get, '/api/v1/oauth2/deviation/embeddedcontent', params)
29
+ end
30
+
31
+ # Fetch deviation metadata for a set of deviations.
32
+ # This endpoint is limited to 50 deviations per query when fetching the base data and 10 when fetching extended data.
33
+ def get_deviation_metadata(deviationids, ext_submission: false, ext_camera: false, ext_stats: false, ext_collection: false)
34
+ params = { deviationids: deviationids.is_a?(Enumerable) ? deviationids : [deviationids] }
35
+ params['ext_submission'] = ext_submission
36
+ params['ext_camera'] = ext_camera
37
+ params['ext_stats'] = ext_stats
38
+ params['ext_collection'] = ext_collection
39
+ perform(DeviantArt::Deviation::Metadata, :get, '/api/v1/oauth2/deviation/metadata', params)
40
+ end
41
+
19
42
  # Fetch a list of users who faved the deviation
20
43
  def get_deviation_whofaved(deviationid, offset: 0, limit: 10)
21
44
  params = {}
@@ -29,8 +52,6 @@ module DeviantArt
29
52
  def download_deviation(deviationid)
30
53
  perform(DeviantArt::Deviation::Download, :get, "/api/v1/oauth2/deviation/download/#{deviationid}")
31
54
  end
32
-
33
- # TODO: embeddedcontent, metadata
34
55
  end
35
56
  end
36
57
  end
@@ -0,0 +1,6 @@
1
+ require 'deviantart/base'
2
+
3
+ module DeviantArt
4
+ class ClientCredentials < Base
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ require 'deviantart/base'
2
+
3
+ module DeviantArt
4
+ class ClientCredentials::AccessToken < Base
5
+ attr_accessor :expires_in, :status, :access_token, :token_type
6
+ end
7
+ end
8
+
@@ -2,5 +2,6 @@ require 'deviantart/base'
2
2
 
3
3
  module DeviantArt
4
4
  class Deviation::Content < Base
5
+ attr_accessor :html, :css, :css_fonts
5
6
  end
6
7
  end
@@ -2,5 +2,6 @@ require 'deviantart/base'
2
2
 
3
3
  module DeviantArt
4
4
  class Deviation::Download < Base
5
+ attr_accessor :src, :width, :height, :filesize
5
6
  end
6
7
  end
@@ -0,0 +1,8 @@
1
+ require 'deviantart/base'
2
+
3
+ module DeviantArt
4
+ class Deviation::EmbeddedContent < Base
5
+ attr_accessor :has_more, :next_offset, :has_less, :prev_offset, :results
6
+ point_to_class [:results, :[]], DeviantArt::Deviation
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'deviantart/base'
2
+
3
+ module DeviantArt
4
+ class Deviation::Metadata < Base
5
+ attr_accessor :metadata
6
+ point_to_class [:metadata, :[], :author], DeviantArt::User
7
+ end
8
+ end
@@ -2,6 +2,7 @@ require 'deviantart/base'
2
2
 
3
3
  module DeviantArt
4
4
  class Deviation::WhoFaved < Base
5
+ attr_accessor :has_more, :next_offset, :results
5
6
  point_to_class [:results, :[], :user], DeviantArt::User
6
7
  end
7
8
  end
@@ -1,3 +1,3 @@
1
1
  module DeviantArt
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deviantart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code Ass
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-29 00:00:00.000000000 Z
11
+ date: 2016-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,6 +69,9 @@ files:
69
69
  - bin/setup
70
70
  - deviantart.gemspec
71
71
  - lib/deviantart.rb
72
+ - lib/deviantart/authorization_code.rb
73
+ - lib/deviantart/authorization_code/access_token.rb
74
+ - lib/deviantart/authorization_code/refresh_token.rb
72
75
  - lib/deviantart/base.rb
73
76
  - lib/deviantart/client.rb
74
77
  - lib/deviantart/client/collections.rb
@@ -77,6 +80,8 @@ files:
77
80
  - lib/deviantart/client/feed.rb
78
81
  - lib/deviantart/client/gallery.rb
79
82
  - lib/deviantart/client/user.rb
83
+ - lib/deviantart/client_credentials.rb
84
+ - lib/deviantart/client_credentials/access_token.rb
80
85
  - lib/deviantart/collections.rb
81
86
  - lib/deviantart/collections/folders.rb
82
87
  - lib/deviantart/data.rb
@@ -87,6 +92,8 @@ files:
87
92
  - lib/deviantart/deviation.rb
88
93
  - lib/deviantart/deviation/content.rb
89
94
  - lib/deviantart/deviation/download.rb
95
+ - lib/deviantart/deviation/embeddedcontent.rb
96
+ - lib/deviantart/deviation/metadata.rb
90
97
  - lib/deviantart/deviation/whofaved.rb
91
98
  - lib/deviantart/error.rb
92
99
  - lib/deviantart/feed.rb