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
Methods included from Feed
Methods included from Data
#get_countries, #get_privacy, #get_submission, #get_tos
Methods included from User
#get_friends, #get_profile, #get_status, #get_statuses, #get_watchers, #search_friends, #whoami, #whois
Methods included from Collections
#get_collections, #get_collections_folders
Methods included from Gallery
#get_gallery, #get_gallery_all, #get_gallery_folders
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.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/deviantart/client.rb', line 57 def initialize( = {}) @access_token = nil @host = @@default_host @on_refresh_access_token = [] = [] @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
79 80 81 |
# File 'lib/deviantart/client.rb', line 79 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’
74 75 76 |
# File 'lib/deviantart/client.rb', line 74 def self.default_host @@default_host end |
Instance Method Details
#access_token_auto_refresh? ⇒ Boolean
Auto refresh access token flag
84 85 86 |
# File 'lib/deviantart/client.rb', line 84 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
130 131 132 |
# File 'lib/deviantart/client.rb', line 130 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
125 126 127 |
# File 'lib/deviantart/client.rb', line 125 def (&block) << block end |
#perform(klass, method, path, params = {}) ⇒ Object
Access API with params by method
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/deviantart/client.rb', line 89 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
134 135 136 137 138 139 140 141 |
# File 'lib/deviantart/client.rb', line 134 def refresh_access_token case @grant_type.to_sym when :authorization_code when :client_credentials refresh_client_credentials end end |