Class: DeviantArt::Client
- Inherits:
-
Object
- Object
- DeviantArt::Client
- Defined in:
- lib/deviantart/client/data.rb,
lib/deviantart/client/feed.rb,
lib/deviantart/client/user.rb,
lib/deviantart/client/gallery.rb,
lib/deviantart/client/deviation.rb,
lib/deviantart/client/collections.rb,
lib/deviantart/client.rb
Defined Under Namespace
Modules: Collections, Data, Deviation, Feed, Gallery, User
Constant Summary collapse
- @@default_host =
'www.deviantart.com'
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#access_token_auto_refresh ⇒ Object
Returns the value of attribute access_token_auto_refresh.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#code ⇒ Object
Returns the value of attribute code.
-
#grant_type ⇒ Object
Returns the value of attribute grant_type.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#user_agent ⇒ Object
User agent name.
Class Method Summary collapse
-
.default_host ⇒ Object
Default host name, it’s ‘www.deviantart.com’.
Instance Method Summary collapse
-
#access_token_auto_refresh? ⇒ Boolean
Auto refresh access token flag.
-
#initialize(options = {}) {|_self| ... } ⇒ Client
constructor
Create client object.
-
#on_refresh_access_token(&block) ⇒ Object
Call given block when access token is refreshed.
-
#on_refresh_authorization_code(&block) ⇒ Object
Call given block when authorization code is refreshed.
-
#perform(klass, method, path, params = {}) ⇒ Object
Access API with params by method.
-
#refresh_access_token ⇒ Object
Refresh access token by authorization code or client credentials.
Methods included from Feed
Methods included from Data
#get_countries, #get_privacy, #get_submission, #get_tos
Methods included from User
#damntoken, #get_friends, #get_profile, #get_status, #get_statuses, #get_watchers, #search_friends, #unwatch, #update_profile, #watch, #watch_status, #whoami, #whois
Methods included from Collections
#create_collection_folder, #fave, #get_collections, #get_collections_folders, #remove_collection_folder, #unfave
Methods included from Gallery
#create_gallery_folder, #get_gallery, #get_gallery_all, #get_gallery_folders, #remove_gallery_folder
Methods included from Deviation
#download_deviation, #get_deviation, #get_deviation_content, #get_deviation_embeddedcontent, #get_deviation_metadata, #get_deviation_whofaved
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Client
Create client object.
- #host
-
Host name to access API.
- #access_token_auto_refresh
-
Bool for auto refresh access token.
- #grant_type
-
Use for refresh token.
-
:authorization_code -
:client_credentials
-
- #access_token
-
This is valid access token for now.
For refresh token with :authorization_code
- #client_id
-
App’s
client_id. - #client_secret
-
App’s
client_secret. - #redirect_uri
-
URL what is exactly match the value in authorization step.
- #code
-
Authorization step returns this.
- #refresh_token
-
Refresh token for
:authorization_code. - #headers
-
Add custom headers for request.
For refresh token with :client_credentials
- #client_id
-
App’s
client_id. - #client_secret
-
App’s
client_secret.
Example
da = DeviantArt::Client.new do |config|
config.client_id = 9999
config.client_secret = 'LMNOPQRSTUVWXYZZZZZZZZ9999999999'
config.grant_type = :client_credentials
# auto refresh access_token with Client Credentials Grant when expired
config.access_token_auto_refresh = true
end
deviation = da.get_deviation('F98C2XXX-C6A8-XXXX-08F9-57CCXXXXX187')
deviation.title # => deviation's title
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/deviantart/client.rb', line 70 def initialize( = {}) @access_token = nil @host = @@default_host @on_refresh_access_token = [] @on_refresh_authorization_code = [] @access_token_auto_refresh = true @grant_type = nil @headers = {} .each do |key, value| instance_variable_set("@#{key}", value) end yield(self) if block_given? @http = Net::HTTP.new(@host, 443) @http.use_ssl = true end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def access_token @access_token end |
#access_token_auto_refresh ⇒ Object
Returns the value of attribute access_token_auto_refresh.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def access_token_auto_refresh @access_token_auto_refresh end |
#client_id ⇒ Object
Returns the value of attribute client_id.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def client_secret @client_secret end |
#code ⇒ Object
Returns the value of attribute code.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def code @code end |
#grant_type ⇒ Object
Returns the value of attribute grant_type.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def grant_type @grant_type end |
#headers ⇒ Object
Returns the value of attribute headers.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def host @host end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
30 31 32 |
# File 'lib/deviantart/client.rb', line 30 def refresh_token @refresh_token end |
#user_agent ⇒ Object
User agent name
92 93 94 |
# File 'lib/deviantart/client.rb', line 92 def user_agent @user_agent ||= "DeviantArtRubyGem/#{DeviantArt::VERSION}/#{RUBY_DESCRIPTION}" end |
Class Method Details
.default_host ⇒ Object
Default host name, it’s ‘www.deviantart.com’
87 88 89 |
# File 'lib/deviantart/client.rb', line 87 def self.default_host @@default_host end |
Instance Method Details
#access_token_auto_refresh? ⇒ Boolean
Auto refresh access token flag
97 98 99 |
# File 'lib/deviantart/client.rb', line 97 def access_token_auto_refresh? @access_token_auto_refresh && !@grant_type.nil? end |
#on_refresh_access_token(&block) ⇒ Object
Call given block when access token is refreshed
143 144 145 |
# File 'lib/deviantart/client.rb', line 143 def on_refresh_access_token(&block) @on_refresh_access_token << block end |
#on_refresh_authorization_code(&block) ⇒ Object
Call given block when authorization code is refreshed
138 139 140 |
# File 'lib/deviantart/client.rb', line 138 def (&block) @on_refresh_authorization_code << block end |
#perform(klass, method, path, params = {}) ⇒ Object
Access API with params by method
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/deviantart/client.rb', line 102 def perform(klass, method, path, params = {}) if @access_token.nil? && access_token_auto_refresh? refresh_access_token end response = request(method, path, params) if response.code == '401' && access_token_auto_refresh? refresh_access_token response = request(method, path, params) end status_code = response.code.to_i case status_code when 200 klass.new(response.json) when 400 # Request failed due to client error, # e.g. validation failed or User not found DeviantArt::Error.new(response.json, status_code) when 401 # Invalid token DeviantArt::Error.new(response.json, status_code) when 429 # Rate limit reached or service overloaded DeviantArt::Error.new(response.json, status_code) when 500 # Our servers encountered an internal error, try again DeviantArt::Error.new(response.json, status_code) when 503 # Our servers are currently unavailable, try again later. # This is normally due to planned or emergency maintenance. DeviantArt::Error.new(response.json, status_code) else DeviantArt::Error.new(response.json, status_code) end end |
#refresh_access_token ⇒ Object
Refresh access token by authorization code or client credentials
148 149 150 151 152 153 154 155 |
# File 'lib/deviantart/client.rb', line 148 def refresh_access_token case @grant_type.to_sym when :authorization_code when :client_credentials refresh_client_credentials end end |