Class: Zara4::API::Communication::Grant::GrantRequest

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/zara4/api/communication/grant/grant_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, scopes) ⇒ GrantRequest

Constructor



12
13
14
15
16
# File 'lib/zara4/api/communication/grant/grant_request.rb', line 12

def initialize(client_id, client_secret, scopes)
  @client_id     = client_id
  @client_secret = client_secret
  @scopes        = scopes
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



6
7
8
# File 'lib/zara4/api/communication/grant/grant_request.rb', line 6

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



6
7
8
# File 'lib/zara4/api/communication/grant/grant_request.rb', line 6

def client_secret
  @client_secret
end

#scopesObject

Returns the value of attribute scopes.



6
7
8
# File 'lib/zara4/api/communication/grant/grant_request.rb', line 6

def scopes
  @scopes
end

Instance Method Details

#get_tokensObject

Get tokens from the API



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zara4/api/communication/grant/grant_request.rb', line 22

def get_tokens

  url = Zara4::API::Communication::Util::url('/oauth/access_token')
  
  parameters = {
    'grant_type'    => grant_type(),
    'client_id'     => @client_id,
    'client_secret' => @client_secret,
    'scope'         => @scopes.join(','),
  }
  
  headers = {
    'Content-Type'  => 'application/json'
  }
  
  response = self.class.post(url, {
    body: parameters.to_json,
    headers: headers
  })
  
  
  # Check for API error response
  if response.has_key?('error')
    puts 'ERROR IS ' + response.fetch('error')
  end
  
  return {
    'access_token' => response.fetch('access_token'),
    'expires_in'   => response.fetch('expires_in'),
  }
end

#with_image_processingObject

Add image processing to the request scope.



58
59
60
# File 'lib/zara4/api/communication/grant/grant_request.rb', line 58

def with_image_processing
  @scopes.push('image-processing')
end

#with_usageObject

Add usage to the request scope.



66
67
68
# File 'lib/zara4/api/communication/grant/grant_request.rb', line 66

def with_usage
  @scopes.push('usage')
end