Class: ClassicApi::Command::Images

Inherits:
Base
  • Object
show all
Defined in:
lib/classic_api/command/images.rb

Instance Method Summary collapse

Methods inherited from Base

api, handle_argument_error, start

Instance Method Details

#get(id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/classic_api/command/images.rb', line 12

def get(id)
  image = api.images.find(id)
  system "open #{image.url}" if options.open
  present(image)
  if image.children
    image.children.each do|child|
      present(child, 1)
    end
  end
end

#listObject



5
6
7
8
# File 'lib/classic_api/command/images.rb', line 5

def list
  images = api.images.all
  present(images)
end

#transform(id, strategy, width = nil, height = nil) ⇒ Object



25
26
27
28
29
# File 'lib/classic_api/command/images.rb', line 25

def transform(id, strategy, width=nil, height=nil)
  transformation = api.images.find(id).transform(strategy: strategy, width: width, height: height)
  system "open #{transformation.url}" if options.open
  present transformation
end

#upload(file, path = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/classic_api/command/images.rb', line 35

def upload(file, path=nil)
  # Validate arguments
  local_path = File.absolute_path(file)
  raise "File #{file} does not exist!" unless File.exists?(local_path)
  path = file if path.nil?
  
  # Create policy
  policy = api.policies.create!(path: path)
  
  # Throw override error if not forced
  if policy.image.stored_at and !options.force
    print_error("An image file already exists with path '#{path}'.")
    print_info("USAGE", "Use --force to overwrite.")
    exit(1)
  end
  
  # Upload image via policy
  result = policy.upload_file(local_path)
  
  # Prepare transforms
  transformations = options.transformations.map do |transformation_string|
    (strategy,width,height) = transformation_string.split("/")
    {strategy: strategy, width: width&.to_i, height: height&.to_i}
  end
  
  # Update image
  policy.image.update_attributes(stored_at: DateTime.now, transformations: transformations)
  
  # Open image if requested
  system "open #{policy.image.url}" if options.open
  
  # Respond with image
  print_success("Image successfully uploaded")
  present policy.image
end