Class: Gyazo::Client
- Inherits:
-
Object
- Object
- Gyazo::Client
- Defined in:
- lib/gyazo/client.rb
Constant Summary collapse
- UploadURI =
'https://upload.gyazo.com/api/upload'
- APIHost =
'https://api.gyazo.com'
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #delete(image_id:) ⇒ Object
- #image(image_id:) ⇒ Object
-
#initialize(access_token:, user_agent: nil) ⇒ Client
constructor
A new instance of Client.
- #list(page: 1, per_page: 20) ⇒ Object
- #upload(imagefile:, filename: nil, created_at: ::Time.now, referer_url: '', title: '', desc: '', collection_id: '') ⇒ Object
Constructor Details
#initialize(access_token:, user_agent: nil) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 |
# File 'lib/gyazo/client.rb', line 12 def initialize(access_token:, user_agent: nil) @access_token = access_token @user_agent = user_agent || "GyazoRubyGem/#{Gyazo::VERSION}" @conn = ::Faraday.new(url: APIHost) do |f| f.request :url_encoded f.adapter ::Faraday.default_adapter end end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
10 11 12 |
# File 'lib/gyazo/client.rb', line 10 def access_token @access_token end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
10 11 12 |
# File 'lib/gyazo/client.rb', line 10 def user_agent @user_agent end |
Instance Method Details
#delete(image_id:) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/gyazo/client.rb', line 75 def delete(image_id:) path = "/api/images/#{image_id}" res = @conn.delete path do |req| req.params[:access_token] = @access_token req.headers['User-Agent'] = @user_agent end raise Gyazo::Error, res.body unless res.status == 200 return ::JSON.parse res.body, symbolize_names: true end |
#image(image_id:) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/gyazo/client.rb', line 65 def image(image_id:) path = "/api/images/#{image_id}" res = @conn.get path do |req| req.params[:access_token] = @access_token req.headers['User-Agent'] = @user_agent end raise Gyazo::Error, res.body unless res.status == 200 return ::JSON.parse res.body, symbolize_names: true end |
#list(page: 1, per_page: 20) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/gyazo/client.rb', line 46 def list(page: 1, per_page: 20) path = '/api/images' res = @conn.get path do |req| req.params[:access_token] = @access_token req.params[:page] = page req.params[:per_page] = per_page req.headers['User-Agent'] = @user_agent end raise Gyazo::Error, res.body unless res.status == 200 json = ::JSON.parse res.body, symbolize_names: true { total_count: res.headers['X-Total-Count'], current_page: res.headers['X-Current-Page'], per_page: res.headers['X-Per-Page'], user_type: res.headers['X-User-Type'], images: json } end |
#upload(imagefile:, filename: nil, created_at: ::Time.now, referer_url: '', title: '', desc: '', collection_id: '') ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/gyazo/client.rb', line 21 def upload(imagefile:, filename: nil, created_at: ::Time.now, referer_url: '', title: '', desc: '', collection_id: '') ensure_io_or_file_exists imagefile, filename conn = ::Faraday.new do |f| f.request :multipart f.request :url_encoded f.adapter ::Faraday.default_adapter end type = ::MIME::Types.type_for(filename || imagefile)[0].to_s res = conn.post UploadURI do |req| req.body = { access_token: @access_token, imagedata: ::Faraday::UploadIO.new(imagefile, type, filename), created_at: created_at.to_i, referer_url: referer_url.to_s, title: title.to_s, desc: desc.to_s, collection_id: collection_id.to_s, } req.headers['User-Agent'] = @user_agent end raise Gyazo::Error, res.body unless res.status == 200 return ::JSON.parse res.body, symbolize_names: true end |