imgurruby 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/imgurruby.gemspec +21 -0
- data/lib/imgurruby/version.rb +3 -0
- data/lib/imgurruby.rb +52 -0
- metadata +85 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Asitha de Silva
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Imgur Ruby
|
2
|
+
|
3
|
+
Based on [Cubee's imgur-ruby](https://github.com/cubeee/imgur-ruby) script.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'imgurruby', :git => 'https://github.com/asithade/imgur-ruby'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install imgurruby
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
@image = Imgurruby::Imgur.new('YOUR_API_KEY')
|
22
|
+
@image.upload(params[:file])
|
23
|
+
|
24
|
+
You can then access the url/error by calling
|
25
|
+
|
26
|
+
@image.url
|
27
|
+
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
I'm quite a newbie with Ruby, so feel free to add your own insight!
|
32
|
+
|
33
|
+
1. Fork it ( http://github.com/<my-github-username>/imgurruby/fork )
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/imgurruby.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'imgurruby/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "imgurruby"
|
8
|
+
spec.version = Imgurruby::VERSION
|
9
|
+
spec.authors = ["Asitha de Silva"]
|
10
|
+
spec.email = ["asithade@gmail.com"]
|
11
|
+
spec.summary = %q{Imgur image uploader via Imgur API}
|
12
|
+
spec.homepage = "https://github.com/asithade/imgur-ruby"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
20
|
+
spec.add_development_dependency "rake"
|
21
|
+
end
|
data/lib/imgurruby.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "imgurruby/version"
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
require 'base64'
|
5
|
+
require 'rexml/document'
|
6
|
+
|
7
|
+
module Imgurruby
|
8
|
+
attr_accessor :api_key, :host, :proxy_addr, :proxy_port, :url, :msg
|
9
|
+
class Imgur
|
10
|
+
def initialize(key, addr = nil, port = nil)
|
11
|
+
@api_key = key
|
12
|
+
@host = 'api.imgur.com'
|
13
|
+
@proxy_addr = addr
|
14
|
+
@proxy_port = port
|
15
|
+
end
|
16
|
+
|
17
|
+
def upload(file)
|
18
|
+
# Image Data Variable
|
19
|
+
imagedata = nil
|
20
|
+
|
21
|
+
# Temp File
|
22
|
+
tmpfile = file
|
23
|
+
|
24
|
+
# File Name
|
25
|
+
file_name = tmpfile.original_filename
|
26
|
+
|
27
|
+
# Read File Binary
|
28
|
+
File.open(tmpfile.tempfile.path, "rb") do |file|
|
29
|
+
# Base64 Encode Image Data
|
30
|
+
imagedata = Base64.encode64 file.read
|
31
|
+
end
|
32
|
+
|
33
|
+
# Begin file upload
|
34
|
+
Net::HTTP::Proxy(@proxy_addr, @proxy_port).start(@host,80) {|http|
|
35
|
+
res = Net::HTTP.post_form(URI.parse('http://api.imgur.com/2/upload'), {'image' => imagedata, 'key' => @api_key})
|
36
|
+
xml_data = res.body
|
37
|
+
doc = REXML::Document.new(xml_data)
|
38
|
+
doc.elements.each('*/message') do |element|
|
39
|
+
@msg = element.text
|
40
|
+
end
|
41
|
+
doc.elements.each('upload/links/original') do |element|
|
42
|
+
@url = element.text
|
43
|
+
end
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return msg or url
|
48
|
+
def url
|
49
|
+
if @msg ? @msg : @url
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: imgurruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Asitha de Silva
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-01-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.5'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.5'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description:
|
47
|
+
email:
|
48
|
+
- asithade@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- imgurruby.gemspec
|
59
|
+
- lib/imgurruby.rb
|
60
|
+
- lib/imgurruby/version.rb
|
61
|
+
homepage: https://github.com/asithade/imgur-ruby
|
62
|
+
licenses: []
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.24
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Imgur image uploader via Imgur API
|
85
|
+
test_files: []
|