Class: DeviantArt::Client

Inherits:
Object
  • Object
show all
Includes:
Collections, Data, Deviation, Feed, Gallery, User
Defined in:
lib/deviantart/client.rb

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_statuses, #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.

For refresh token with :client_credentials

#client_id

App’s client_id.

#client_secret

App’s client_secret.

Yields:

  • (_self)

Yield Parameters:



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/deviantart/client.rb', line 50

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



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

def access_token
  @access_token
end

#access_token_auto_refreshObject

Returns the value of attribute access_token_auto_refresh.



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

def access_token_auto_refresh
  @access_token_auto_refresh
end

#client_idObject

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



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

def client_secret
  @client_secret
end

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

#grant_typeObject

Returns the value of attribute grant_type.



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

def grant_type
  @grant_type
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



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

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



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

def refresh_token
  @refresh_token
end

#user_agentObject

User agent name



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

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

Class Method Details

.default_hostObject

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



64
65
66
# File 'lib/deviantart/client.rb', line 64

def self.default_host
  @@default_host
end

Instance Method Details

#access_token_auto_refresh?Boolean

Auto refresh access token flag

Returns:

  • (Boolean)


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

def access_token_auto_refresh?
  @access_token_auto_refresh
end

#on_refresh_access_token(&block) ⇒ Object

Call given block when access token is refreshed



97
98
99
# File 'lib/deviantart/client.rb', line 97

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



92
93
94
# File 'lib/deviantart/client.rb', line 92

def on_refresh_authorization_code(&block)
  @on_refresh_authorization_code = block
end

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

Access API with params by method



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/deviantart/client.rb', line 79

def perform(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
  response.json
end