Class: Upload::ImageForest

Inherits:
Object
  • Object
show all
Defined in:
lib/upload/image_forest.rb

Constant Summary collapse

HOST =
"http://uploads.im/api"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ImageForest

Public: Init a ImageForest Object

path - Image’s path, right now must be an absolute path

Example

path = '/Users/kevin/Desktop/image.jpeg'
image_forest = ImageForest.new(path)

Returns an ImageForest object

TODO:

-  path can be a relative path
-  how about upload a list of images (idea)


23
24
25
# File 'lib/upload/image_forest.rb', line 23

def initialize(path)
  @image = Image.new(path)
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



7
8
9
# File 'lib/upload/image_forest.rb', line 7

def image
  @image
end

Instance Method Details

#uploadObject

Public: upload an image to HOST

Example

Upload::ImageForest.new(path).upload

Returns an Response object

Can be Response::Failure and Response::Success

- If @image isn't exist -> return a Response::Failure with status "File not found
- If response.code is 200 -> return a Response::Success with response object
- If response.code isn't 200 (4x, 5x) -> return a Response::Failure


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/upload/image_forest.rb', line 39

def upload
  return Response::Failure.new(nil, message: "File is not exist") unless @image.exist?

  response = RestClient.post HOST, thumb_width: '500', upload: @image.file

  if response.code == 200
    Response::Success.new(response)
  else
    Response::Failure.new(response)
  end
end