upload_image 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d0bc061d0d9126fbcea3470edff33b91241ea6ca
4
+ data.tar.gz: b6d8c7d77e717ac5dece1389c5be67acb2d31184
5
+ SHA512:
6
+ metadata.gz: b2dc52d89baf95f0baca6d6916b714e5ac297cc6b00efb64bdd2035effd5286185f8a218fe0e09363c3a0df13ee243510537039c9ec0bada051461b79bbdb5d0
7
+ data.tar.gz: d9909e66a4b325e961417beee29f9bf4258d27aa86821ea4402d7633351a4ffa13189502627e0e00f5d6705cc1fcc86bbcb2a620bac818bbffde8b531a157a1c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
7
+ - 2.2.3
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in upload_image.gemspec
4
+ gemspec
5
+ gem 'rspec'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Duy Khoa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # UploadImage
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/upload_image`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'upload_image'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install upload_image
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'upload_image'
27
+
28
+ Upload::ImageForrest.new("/path/to/image").upload
29
+ ```
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( https://github.com/[my-github-username]/upload_image/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ Bundler::GemHelper.install_tasks
10
+
11
+ require 'rspec/core/rake_task'
12
+ RSpec::Core::RakeTask.new(:spec)
13
+
14
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "upload_image"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ module Upload
2
+ class Image
3
+ attr_reader :path
4
+
5
+ def initialize(path)
6
+ @path = path
7
+ end
8
+
9
+ def exist?
10
+ !!File.file?(path)
11
+ end
12
+
13
+ def file
14
+ File.new(@path, "rb")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,51 @@
1
+ require 'rest-client'
2
+
3
+ module Upload
4
+ class ImageForrest
5
+ HOST = "http://uploads.im/api"
6
+
7
+ attr_reader :image
8
+
9
+ # Public: Init a ImageForrest Object
10
+ #
11
+ # path - Image's path, right now must be an absolute path
12
+ #
13
+ # Example
14
+ #
15
+ # path = '/Users/kevin/Desktop/image.jpeg'
16
+ # image_forrest = ImageForrest.new(path)
17
+ #
18
+ # Returns an ImageForrest object
19
+ #
20
+ # TODO:
21
+ # - path can be a relative path
22
+ # - how about upload a list of images (idea)
23
+ def initialize(path)
24
+ @image = Image.new(path)
25
+ end
26
+
27
+ # Public: upload an image to HOST
28
+ #
29
+ # Example
30
+ #
31
+ # Upload::ImageForrest.new(path).upload
32
+ #
33
+ # Returns an Response object
34
+ # Can be Response::Failure and Response::Success
35
+ #
36
+ # - If @image isn't exist -> return a Response::Failure with status "File not found
37
+ # - If response.code is 200 -> return a Response::Success with response object
38
+ # - If response.code isn't 200 (4x, 5x) -> return a Response::Failure
39
+ def upload
40
+ return Response::Failure.new(nil, message: "File is not exist") unless @image.exist?
41
+
42
+ response = RestClient.post HOST, thumb_width: '500', upload: @image.file
43
+
44
+ if response.code == 200
45
+ Response::Success.new(response)
46
+ else
47
+ Response::Failure.new(response)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ module Upload
2
+ module Response
3
+ class Failure < Base
4
+ attr_reader :message
5
+
6
+ def initialize(response, opts = {})
7
+ super(response, opts)
8
+
9
+ @message = opts[:message]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ module Upload
2
+ module Response
3
+ class Success < Base
4
+ attr_reader :url, :original_url
5
+
6
+ def initialize(response, opts={})
7
+ super(response, opts)
8
+
9
+ # Use thumb url instead of the original one
10
+ # The thumb url has fixed width for now (500px)
11
+ # Will be configuarable soon.
12
+ # TODO:
13
+ # - Check the data field before return
14
+ # - Allow user to config the size of thumb
15
+ #
16
+ @url = @response["data"]["thumb_url"]
17
+
18
+ # carefully check the data key here
19
+ # TODO: add condition to check if there is data key
20
+ #
21
+ @original_url = @response["data"]["img_url"]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ module Upload
2
+ module Response
3
+ class Base
4
+ attr :response
5
+
6
+ def initialize(response, opts={})
7
+ @response = JSON.parse response if response
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module UploadImage
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__)))
2
+
3
+ require "upload/version"
4
+ require "upload/image_forrest"
5
+ require "upload/image"
6
+ require "upload/response"
7
+
8
+ require "upload/response/failure"
9
+ require "upload/response/success"
10
+
11
+ module Upload
12
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'upload/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "upload_image"
8
+ spec.version = UploadImage::VERSION
9
+ spec.authors = ["Duy Khoa"]
10
+ spec.email = ["duykhoa12t@gmail.com"]
11
+
12
+ spec.summary = "A tiny gem to upload image to upload.im"
13
+ spec.description = "Use RestClient to upload image to server"
14
+ spec.homepage = "https://github.com/duykhoa/upload_image"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rest-client", "~> 1.8"
25
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: upload_image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Duy Khoa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ description: Use RestClient to upload image to server
56
+ email:
57
+ - duykhoa12t@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/upload/image.rb
71
+ - lib/upload/image_forrest.rb
72
+ - lib/upload/response.rb
73
+ - lib/upload/response/failure.rb
74
+ - lib/upload/response/success.rb
75
+ - lib/upload/version.rb
76
+ - lib/upload_image.rb
77
+ - upload_image.gemspec
78
+ homepage: https://github.com/duykhoa/upload_image
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A tiny gem to upload image to upload.im
102
+ test_files: []
103
+ has_rdoc: