Class: Transform

Inherits:
Object
  • Object
show all
Includes:
TransformUtils
Defined in:
lib/filestack/models/filestack_transform.rb

Overview

Class for creating transformation chains and storing them to Filestack

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TransformUtils

#add_transform_task

Constructor Details

#initialize(handle: nil, external_url: nil, security: nil, apikey: nil) ⇒ Transform

Returns a new instance of Transform



9
10
11
12
13
14
15
# File 'lib/filestack/models/filestack_transform.rb', line 9

def initialize(handle: nil, external_url: nil, security: nil, apikey: nil)
  @apikey = apikey
  @handle = handle
  @external_url = external_url
  @security = security
  @transform_tasks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, **args) ⇒ Filestack::Transform

Catches method calls and checks to see if they exist in transformation list

This is to avoid rewriting the same code over and over for transform chaining

Returns:

  • (Filestack::Transform)

    or Error



23
24
25
26
27
28
29
30
31
32
# File 'lib/filestack/models/filestack_transform.rb', line 23

def method_missing(method_name, **args)
  if TransformConfig::TRANSFORMATIONS.include? method_name.to_s
    @transform_tasks.push(
      add_transform_task(method_name, args)
    )
    self
  else
    super
  end
end

Instance Attribute Details

#external_urlObject (readonly)

Returns the value of attribute external_url



7
8
9
# File 'lib/filestack/models/filestack_transform.rb', line 7

def external_url
  @external_url
end

#handleObject (readonly)

Returns the value of attribute handle



7
8
9
# File 'lib/filestack/models/filestack_transform.rb', line 7

def handle
  @handle
end

#securityObject (readonly)

Returns the value of attribute security



7
8
9
# File 'lib/filestack/models/filestack_transform.rb', line 7

def security
  @security
end

Instance Method Details

#av_convert(options) ⇒ Filestack::AV

Converts video or audio based on user-provided parameters

Parameters:

  • options (Hash)

    User-provided parameters

Returns:

  • (Filestack::AV)


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/filestack/models/filestack_transform.rb', line 51

def av_convert(options)
  if @external_url
    return 'av_convert does not support external URLs. Please upload file first.'
  end
  @transform_tasks.push(
    add_transform_task('video_convert', options)
  )
  response = UploadUtils.make_call(url, 'post')
  if response.code == 200
    return AV.new(url, apikey: @apikey, security: @security)
  end
  response.body
end

#debugTyphoeus::Response

Add debug parameter to get information on transformation image

Returns:

  • (Typhoeus::Response)


68
69
70
71
72
73
# File 'lib/filestack/models/filestack_transform.rb', line 68

def debug
  @transform_tasks.push(
    add_transform_task('debug')
  )
  UploadUtils.make_call(url, 'get').body
end

#filetype_conversion(options) ⇒ Filestack::Transform

Converts one filetype to the other

Parameters:

  • options

Returns:

  • (Filestack::Transform)


39
40
41
42
43
44
# File 'lib/filestack/models/filestack_transform.rb', line 39

def filetype_conversion(options)
  @transform_tasks.push(
    add_transform_task('output', options)
  )
  self
end

#respond_to_missing?(method_name) ⇒ Boolean

Override default method (best practice when overriding method_missing)

Returns:

  • (Boolean)


88
89
90
# File 'lib/filestack/models/filestack_transform.rb', line 88

def respond_to_missing?(method_name, *)
  TransformConfig::TRANSFORMATIONS.include?(method_name.to_s || super)
end

#storeFilestack::FilestackFilelink

Stores a transformation URL and returns a filelink

Returns:

  • (Filestack::FilestackFilelink)


78
79
80
81
82
83
84
85
# File 'lib/filestack/models/filestack_transform.rb', line 78

def store
  @transform_tasks.push(
    add_transform_task('store', {})
  )
  response = UploadUtils.make_call(url, 'get')
  handle = response.body['url'].split('/').last
  FilestackFilelink.new(handle, apikey: @apikey, security: @security)
end

#urlString

Creates a URL based on transformation instance state

Returns:

  • (String)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/filestack/models/filestack_transform.rb', line 95

def url
  base = [FilestackConfig::CDN_URL]
  if @transform_tasks.include? 'debug'
    @transform_tasks.delete('debug')
    base.push('debug')
  end
  base.push(@apikey) if @apikey && @external_url
  if @security
    policy = @security.policy
    signature = @security.signature
    security_string = "security=policy:#{policy},signature:#{signature}"
    base.push(security_string)
  end
  base += @transform_tasks
  base.push(@handle || @external_url)
  base.join('/')
end