Class: DeviantArt::Client

Inherits:
Object
  • Object
show all
Includes:
Collections, Data, Deviation, Feed, Gallery, User
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Feed

#get_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_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.

Yields:

  • (_self)

Yield Parameters:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/deviantart/client.rb', line 52

def initialize(options = {})
  @access_token = nil
  @host = @@default_host
  @on_refresh_access_token = nil
  @on_refresh_authorization_code = nil
  @access_token_auto_refresh = true
  @grant_type = nil
  @headers = {}
  options.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_tokenObject

Returns the value of attribute access_token.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def access_token
  @access_token
end

#access_token_auto_refreshObject

Returns the value of attribute access_token_auto_refresh.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def access_token_auto_refresh
  @access_token_auto_refresh
end

#client_idObject

Returns the value of attribute client_id.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def client_secret
  @client_secret
end

#codeObject

Returns the value of attribute code.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def code
  @code
end

#grant_typeObject

Returns the value of attribute grant_type.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def grant_type
  @grant_type
end

#headersObject

Returns the value of attribute headers.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def host
  @host
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



25
26
27
# File 'lib/deviantart/client.rb', line 25

def refresh_token
  @refresh_token
end

#user_agentObject

User agent name



74
75
76
# File 'lib/deviantart/client.rb', line 74

def user_agent
  @user_agent ||= "DeviantArtRubyGem/#{DeviantArt::VERSION}/#{RUBY_DESCRIPTION}"
end

Class Method Details

.default_hostObject

Default host name, it’s ‘www.deviantart.com



69
70
71
# File 'lib/deviantart/client.rb', line 69

def self.default_host
  @@default_host
end

Instance Method Details

#access_token_auto_refresh?Boolean

Auto refresh access token flag

Returns:

  • (Boolean)


79
80
81
# File 'lib/deviantart/client.rb', line 79

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



124
125
126
# File 'lib/deviantart/client.rb', line 124

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



119
120
121
# File 'lib/deviantart/client.rb', line 119

def on_refresh_authorization_code(&block)
  @on_refresh_authorization_code = block
end

#perform(klass, method, path, params = {}) ⇒ Object

Access API with params by method



84
85
86
87
88
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
# File 'lib/deviantart/client.rb', line 84

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
  case response.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, response.code.to_i)
  when '401'
    # Invalid token
    DeviantArt::Error.new(response.json, response.code.to_i)
  when '429'
    # Rate limit reached or service overloaded
    DeviantArt::Error.new(response.json, response.code.to_i)
  when '500'
    # Our servers encountered an internal error, try again
    DeviantArt::Error.new(response.json, response.code.to_i)
  when '503'
    # Our servers are currently unavailable, try again later.
    # This is normally due to planned or emergency maintenance.
    DeviantArt::Error.new(response.json, response.code.to_i)
  else
    DeviantArt::Error.new(response.json, response.code.to_i)
  end
end