Class: Upload::ImageForrest

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, thumb_width = "500") ⇒ ImageForrest

Public: Init a ImageForrest Object

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

Example

path = '/Users/kevin/Desktop/image.jpeg'
image_forrest = ImageForrest.new(path)

Returns an ImageForrest object

TODO:

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


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

def initialize(path, thumb_width = "500")
  @image = Image.new(path)
  @thumb_width = thumb_width
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



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

def image
  @image
end

#thumb_widthObject (readonly)

Returns the value of attribute thumb_width.



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

def thumb_width
  @thumb_width
end

Instance Method Details

#uploadObject

Public: upload an image to HOST

- Default thumb_width is 500

Example

Upload::ImageForrest.new(path).upload("700")

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


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/upload/image_forrest.rb', line 42

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

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

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