Class: TwitPic
- Inherits:
-
Object
- Object
- TwitPic
- Defined in:
- lib/twitpic.rb
Instance Method Summary collapse
- #create_body(parts, file_path, boundary) ⇒ Object
-
#initialize(username, password) ⇒ TwitPic
constructor
A new instance of TwitPic.
- #post(url, body, boundary) ⇒ Object
- #upload(file_path, message = nil, boundary = "----------------------------TwitPic#{rand(1000000000000)}") ⇒ Object
Constructor Details
#initialize(username, password) ⇒ TwitPic
Returns a new instance of TwitPic.
6 7 8 9 |
# File 'lib/twitpic.rb', line 6 def initialize(username, password) @username = username @password = password end |
Instance Method Details
#create_body(parts, file_path, boundary) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/twitpic.rb', line 41 def create_body(parts, file_path, boundary) parts[:media] = open(file_path, 'rb').read body = '' [:media, :username, :password, :message].each do |key| value = parts[key] next unless value body << "--#{boundary}\r\n" if key == :media body << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{File.basename(file_path)}\"\r\n" body << "Content-Type: #{MIME::Types.type_for(file_path).first.content_type}\r\n" else body << "Content-Disposition: form-data; name=\"#{key}\"\r\n" end body << "\r\n" body << "#{value}\r\n" end body << "--#{boundary}--\r\n" end |
#post(url, body, boundary) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/twitpic.rb', line 30 def post(url, body, boundary) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) xml = http.start do |http| headers = {"Content-Type" => "multipart/form-data; boundary=" + boundary} response = http.post(uri.path, body, headers) response.body end xml end |
#upload(file_path, message = nil, boundary = "----------------------------TwitPic#{rand(1000000000000)}") ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/twitpic.rb', line 11 def upload(file_path, = nil, boundary = "----------------------------TwitPic#{rand(1000000000000)}") parts = { :username => @username, :password => @password } parts[:message] = if body = create_body(parts, file_path, boundary) url = if 'http://twitpic.com/api/uploadAndPost' else 'http://twitpic.com/api/upload' end post(url, body, boundary) end |