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



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

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



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

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.



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

def external_url
  @external_url
end

#handleObject (readonly)

Returns the value of attribute handle.



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

def handle
  @handle
end

#securityObject (readonly)

Returns the value of attribute security.



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

def security
  @security
end

Instance Method Details

#av_convert(options) ⇒ Filestack::AV

Converts video or audio based on user-provided parameters



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

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
  JSON.parse(response.body)
end

#debugHash

Add debug parameter to get information on transformation image



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

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

#filetype_conversion(options) ⇒ Filestack::Transform

Converts one filetype to the other



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

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)



91
92
93
# File 'lib/filestack/models/filestack_transform.rb', line 91

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



80
81
82
83
84
85
86
87
88
# File 'lib/filestack/models/filestack_transform.rb', line 80

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

#urlString

Creates a URL based on transformation instance state



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

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