class DeviantArt::Client

Attributes

access_token[RW]
access_token_auto_refresh[RW]
client_id[RW]
client_secret[RW]
code[RW]
grant_type[RW]
headers[RW]
host[RW]
redirect_uri[RW]
refresh_token[RW]
user_agent[W]

Public Class Methods

default_host() click to toggle source

Default host name, it's 'www.deviantart.com'

# File lib/deviantart/client.rb, line 87
def self.default_host
  @@default_host
end
new(options = {}) { |self| ... } click to toggle source

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
# File lib/deviantart/client.rb, line 70
def initialize(options = {})
  @access_token = nil
  @host = @@default_host
  @on_refresh_access_token = []
  @on_refresh_authorization_code = []
  @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

Public Instance Methods

access_token_auto_refresh?() click to toggle source

Auto refresh access token flag

# 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) click to toggle source

Call given block when access token is refreshed

# File lib/deviantart/client.rb, line 143
def on_refresh_access_token(&block)
  @on_refresh_access_token << block
end
on_refresh_authorization_code(&block) click to toggle source

Call given block when authorization code is refreshed

# File lib/deviantart/client.rb, line 138
def on_refresh_authorization_code(&block)
  @on_refresh_authorization_code << block
end
perform(klass, method, path, params = {}) click to toggle source

Access API with params by method

# 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() click to toggle source

Refresh access token by authorization code or client credentials

# File lib/deviantart/client.rb, line 148
def refresh_access_token
  case @grant_type.to_sym
  when :authorization_code
    refresh_authorization_code
  when :client_credentials
    refresh_client_credentials
  end
end
user_agent() click to toggle source

User agent name

# File lib/deviantart/client.rb, line 92
def user_agent
  @user_agent ||= "DeviantArtRubyGem/#{DeviantArt::VERSION}/#{RUBY_DESCRIPTION}"
end

Private Instance Methods

refresh_authorization_code() click to toggle source
# File lib/deviantart/client.rb, line 222
        def refresh_authorization_code
  response = request(
    :post, '/oauth2/token',
    { grant_type: 'refresh_token', client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token }
  )
  status_code = response.code.to_i
  if status_code == 200
    @access_token = response.json['access_token']
    if !@on_refresh_access_token.empty?
      @on_refresh_access_token.each do |p|
        p.call(@access_token)
      end
    end
    if !@on_refresh_authorization_code.empty?
      @on_refresh_authorization_code.each do |p|
        p.call(@access_token, response.json['refresh_token'])
      end
    end
    AuthorizationCode::RefreshToken.new(response.json)
  else
    @access_token = nil
    DeviantArt::Error.new(response.json, status_code)
  end
end
refresh_client_credentials() click to toggle source
# File lib/deviantart/client.rb, line 202
        def refresh_client_credentials
  response = request(
    :post, '/oauth2/token',
    { grant_type: 'client_credentials', client_id: @client_id, client_secret: @client_secret }
  )
  status_code = response.code.to_i
  if status_code == 200
    @access_token = response.json['access_token']
    if !@on_refresh_access_token.empty?
      @on_refresh_access_token.each do |p|
        p.call(@access_token)
      end
    end
    ClientCredentials::AccessToken.new(response.json)
  else
    @access_token = nil
    DeviantArt::Error.new(response.json, status_code)
  end
end
request(method, path, params = {}) click to toggle source
# File lib/deviantart/client.rb, line 157
        def request(method, path, params = {})
  uri = URI.parse("https://#{@host}#{path}")
  if params.any?{ |key, value| value.is_a?(Enumerable) }
    converted_params = []
    params.each do |key, value|
      if value.is_a?(Hash)
        value.each_pair do |k, v|
          converted_params << ["#{key}[#{k}]", v]
        end
      elsif value.is_a?(Enumerable)
        value.each_index do |i|
          converted_params << ["#{key}[#{i}]", value[i]]
        end
      else
        converted_params << [key, value]
      end
    end
    params = converted_params
  end
  case method
  when :get
    uri.query = URI.encode_www_form(params)
    request = Net::HTTP::Get.new(uri)
  when :post
    request = Net::HTTP::Post.new(uri.path)
    request.body = URI.encode_www_form(params)
  end
  request['Content-Type'] = 'application/x-www-form-urlencoded'
  request['User-Agent'] = user_agent
  if not @access_token.nil?
    request['Authorization'] = "Bearer #{@access_token}"
  end
  @headers.each_pair do |key, value|
    request[key] = value
  end
  response = @http.request(request)
  if response.code == '403'
    # need to send User-Agent and use HTTP compression in request
    response.json = JSON.parse('{}')
  else
    response.json = JSON.parse(response.body)
  end
  response
end