gyazo 2.1.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 73172604e611a854e2191a013b9e4e3367a72ec7
4
- data.tar.gz: 055ea0cca4ad119bd3c65163c39179f5f200ef93
2
+ SHA256:
3
+ metadata.gz: 6d5225cea423961aa57f0a32689b29b38c217ba80f0ca61b751d575ec267680d
4
+ data.tar.gz: c4c3357d3758f5b3ead2b7512cd8eb7fcc413c3f7962b6462aff3910ed44bf73
5
5
  SHA512:
6
- metadata.gz: a6d592e7a2155d0421a5e1279a4caa85f1764b456702ef8f8d943f4d46de0e45e1aa5708951a26df747cfe97f1912222ec91675078d01c7c83b86a72bddd1675
7
- data.tar.gz: c7cdc51eca52d3ec370cc8cca78b8c6c8e59c1228a3ea5d07281ffa573e4ddc8562a78b896195c0a6ff8b6182efdfbba5440c00144c942ec9d8e1c439e5b411b
6
+ metadata.gz: 077ea80315430b3047383db3b836fbab37442895370387d6eb9ddaf28acdb75cfca5600d74c72ab3483d813da72b83cb5d95f208c152ed9f7c21af66e7c4d266
7
+ data.tar.gz: c388377a4eea985bdabcab0b652d3f6a042d2bccc2193258c3a65dec51bb663ef1a8d7976f6d444d41fd246ac46ada28ec2bd7fe7ed6bab126bd37b29a467596
@@ -1,5 +1,8 @@
1
+ sudo: false
1
2
  language: ruby
2
3
  rvm:
3
- - 2.1.2
4
+ - 2.2.3
4
5
  env:
5
6
  secure: SKTPKUXfgOehDNdgdMxKykef1m7XswYKA72qANyqHQ3/P/9c7RcRfTSVD86wj4riT8BX5YTbNhKKQFa79HozioezF7tT5A6xF5qiJOt1sjjtMfhc0d3zqN3RVJNRpx+1+hgNpv9OQ6RrTdw3At1TjzvgdMAiJI4X8lqZ904Gu60=
7
+ install: bundle install
8
+ script: bundle exec rake test
data/README.md CHANGED
@@ -2,59 +2,69 @@ Gyazo
2
2
  =====
