Class: Zara4::API::Client

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/zara4/api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ Client

Application authenticated client.



15
16
17
18
19
20
21
22
23
24
# File 'lib/zara4/api/client.rb', line 15

def initialize(credentials)
  @client_id     = credentials.fetch('client_id')
  @client_secret = credentials.fetch('client_secret')
  
  if @client_id && @client_secret
    authenticator = Zara4::API::Communication::Authentication::ApplicationAuthenticator.new(@client_id, @client_secret)
    authenticator.with_image_processing().with_usage()
    @access_token = authenticator.acquire_access_token()
  end
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



9
10
11
# File 'lib/zara4/api/client.rb', line 9

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



9
10
11
# File 'lib/zara4/api/client.rb', line 9

def client_secret
  @client_secret
end

Instance Method Details

#download_processed_image(processed_image, save_path) ⇒ Object

Download the given ProcessedImage and save it to the given path.

Parameters:

  • processed_image

    The ProcessedImage to be downloaded.

  • save_path

    The path where the image should be saved.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/zara4/api/client.rb', line 65

def download_processed_image(processed_image, save_path)
  
  url = processed_image.file_urls[0]
  
  if @access_token
    url += '?access_token=' + @access_token.token
  end

  
  File.open(save_path, "w") do |f|
    IO.copy_stream(open(url), f)
  end
  
end

#process_image(image_processing_request) ⇒ Object

Process the given image processing Request.

Parameters:

  • image_processing_request

    The request to be processed



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zara4/api/client.rb', line 32

def process_image(image_processing_request)
  url = Zara4::API::Communication::Util::url('/v1/image-processing/request')
      
  parameters = image_processing_request.generate_form_data
  parameters['access_token'] = @access_token.token
  
  response = self.class.post(url, {
    query: parameters,
    detect_mime_type: true
  })
        
  
  # Check for API error response
  if response.has_key?('error')
    raise 'ERROR IS ' + response.fetch('error')
  end
       
  
  request_id                = response['request-id']
  generate_images_urls      = response['generated-images']['urls']
  bytes_original            = response['compression']['bytes-original']
  bytes_compressed          = response['compression']['bytes-compressed']
  
  return Zara4::API::ImageProcessing::ProcessedImage.new(image_processing_request, request_id, generate_images_urls, bytes_original, bytes_compressed)
end