Class: Snapshot::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/snapshot/image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filesizeObject

Returns the value of attribute filesize.



5
6
7
# File 'lib/snapshot/image.rb', line 5

def filesize
  @filesize
end

#formatObject

Returns the value of attribute format.



5
6
7
# File 'lib/snapshot/image.rb', line 5

def format
  @format
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/snapshot/image.rb', line 5

def height
  @height
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/snapshot/image.rb', line 5

def id
  @id
end

#last_accessed_atObject

Returns the value of attribute last_accessed_at.



5
6
7
# File 'lib/snapshot/image.rb', line 5

def last_accessed_at
  @last_accessed_at
end

#uploaded_atObject Also known as: created_at

Returns the value of attribute uploaded_at.



5
6
7
# File 'lib/snapshot/image.rb', line 5

def uploaded_at
  @uploaded_at
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/snapshot/image.rb', line 5

def width
  @width
end

Class Method Details

.create(file) ⇒ Object

Uploads a file to the connected Snapshot account.

Parameters

  • file - File or path to a file

Examples

# With a path:
img = Snapshot::Image.create('example.jpg')

# With a file:
file = File.open('example.jpg')
img = Snapshot::Image.create(file)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/snapshot/image.rb', line 27

def create(file)
  file = File.open(file) if file.is_a?(String)
  result = Snapshot.connection.post('/', file: file)
  info = JSON.parse(result)['file']
  
  self.new.tap do |img|
    img.filesize = info['size'].to_i
    img.format = info['type']
    img.height = info['height'].to_i
    img.id = info['id']
    img.uploaded_at = Time.at(info['uploaded_at'].to_i)
    img.width = info['width'].to_i
  end
end

.destroy(id) ⇒ Object

Deletes a file from Snapshot.

Parameters

  • id - The ID of the image

Examples

Snapshot::Image.destroy('a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a')


52
53
54
# File 'lib/snapshot/image.rb', line 52

def destroy(id)
  find(id).destroy
end

.find(id) ⇒ Object

Finds a file on the connected Snapshot account.

Parameters

  • id - The ID of the image

Examples

Snapshot::Image.find('a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a')


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/snapshot/image.rb', line 66

def find(id)
  result = Snapshot.connection.get("/#{id}")
  info = JSON.parse(result)
  
  self.new.tap do |img|
    img.filesize = info['size'].to_i
    img.format = info['type']
    img.height = info['height'].to_i
    img.id = info['id']
    img.last_accessed_at = Time.at(info['last_accessed_at'].to_i)
    img.uploaded_at = Time.at(info['uploaded_at'].to_i)
    img.width = info['width'].to_i
  end
rescue RestClient::ResourceNotFound
  nil
end

Instance Method Details

#destroyObject

Removes the image from the connect Snapshot account

Examples

img = Snapshot::Image.find('a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a')
img.destroy


91
92
93
94
95
96
# File 'lib/snapshot/image.rb', line 91

def destroy
  Snapshot.connection.delete("/#{id}")
  true
rescue RestClient::ResourceNotFound
  nil
end

#url(&block) ⇒ Object

Return the URL of the image

Examples

img = Snapshot::Image.find('a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a')
img.url # => http://subdomain.snapshothq.com/a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a.jpg


105
106
107
# File 'lib/snapshot/image.rb', line 105

def url(&block)
  Snapshot::URL.new(id, extension, &block)
end