3
3
  [Gyazo API](https://gyazo.com/api/docs) wrapper for Ruby
4
4
 
5
- [![Build Status](https://travis-ci.org/masui/gyazo-ruby.svg?branch=master)](https://travis-ci.org/masui/gyazo-ruby)
6
-
7
- - http://github.com/masui/gyazo-ruby
5
+ - http://github.com/gyazo/gyazo-ruby
8
6
  - https://rubygems.org/gems/gyazo
9
7
 
10
8
 
11
9
  # Install
12
10
 
13
-
14
11
  % gem install gyazo
15
12
 
16
-
17
13
  # Usage
18
14
 
19
15
  Register new application and get [ACCESS TOKEN](https://gyazo.com/oauth/applications), then
20
16
 
21
- ### Upload
17
+ ## Upload
22
18
 
23
19
  ```ruby
24
20
  require 'gyazo'
25
-
26
- gyazo = Gyazo::Client.new 'your-access-token'
27
- res = gyazo.upload 'my_image.png'
28
- puts res['permalink_url'] # => "http://gyazo.com/a1b2cdef345"
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"}
29
24
  ```
30
- #### Upload with metadata
25
+
26
+ ### passing filename
27
+ if you give io for `imagefile:`, you need `filename:`.
31
28
 
32
29
  ```ruby
33
- require 'gyazo'
30
+ gyazo.upload imagefile: File.open(image), filename: 'image.png'
31
+ ```
32
+
33
+ ### Upload with metadata
34
+ Following attributes can be set
34
35
 
35
- gyazo = Gyazo::Client.new 'your-access-token'
36
- res = gyazo.upload 'my_image.png',
37
- { :time => Time.now, :url => 'http://example.com/' }
38
- puts res['permalink_url']
36
+ * created_at(default: `Time.now`)
37
+ * referer_url(default: '')
38
+ * title(default: '')
39
+ * desc(default: '')
40
+
41
+
42
+ ```ruby
43
+ res = gyazo.upload imagefile: 'my_image.png', created_at: Time.now, referer_url: 'https://example.com/'
39
44
  ```
40
45
 
41
- ### List
46
+ ## List
42
47
 
43
48
  ```ruby
44
- gyazo.list.each do |image|
45
- puts image['url']
49
+ gyazo.list[:images].each do |image|
50
+ puts image[:url]
46
51
  end
47
52
  ```
48
53
 
49
- ### Delete
54
+ ## image detail
55
+
56
+ ```ruby
57
+ gyazo.image image_id: image_id
58
+ ```
59
+
60
+ ## Delete
50
61
 
51
62
  ```ruby
52
- gyazo.delete image_id
63
+ gyazo.delete image_id: image_id
53
64
  ```
54
65
 
55
66
 
56
- Test
57
- ----
67
+ # Test
58
68
 
59
69
  setup
60
70
 
@@ -6,11 +6,11 @@ require 'gyazo/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "gyazo"
8
8
  spec.version = Gyazo::VERSION
9
- spec.authors = ["Toshiyuki Masui", "Sho Hashimoto"]
9
+ spec.authors = ["Toshiyuki Masui", "Sho Hashimoto", "Nana Kugayama"]
10
10
  spec.email = ["masui@pitecan.com"]
11
11
  spec.description = %q{Gyazo.com API Wrapper}
12
12
  spec.summary = spec.description
13
- spec.homepage = "http://github.com/masui/gyazo-ruby"
13
+ spec.homepage = "http://github.com/gyazo/gyazo-ruby"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/).reject{|i| i=="Gemfile.lock" }
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "minitest"
24
24
 
25
25
  spec.add_dependency "json"
26
- spec.add_dependency "httparty"
27
- spec.add_dependency "httmultiparty"
26
+ spec.add_dependency "faraday"
27
+ spec.add_dependency "multipart-post"
28
+ spec.add_dependency "mime-types"
28
29
  end
@@ -1,8 +1,3 @@
1
1
  require 'gyazo/version'
2
2
  require 'gyazo/error'
3
3
  require 'gyazo/client'
4
-
5
- require 'httparty'
6
- require 'httmultiparty'
7
- require 'json'
8
- require 'kconv'
@@ -1,57 +1,97 @@
1
1
  # coding: utf-8
2
- module Gyazo
2
+ require 'json'
3
+ require 'faraday'
4
+ require 'mime/types'
3
5
 
6
+ module Gyazo
4
7
  class Client
8
+ UploadURI = 'https://upload.gyazo.com/api/upload'
9
+ APIHost = 'https://api.gyazo.com'
10
+ attr_accessor :access_token, :user_agent
5
11
 
6
- attr_accessor :id, :user_id, :user_agent, :host
7
-
8
- def initialize(access_token = nil)
12
+ def initialize(access_token:, user_agent: nil)
9
13
  @access_token = access_token
10
- @user_agent = "GyazoRubyGem/#{Gyazo::VERSION}"
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
11
19
  end
12
-
13
- def upload(imagefile,params={})
14
- url = "https://upload.gyazo.com/api/upload"
15
- time = params[:time] || params[:created_at] || Time.now
16
- res = HTTMultiParty.post url, {
17
- :query => {
18
- :access_token => @access_token,
19
- :imagedata => File.open(imagefile),
20
- :created_at => time.to_i,
21
- :referer_url => params[:referer_url] || params[:url] || '',
22
- :title => params[:title] || '',
23
- :desc => params[:desc] || ''
24
- },
25
- :header => {
26
- 'User-Agent' => @user_agent
20
+
21
+ def upload(imagefile:, filename: nil, created_at: ::Time.now, referer_url: '', title: '', desc: '')
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,
27
38
  }
28
- }
29
- raise Gyazo::Error, res.body unless res.code == 200
30
- return JSON.parse res.body
39
+ req.headers['User-Agent'] = @user_agent
40
+ end
41
+ raise Gyazo::Error, res.body unless res.status == 200
42
+ return ::JSON.parse res.body, symbolize_names: true
31
43
  end
32
44
 
33
- def list(query = {})
34
- url = "https://api.gyazo.com/api/images"
35
- query[:access_token] = @access_token
36
- res = HTTParty.get url, {
37
- :query => query,
38
- :header => {
39
- 'User-Agent' => @user_agent
40
- }
45
+ def list(page: 1, per_page: 20)
46
+ path = '/api/images'
47
+ res = @conn.get path do |req|
48
+ req.params[:access_token] = @access_token
49
+ req.params[:page] = page
50
+ req.params[:per_page] = per_page
51
+ req.headers['User-Agent'] = @user_agent
52
+ end
53
+ raise Gyazo::Error, res.body unless res.status == 200
54
+ json = ::JSON.parse res.body, symbolize_names: true
55
+ {
56
+ total_count: res.headers['X-Total-Count'],
57
+ current_page: res.headers['X-Current-Page'],
58
+ per_page: res.headers['X-Per-Page'],
59
+ user_type: res.headers['X-User-Type'],
60
+ images: json
41
61
  }
42
- raise Gyazo::Error, res.body unless res.code == 200
43
- return JSON.parse res.body
44
62
  end
45
63
 
46
- def delete(image_id)
47
- url = "https://api.gyazo.com/api/images/#{image_id}"
48
- res = HTTParty.delete url, {
49
- :query => {
50
- :access_token => @access_token
51
- }
52
- }
53
- raise Gyazo::Error, res.body unless res.code == 200
54
- return JSON.parse res.body
64
+ def image(image_id:)
65
+ path = "/api/images/#{image_id}"
66
+ res = @conn.get path do |req|
67
+ req.params[:access_token] = @access_token
68
+ req.headers['User-Agent'] = @user_agent
69
+ end
70
+ raise Gyazo::Error, res.body unless res.status == 200
71
+ return ::JSON.parse res.body, symbolize_names: true
72
+ end
73
+
74
+ def delete(image_id:)
75
+ path = "/api/images/#{image_id}"
76
+ res = @conn.delete path do |req|
77
+ req.params[:access_token] = @access_token
78
+ req.headers['User-Agent'] = @user_agent
79
+ end
80
+ raise Gyazo::Error, res.body unless res.status == 200
81
+ return ::JSON.parse res.body, symbolize_names: true
82
+ end
83
+
84
+ private
85
+
86
+ def ensure_io_or_file_exists(file, name)
87
+ if file.respond_to?(:read) && file.respond_to?(:rewind)
88
+ if name.nil?
89
+ raise ArgumentError, "need filename: when file is io"
90
+ end
91
+ return
92
+ end
93
+ return if ::File.file? file
94
+ raise ArgumentError, "cannot find file #{file}"
55
95
  end
56
96
  end
57
97
  end
@@ -1,3 +1,3 @@
1
1
  module Gyazo
2
- VERSION = '2.1.2'
2
+ VERSION = '3.0.0'
3
3
  end
@@ -1,30 +1,38 @@
1
1
  require File.expand_path 'test_helper', File.dirname(__FILE__)
2
2
 
3
3
  class TestGyazo < MiniTest::Test
4
+ GYAZO_REGEXP = %r{^https://gyazo\.com/[a-z\d]{32}$}
4
5
 
5
6
  def setup
6
- @gyazo = Gyazo::Client.new ENV['GYAZO_TOKEN']
7
+ @gyazo = Gyazo::Client.new access_token: ENV['GYAZO_TOKEN']
7
8
  @imagefile = File.expand_path 'test.png', File.dirname(__FILE__)
8
9
  end
9
10
 
10
11
  def test_upload_filepath
11
- res = @gyazo.upload @imagefile
12
- assert res['permalink_url'].match /^https?:\/\/gyazo.com\/[a-z\d]{32}$/i
12
+ res = @gyazo.upload imagefile: @imagefile
13
+ assert res[:permalink_url].match GYAZO_REGEXP
13
14
  end
14
15
 
15
16
  def test_upload_file
16
- res = @gyazo.upload File.open(@imagefile)
17
- assert res['permalink_url'].match /^https?:\/\/gyazo.com\/[a-z\d]{32}$/i
17
+ res = @gyazo.upload imagefile: File.open(@imagefile), filename: 'test.png'
18
+ assert res[:permalink_url].match GYAZO_REGEXP
18
19
  end
19
20
 
20
21
  def test_list
21
- assert_equal @gyazo.list.class, Array
22
+ list = @gyazo.list
23
+ assert_instance_of Hash, list
24
+ assert_instance_of Array, list[:images]
22
25
  end
23
26
 
24
27
  def test_delete
25
- res_up = @gyazo.upload @imagefile
26
- res_del = @gyazo.delete res_up['image_id']
27
- assert_equal res_del['image_id'], res_up['image_id']
28
+ res_up = @gyazo.upload imagefile: @imagefile
29
+ res_del = @gyazo.delete image_id: res_up[:image_id]
30
+ assert_equal res_del[:image_id], res_up[:image_id]
28
31
  end
29
32
 
33
+ def test_image
34
+ res_up = @gyazo.upload imagefile: @imagefile
35
+ res = @gyazo.image image_id: res_up[:image_id]
36
+ assert_equal res[:image_id], res_up[:image_id]
37
+ end
30
38
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gyazo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshiyuki Masui
8
8
  - Sho Hashimoto
9
+ - Nana Kugayama
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2015-12-23 00:00:00.000000000 Z
13
+ date: 2019-05-07 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: bundler
@@ -68,7 +69,7 @@ dependencies:
68
69
  - !ruby/object:Gem::Version
69
70
  version: '0'
70
71
  - !ruby/object:Gem::Dependency
71
- name: httparty
72
+ name: faraday
72
73
  requirement: !ruby/object:Gem::Requirement
73
74
  requirements:
74
75
  - - ">="
@@ -82,7 +83,21 @@ dependencies:
82
83
  - !ruby/object:Gem::Version
83
84
  version: '0'
84
85
  - !ruby/object:Gem::Dependency
85
- name: httmultiparty
86
+ name: multipart-post
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :runtime
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: mime-types
86
101
  requirement: !ruby/object:Gem::Requirement
87
102
  requirements:
88
103
  - - ">="
@@ -119,7 +134,7 @@ files:
119
134
  - test/test.png
120
135
  - test/test_gyazo.rb
121
136
  - test/test_helper.rb
122
- homepage: http://github.com/masui/gyazo-ruby
137
+ homepage: http://github.com/gyazo/gyazo-ruby
123
138
  licenses:
124
139
  - MIT
125
140
  metadata: {}
@@ -138,8 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
153
  - !ruby/object:Gem::Version
139
154
  version: '0'
140
155
  requirements: []
141
- rubyforge_project:
142
- rubygems_version: 2.2.2
156
+ rubygems_version: 3.0.1
143
157
  signing_key:
144
158
  specification_version: 4
145
159
  summary: Gyazo.com API Wrapper