├── .gitignore ├── lib ├── gyazo │ ├── version.rb │ ├── error.rb │ └── client.rb └── gyazo.rb ├── test ├── test.png ├── test_helper.rb └── test_gyazo.rb ├── Gemfile ├── Rakefile ├── samples ├── list.rb └── upload.rb ├── .travis.yml ├── Makefile ├── LICENSE.txt ├── gyazo.gemspec ├── Gemfile.lock └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *#* 2 | *~ 3 | .DS_Store 4 | pkg 5 | tmp 6 | .ruby-version 7 | -------------------------------------------------------------------------------- /lib/gyazo/version.rb: -------------------------------------------------------------------------------- 1 | module Gyazo 2 | VERSION = '3.2.0' 3 | end 4 | -------------------------------------------------------------------------------- /test/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gyazo/gyazo-ruby/HEAD/test/test.png -------------------------------------------------------------------------------- /lib/gyazo/error.rb: -------------------------------------------------------------------------------- 1 | module Gyazo 2 | class Error < StandardError 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /lib/gyazo.rb: -------------------------------------------------------------------------------- 1 | require 'gyazo/version' 2 | require 'gyazo/error' 3 | require 'gyazo/client' 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in event_emitter.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'minitest/autorun' 3 | 4 | $:.unshift File.expand_path '../lib', File.dirname(__FILE__) 5 | require 'gyazo' 6 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rake/testtask" 3 | 4 | Rake::TestTask.new do |t| 5 | t.pattern = "test/test_*.rb" 6 | end 7 | 8 | task :default => :test 9 | -------------------------------------------------------------------------------- /samples/list.rb: -------------------------------------------------------------------------------- 1 | $:.unshift File.expand_path '../lib', File.dirname(__FILE__) 2 | 3 | require 'gyazo' 4 | 5 | gyazo = Gyazo::Client.new(ENV['GYAZO_TOKEN']) 6 | 7 | gyazo.list(:page => 1, :per_page => 5).each do |img| 8 | puts img['url'] 9 | end 10 | -------------------------------------------------------------------------------- /samples/upload.rb: -------------------------------------------------------------------------------- 1 | $:.unshift File.expand_path '../lib', File.dirname(__FILE__) 2 | 3 | require 'gyazo' 4 | 5 | gyazo = Gyazo::Client.new(ENV['GYAZO_TOKEN']) 6 | 7 | img_path = ARGV.shift 8 | 9 | res = gyazo.upload(img_path) 10 | puts res['url'] 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: ruby 3 | rvm: 4 | - 2.2.3 5 | env: 6 | secure: SKTPKUXfgOehDNdgdMxKykef1m7XswYKA72qANyqHQ3/P/9c7RcRfTSVD86wj4riT8BX5YTbNhKKQFa79HozioezF7tT5A6xF5qiJOt1sjjtMfhc0d3zqN3RVJNRpx+1+hgNpv9OQ6RrTdw3At1TjzvgdMAiJI4X8lqZ904Gu60= 7 | install: bundle install 8 | script: bundle exec rake test 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # バージョンを変えた場合はlib/gyazo/version.rbを変えること 3 | # 4 | 5 | localinstall: 6 | rake install 7 | gempush: 8 | rake release 9 | gitpush: 10 | git push git@github.com:masui/gyazo-ruby.git 11 | git push pitecan.com:/home/masui/git/gyazo-ruby.git 12 | 13 | test: test_always 14 | test_always: 15 | bundle exec rake test 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2014 Toshiyuki Masui 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. 23 | -------------------------------------------------------------------------------- /gyazo.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'gyazo/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "gyazo" 8 | spec.version = Gyazo::VERSION 9 | spec.authors = ["Toshiyuki Masui", "Sho Hashimoto", "Nana Kugayama"] 10 | spec.email = ["masui@pitecan.com"] 11 | spec.description = %q{Gyazo.com API Wrapper} 12 | spec.summary = spec.description 13 | spec.homepage = "http://github.com/gyazo/gyazo-ruby" 14 | spec.license = "MIT" 15 | 16 | spec.files = `git ls-files`.split($/).reject{|i| i=="Gemfile.lock" } 17 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 18 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 19 | spec.require_paths = ["lib"] 20 | 21 | spec.add_development_dependency "bundler" 22 | spec.add_development_dependency "rake" 23 | spec.add_development_dependency "minitest" 24 | 25 | spec.add_dependency "faraday", '< 2.0.0' 26 | spec.add_dependency "multipart-post" 27 | spec.add_dependency "mime-types" 28 | end 29 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | gyazo (3.1.1) 5 | faraday (< 2.0.0) 6 | mime-types 7 | multipart-post 8 | 9 | GEM 10 | remote: https://rubygems.org/ 11 | specs: 12 | faraday (1.8.0) 13 | faraday-em_http (~> 1.0) 14 | faraday-em_synchrony (~> 1.0) 15 | faraday-excon (~> 1.1) 16 | faraday-httpclient (~> 1.0.1) 17 | faraday-net_http (~> 1.0) 18 | faraday-net_http_persistent (~> 1.1) 19 | faraday-patron (~> 1.0) 20 | faraday-rack (~> 1.0) 21 | multipart-post (>= 1.2, < 3) 22 | ruby2_keywords (>= 0.0.4) 23 | faraday-em_http (1.0.0) 24 | faraday-em_synchrony (1.0.0) 25 | faraday-excon (1.1.0) 26 | faraday-httpclient (1.0.1) 27 | faraday-net_http (1.0.1) 28 | faraday-net_http_persistent (1.2.0) 29 | faraday-patron (1.0.0) 30 | faraday-rack (1.0.0) 31 | mime-types (3.4.1) 32 | mime-types-data (~> 3.2015) 33 | mime-types-data (3.2021.1115) 34 | minitest (5.6.1) 35 | multipart-post (2.1.1) 36 | rake (13.0.1) 37 | ruby2_keywords (0.0.5) 38 | 39 | PLATFORMS 40 | ruby 41 | 42 | DEPENDENCIES 43 | bundler 44 | gyazo! 45 | minitest 46 | rake 47 | 48 | BUNDLED WITH 49 | 2.6.2 50 | -------------------------------------------------------------------------------- /test/test_gyazo.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path 'test_helper', File.dirname(__FILE__) 2 | 3 | class TestGyazo < MiniTest::Test 4 | GYAZO_REGEXP = %r{^https://gyazo\.com/[a-z\d]{32}$} 5 | 6 | def setup 7 | @gyazo = Gyazo::Client.new access_token: ENV['GYAZO_TOKEN'] 8 | @imagefile = File.expand_path 'test.png', File.dirname(__FILE__) 9 | end 10 | 11 | def test_upload_filepath 12 | res = @gyazo.upload imagefile: @imagefile 13 | assert res[:permalink_url].match GYAZO_REGEXP 14 | end 15 | 16 | def test_upload_file 17 | res = @gyazo.upload imagefile: File.open(@imagefile), filename: 'test.png' 18 | assert res[:permalink_url].match GYAZO_REGEXP 19 | end 20 | 21 | def test_upload_with_collection_id 22 | res = @gyazo.upload imagefile: @imagefile, collection_id: ENV['GYAZO_COLLECTION_ID'] 23 | assert res[:permalink_url].match GYAZO_REGEXP 24 | end 25 | 26 | def test_list 27 | list = @gyazo.list 28 | assert_instance_of Hash, list 29 | assert_instance_of Array, list[:images] 30 | end 31 | 32 | def test_delete 33 | res_up = @gyazo.upload imagefile: @imagefile 34 | res_del = @gyazo.delete image_id: res_up[:image_id] 35 | assert_equal res_del[:image_id], res_up[:image_id] 36 | end 37 | 38 | def test_image 39 | res_up = @gyazo.upload imagefile: @imagefile 40 | res = @gyazo.image image_id: res_up[:image_id] 41 | assert_equal res[:image_id], res_up[:image_id] 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Gyazo 2 | ===== 3 | [Gyazo API](https://gyazo.com/api/docs) wrapper for Ruby 4 | 5 | - http://github.com/gyazo/gyazo-ruby 6 | - https://rubygems.org/gems/gyazo 7 | 8 | 9 | # Install 10 | 11 | % gem install gyazo 12 | 13 | # Usage 14 | 15 | Register new application and get [ACCESS TOKEN](https://gyazo.com/oauth/applications), then 16 | 17 | ## Upload 18 | 19 | ```ruby 20 | require 'gyazo' 21 | gyazo = Gyazo::Client.new access_token: 'your-access-token' 22 | res = gyazo.upload imagefile: 'my_image.png' 23 | puts res #=> {:type=>"png", :thumb_url=>"https://thumb.gyazo.com/thumb/...", :created_at=>"2019-05-03T11:57:35+0000", :image_id=>"...", :permalink_url=>"https://gyazo.com/...", :url=>"https://i.gyazo.com/....png"} 24 | ``` 25 | 26 | ### passing filename 27 | if you give io for `imagefile:`, you need `filename:`. 28 | 29 | ```ruby 30 | gyazo.upload imagefile: File.open(image), filename: 'image.png' 31 | ``` 32 | 33 | ### Upload with metadata 34 | Following attributes can be set 35 | 36 | * created_at(default: `Time.now`) 37 | * referer_url(default: '') 38 | * title(default: '') 39 | * desc(default: '') 40 | * collection_id(default: '') 41 | 42 | ```ruby 43 | res = gyazo.upload imagefile: 'my_image.png', created_at: Time.now, referer_url: 'https://example.com/' 44 | ``` 45 | 46 | ## List 47 | 48 | ```ruby 49 | gyazo.list[:images].each do |image| 50 | puts image[:url] 51 | end 52 | ``` 53 | 54 | ## image detail 55 | 56 | ```ruby 57 | gyazo.image image_id: image_id 58 | ``` 59 | 60 | ## Delete 61 | 62 | ```ruby 63 | gyazo.delete image_id: image_id 64 | ``` 65 | 66 | 67 | # Test 68 | 69 | setup 70 | 71 | % gem install bundler 72 | % bundle install 73 | % export GYAZO_TOKEN=a1b2cdef3456 ## set your API Token 74 | 75 | run test 76 | 77 | % bundle exec rake test 78 | 79 | 80 | Contributing 81 | ------------ 82 | 1. Fork it 83 | 2. Create your feature branch (`git checkout -b my-new-feature`) 84 | 3. Commit your changes (`git commit -am 'Add some feature'`) 85 | 4. Push to the branch (`git push origin my-new-feature`) 86 | 5. Create new Pull Request 87 | -------------------------------------------------------------------------------- /lib/gyazo/client.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'json' 3 | require 'faraday' 4 | require 'mime/types' 5 | 6 | module Gyazo 7 | class Client 8 | UploadURI = 'https://upload.gyazo.com/api/upload' 9 | APIHost = 'https://api.gyazo.com' 10 | attr_accessor :access_token, :user_agent 11 | 12 | def initialize(access_token:, user_agent: nil) 13 | @access_token = access_token 14 | @user_agent = user_agent || "GyazoRubyGem/#{Gyazo::VERSION}" 15 | @conn = ::Faraday.new(url: APIHost) do |f| 16 | f.request :url_encoded 17 | f.adapter ::Faraday.default_adapter 18 | end 19 | end 20 | 21 | def upload(imagefile:, filename: nil, created_at: ::Time.now, referer_url: '', title: '', desc: '', collection_id: '') 22 | ensure_io_or_file_exists imagefile, filename 23 | 24 | conn = ::Faraday.new do |f| 25 | f.request :multipart 26 | f.request :url_encoded 27 | f.adapter ::Faraday.default_adapter 28 | end 29 | type = ::MIME::Types.type_for(filename || imagefile)[0].to_s 30 | res = conn.post UploadURI do |req| 31 | req.body = { 32 | access_token: @access_token, 33 | imagedata: ::Faraday::UploadIO.new(imagefile, type, filename), 34 | created_at: created_at.to_i, 35 | referer_url: referer_url.to_s, 36 | title: title.to_s, 37 | desc: desc.to_s, 38 | collection_id: collection_id.to_s, 39 | } 40 | req.headers['User-Agent'] = @user_agent 41 | end 42 | raise Gyazo::Error, res.body unless res.status == 200 43 | return ::JSON.parse res.body, symbolize_names: true 44 | end 45 | 46 | def list(page: 1, per_page: 20) 47 | path = '/api/images' 48 | res = @conn.get path do |req| 49 | req.params[:access_token] = @access_token 50 | req.params[:page] = page 51 | req.params[:per_page] = per_page 52 | req.headers['User-Agent'] = @user_agent 53 | end 54 | raise Gyazo::Error, res.body unless res.status == 200 55 | json = ::JSON.parse res.body, symbolize_names: true 56 | { 57 | total_count: res.headers['X-Total-Count'], 58 | current_page: res.headers['X-Current-Page'], 59 | per_page: res.headers['X-Per-Page'], 60 | user_type: res.headers['X-User-Type'], 61 | images: json 62 | } 63 | end 64 | 65 | def image(image_id:) 66 | path = "/api/images/#{image_id}" 67 | send_get_without_param(path:) 68 | end 69 | 70 | def delete(image_id:) 71 | path = "/api/images/#{image_id}" 72 | res = @conn.delete path do |req| 73 | req.params[:access_token] = @access_token 74 | req.headers['User-Agent'] = @user_agent 75 | end 76 | raise Gyazo::Error, res.body unless res.status == 200 77 | return ::JSON.parse res.body, symbolize_names: true 78 | end 79 | 80 | def user_info 81 | path = '/api/users/me' 82 | send_get_without_param(path:) 83 | end 84 | 85 | private 86 | 87 | def ensure_io_or_file_exists(file, name) 88 | if file.respond_to?(:read) && file.respond_to?(:rewind) 89 | if name.nil? 90 | raise ArgumentError, "need filename: when file is io" 91 | end 92 | return 93 | end 94 | return if ::File.file? file 95 | raise ArgumentError, "cannot find file #{file}" 96 | end 97 | 98 | def send_get_without_param(path:) 99 | res = @conn.get path do |req| 100 | req.params[:access_token] = @access_token 101 | req.headers['User-Agent'] = @user_agent 102 | end 103 | raise Gyazo::Error, res.body unless res.status == 200 104 | return ::JSON.parse res.body, symbolize_names: true 105 | end 106 | end 107 | end 108 | --------------------------------------------------------------------------------