Class: Gyazo
- Inherits:
-
Object
- Object
- Gyazo
- Defined in:
- lib/gyazo.rb
Constant Summary collapse
- VERSION =
'0.4.2'
- DEFAULT_UPLOAD_OPTS =
{:time => nil, :raw => false}
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#userid ⇒ Object
Returns the value of attribute userid.
Instance Method Summary collapse
- #info(gyazoid) ⇒ Object
-
#initialize(app = '/Applications/Gyazo.app', userid = nil) ⇒ Gyazo
constructor
A new instance of Gyazo.
- #list(page, count) ⇒ Object
- #upload(imagefile, opts) ⇒ Object
Constructor Details
#initialize(app = '/Applications/Gyazo.app', userid = nil) ⇒ Gyazo
Returns a new instance of Gyazo.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/gyazo.rb', line 11 def initialize(app = '/Applications/Gyazo.app',userid=nil) @user = IO.popen("whoami", "r+").gets.chomp @userid = userid @program = app @idfile = "/Users/#{@user}/Library/Gyazo/id" @old_idfile = File.dirname(@program) + "/gyazo.app/Contents/Resources/id" @id = '' if File.exist?(@idfile) then @id = File.read(@idfile).chomp elsif File.exist?(@old_idfile) then @id = File.read(@old_idfile).chomp end @host = 'gyazo.com' end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
26 27 28 |
# File 'lib/gyazo.rb', line 26 def id @id end |
#userid ⇒ Object
Returns the value of attribute userid.
27 28 29 |
# File 'lib/gyazo.rb', line 27 def userid @userid end |
Instance Method Details
#info(gyazoid) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gyazo.rb', line 29 def info(gyazoid) gyazoid =~ /[0-9a-f]{32}/ gyazoid = $& cgi = "/api/image/get?image_id=#{gyazoid}" header = {} res = Net::HTTP.start(@host,80){|http| http.get(cgi,header) } JSON.parse(res.read_body) end |
#list(page, count) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gyazo.rb', line 40 def list(page,count) # 旧API # cgi = "/api/image/list?userkey=#{@id}&page=#{page}&count=#{count}" # 新API: @useridが指定されている場合はマシン共通のUserIDを利用する cgi = (@userid ? "/api/image/list?user_id=#{@userid}&page=#{page}&count=#{count}" : "/api/image/list?device_id=#{@id}&page=#{page}&count=#{count}") header = {} res = Net::HTTP.start(@host,80){|http| http.get(cgi,header) } JSON.parse(res.read_body)['images'] end |
#upload(imagefile, opts) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/gyazo.rb', line 55 def upload(imagefile, opts) DEFAULT_UPLOAD_OPTS.each do |k,v| opts[k] = v unless opts.include? k end if opts[:raw] then imagedata = File.read(imagefile) else tmpfile = "/tmp/image_upload#{$$}.png" if imagefile && File.exist?(imagefile) then system "sips -s format png \"#{imagefile}\" --out \"#{tmpfile}\" > /dev/null" end imagedata = File.read(tmpfile) File.delete(tmpfile) end boundary = '----BOUNDARYBOUNDARY----' @cgi = '/upload.cgi' @ua = 'Gyazo/1.0' data = <<EOF --#{boundary}\r content-disposition: form-data; name="id"\r \r #{@id}\r --#{boundary}\r content-disposition: form-data; name="imagedata"; filename="gyazo.com"\r \r #{imagedata}\r --#{boundary}--\r EOF if opts[:time].kind_of? Time then @timestr = opts[:time].gmtime.strftime("%Y-%m-%d %H:%M:%S") s = <<EOF --#{boundary}\r content-disposition: form-data; name="date"\r \r #{@timestr}\r EOF data = s + data end header ={ 'Content-Length' => data.length.to_s, 'Content-type' => "multipart/form-data; boundary=#{boundary}", 'User-Agent' => @ua } res = Net::HTTP.start(@host,80){|http| http.post(@cgi,data,header) } @url = res.read_body # save id newid = res['X-Gyazo-Id'] if newid and newid != "" then if !File.exist?(File.dirname(@idfile)) then Dir.mkdir(File.dirname(@idfile)) end if File.exist?(@idfile) then File.rename(@idfile, @idfile+Time.new.strftime("_%Y%m%d%H%M%S.bak")) end File.open(@idfile,"w").print(newid) if File.exist?(@old_idfile) then File.delete(@old_idfile) end end @url end |