psychs / limechat

IRC Client for OSX

limechat / ruby / lib / pasternakclient.rb
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 1 # Created by Satoshi Nakagawa.
2 # You can redistribute it and/or modify it under the Ruby's license or the GPL2.
3
4 require 'cgi'
5
35193f90 » psychs 2008-11-22 started porting 6 class PasternakClient
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 7 attr_accessor :delegate
8
9 TIMEOUT = 10
10 REQUEST_URL = 'http://pasternak.superalloy.nl/pastes'
11
12 def start(content, nick, syntax='ruby')
13 cancel
4c8a75af » psychs 2008-12-25 porting 14 @buf = NSMutableData.new
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 15 @response = nil
16 params_hash = {
17 'paste[username]' => nick,
18 'paste[language]' => syntax,
19 'paste[code]' => content,
20 'wants_url_response' => 'true',
21 'patch_style' => 'monkeypatch',
22 }
23 body = params_hash.inject('') {|v,i| v << "#{i[0].to_s}=#{CGI.escape(i[1].to_s)}&"}.chop
24
25 url = NSURL.URLWithString(REQUEST_URL)
26 policy = 1 # NSURLRequestReloadIgnoringLocalCacheData
4c8a75af » psychs 2008-12-25 porting 27 req = NSMutableURLRequest.requestWithURL(url, cachePolicy:policy, timeoutInterval:TIMEOUT)
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 28 req.setHTTPMethod('POST')
4c8a75af » psychs 2008-12-25 porting 29 req.setHTTPBody(body.dataUsingEncoding(NSUTF8StringEncoding))
30 @conn = NSURLConnection.alloc.initWithRequest(req, delegate:self)
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 31 end
32
33 def cancel
34 if @conn
35 @conn.cancel
36 @conn = nil
37 end
38 end
39
4c8a75af » psychs 2008-12-25 porting 40 def connection(conn, didReceiveResponse:res)
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 41 return if @conn != conn
42 @response = res
43 end
44
45 def connectionDidFinishLoading(conn)
46 if @response
47 code = @response.statusCode
48 if code.to_s =~ /^20[01]$/
4c8a75af » psychs 2008-12-25 porting 49 @delegate.pastie_on_success(self, NSString.alloc.initWithData(@buf, encoding:NSUTF8StringEncoding))
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 50 else
51 @delegate.pastie_on_error(self, "#{code} #{@response.oc_class.localizedStringForStatusCode(code)}")
52 end
53 end
54 @conn = nil
55 end
56
4c8a75af » psychs 2008-12-25 porting 57 def connection(conn, didReceiveData:data)
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 58 return if @conn != conn
4c8a75af » psychs 2008-12-25 porting 59 @buf.appendData(data)
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 60 end
61
4c8a75af » psychs 2008-12-25 porting 62 def connection(conn, didFailWithError:err)
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 63 if @conn == conn
64 @delegate.pastie_on_error(self, "#{err.userInfo[:NSLocalizedDescription]}")
65 end
66 @conn = nil
67 end
68
4c8a75af » psychs 2008-12-25 porting 69 def connection(conn, willSendRequest:req, redirectResponse:res)
f60f7bc8 » psychs 2008-11-17 imported files from the mas... 70 return nil if @conn != conn
71 if res && res.statusCode == 302
72 @delegate.pastie_on_success(self, req.URL.to_s)
73 @conn = nil
74 nil
75 else
76 req
77 end
78 end
79 end