Class: Gyazo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gyazo/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/gyazo/client.rb', line 8

def initialize(access_token = nil)
  @access_token = access_token
  @user_agent = "GyazoRubyGem/#{Gyazo::VERSION}"
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/gyazo/client.rb', line 6

def host
  @host
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/gyazo/client.rb', line 6

def id
  @id
end

#user_agentObject

Returns the value of attribute user_agent.



6
7
8
# File 'lib/gyazo/client.rb', line 6

def user_agent
  @user_agent
end

#user_idObject

Returns the value of attribute user_id.



6
7
8
# File 'lib/gyazo/client.rb', line 6

def user_id
  @user_id
end

Instance Method Details

#delete(image_id) ⇒ Object

Raises:



46
47
48
49
50
51
52
53
54
55
# File 'lib/gyazo/client.rb', line 46

def delete(image_id)
  url = "https://api.gyazo.com/api/images/#{image_id}"
  res = HTTParty.delete url, {
    :query => {
      :access_token => @access_token
    }
  }
  raise Gyazo::Error, res.body unless res.code == 200
  return JSON.parse res.body
end

#list(query = {}) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gyazo/client.rb', line 33

def list(query = {})
  url = "https://api.gyazo.com/api/images"
  query[:access_token] = @access_token
  res = HTTParty.get url, {
    :query => query,
    :header => {
      'User-Agent' => @user_agent
    }
  }
  raise Gyazo::Error, res.body unless res.code == 200
  return JSON.parse res.body
end

#upload(imagefile, params = {}) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gyazo/client.rb', line 13

def upload(imagefile,params={})
  url = "https://upload.gyazo.com/api/upload"
  time = params[:time] || params[:created_at] || Time.now
  res = HTTMultiParty.post url, {
    :query => {
      :access_token => @access_token,
      :imagedata => File.open(imagefile),
      :created_at => time.to_i,
      :referer_url => params[:referer_url] || params[:url] || '',
      :title =>  params[:title] || '',
      :desc =>  params[:desc] || ''
    },
    :header => {
      'User-Agent' => @user_agent
    }
  }
  raise Gyazo::Error, res.body unless res.code == 200
  return JSON.parse res.body
end