twitpic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,47 @@
1
+ = TwitPic
2
+
3
+ == DESCRIPTION:
4
+
5
+ Library for TwitPic API.
6
+
7
+ == INSTALL:
8
+
9
+  gem install twitpic
10
+
11
+ == SYNOPSIS:
12
+
13
+ twitpic = TwitPic.new(username, password)
14
+ twitpic.upload('Picture.png', 'My Picture')
15
+
16
+ == REQUIREMENTS:
17
+
18
+ * mime-types
19
+
20
+ == TODO:
21
+
22
+ * Create twitpic command
23
+
24
+ == LICENSE:
25
+
26
+ (The MIT License)
27
+
28
+ Copyright (c) 2008-2009 The Termtter Development Team
29
+
30
+ Permission is hereby granted, free of charge, to any person obtaining
31
+ a copy of this software and associated documentation files (the
32
+ 'Software'), to deal in the Software without restriction, including
33
+ without limitation the rights to use, copy, modify, merge, publish,
34
+ distribute, sublicense, and/or sell copies of the Software, and to
35
+ permit persons to whom the Software is furnished to do so, subject to
36
+ the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be
39
+ included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
42
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
45
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
46
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
47
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ $:.unshift File.dirname(__FILE__) + '/lib'
2
+
3
+ require 'spec/rake/spectask'
4
+ require 'rake/clean'
5
+ require 'rake/gempackagetask'
6
+ require 'rake/rdoctask'
7
+
8
+ name = 'twitpic'
9
+ version = '0.1.0'
10
+
11
+ spec = Gem::Specification.new do |s|
12
+ s.name = name
13
+ s.version = version
14
+ s.summary = "Library for TwitPic API."
15
+ s.description = "Library for TwitPic API."
16
+ s.files = %w(Rakefile README.rdoc History.txt) + Dir.glob("{lib,spec}/**/*")
17
+ s.add_dependency("mime-types", ">= 1.15")
18
+ s.authors = %w(jugyo)
19
+ s.email = 'jugyo.org@gmail.com'
20
+ s.homepage = 'http://github.com/jugyo/twitpic'
21
+ s.rubyforge_project = 'twitpic'
22
+ s.has_rdoc = true
23
+ s.rdoc_options = ["--main", "README.rdoc", "--exclude", "spec"]
24
+ s.extra_rdoc_files = ["README.rdoc", "History.txt"]
25
+ end
26
+
27
+ Rake::GemPackageTask.new(spec) do |p|
28
+ p.need_tar = true
29
+ end
30
+
31
+ task :gemspec do
32
+ filename = "#{name}.gemspec"
33
+ open(filename, 'w') do |f|
34
+ f.write spec.to_ruby
35
+ end
36
+ puts <<-EOS
37
+ Successfully generated gemspec
38
+ Name: #{name}
39
+ Version: #{version}
40
+ File: #{filename}
41
+ EOS
42
+ end
43
+
44
+ task :install => [ :package ] do
45
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
46
+ end
47
+
48
+ task :uninstall => [ :clean ] do
49
+ sh %{sudo gem uninstall #{name}}
50
+ end
51
+
52
+ desc 'run all specs'
53
+ Spec::Rake::SpecTask.new do |t|
54
+ t.spec_files = FileList['spec/**/*_spec.rb']
55
+ t.spec_opts = ['-c']
56
+ end
57
+
58
+ Rake::RDocTask.new do |t|
59
+ t.rdoc_dir = 'rdoc'
60
+ t.title = "rest-client, fetch RESTful resources effortlessly"
61
+ t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
62
+ t.options << '--charset' << 'utf-8'
63
+ t.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
64
+ end
65
+
66
+ CLEAN.include [ 'pkg', 'rdoc' ]
@@ -0,0 +1,59 @@
1
+ require "net/https"
2
+ require "uri"
3
+ require 'mime/types'
4
+
5
+ class TwitPic
6
+ def initialize(username, password)
7
+ @username = username
8
+ @password = password
9
+ end
10
+
11
+ def upload(file_path, message = nil, boundary = "----------------------------TwitPic#{rand(1000000000000)}")
12
+ parts = {
13
+ :username => @username,
14
+ :password => @password
15
+ }
16
+ parts[:message] = message if message
17
+
18
+ body = create_body(parts, file_path, boundary)
19
+
20
+ url =
21
+ if message
22
+ 'http://twitpic.com/api/uploadAndPost'
23
+ else
24
+ 'http://twitpic.com/api/upload'
25
+ end
26
+
27
+ post(url, body, boundary)
28
+ end
29
+
30
+ def post(url, body, boundary)
31
+ uri = URI.parse(url)
32
+ http = Net::HTTP.new(uri.host, uri.port)
33
+ xml = http.start do |http|
34
+ headers = {"Content-Type" => "multipart/form-data; boundary=" + boundary}
35
+ response = http.post(uri.path, body, headers)
36
+ response.body
37
+ end
38
+ xml
39
+ end
40
+
41
+ def create_body(parts, file_path, boundary)
42
+ parts[:media] = open(file_path, 'rb').read
43
+ body = ''
44
+ [:media, :username, :password, :message].each do |key|
45
+ value = parts[key]
46
+ next unless value
47
+ body << "--#{boundary}\r\n"
48
+ if key == :media
49
+ body << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{File.basename(file_path)}\"\r\n"
50
+ body << "Content-Type: #{MIME::Types.type_for(file_path).first.content_type}\r\n"
51
+ else
52
+ body << "Content-Disposition: form-data; name=\"#{key}\"\r\n"
53
+ end
54
+ body << "\r\n"
55
+ body << "#{value}\r\n"
56
+ end
57
+ body << "--#{boundary}--\r\n"
58
+ end
59
+ end
@@ -0,0 +1,2 @@
1
+ test
2
+ test
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__) + '/../lib/twitpic'
3
+
4
+ describe TwitPic do
5
+ before(:each) do
6
+ @twitpic = TwitPic.new('test', 'password')
7
+ end
8
+
9
+ it 'should create body for upload' do
10
+ file_path =File.dirname(__FILE__) + '/test.txt'
11
+ body = @twitpic.create_body(
12
+ {:username => 'test', :password => 'password', :media => file_path},
13
+ file_path,
14
+ 'boundary'
15
+ )
16
+ body.should == <<-EOS
17
+ --boundary\r
18
+ Content-Disposition: form-data; name="media"; filename="test.txt"\r
19
+ Content-Type: text/plain\r
20
+ \r
21
+ test
22
+ test\r
23
+ --boundary\r
24
+ Content-Disposition: form-data; name="username"\r
25
+ \r
26
+ test\r
27
+ --boundary\r
28
+ Content-Disposition: form-data; name="password"\r
29
+ \r
30
+ password\r
31
+ --boundary--\r
32
+ EOS
33
+ end
34
+
35
+ describe 'with message' do
36
+ it 'should call method "post"' do
37
+ file_path = File.dirname(__FILE__) + '/test.txt'
38
+ @twitpic.should_receive(:post).once do |url, body, boundary|
39
+ url.should == 'http://twitpic.com/api/uploadAndPost'
40
+ end
41
+ @twitpic.upload(file_path, 'test')
42
+ end
43
+ end
44
+
45
+ describe 'without message' do
46
+ it 'should call method "post"' do
47
+ file_path = File.dirname(__FILE__) + '/test.txt'
48
+ @twitpic.should_receive(:post).once do |url, body, boundary|
49
+ url.should == 'http://twitpic.com/api/upload'
50
+ end
51
+ @twitpic.upload(file_path)
52
+ end
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitpic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jugyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-19 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mime-types
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "1.15"
24
+ version:
25
+ description: Library for TwitPic API.
26
+ email: jugyo.org@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ - History.txt
34
+ files:
35
+ - Rakefile
36
+ - README.rdoc
37
+ - History.txt
38
+ - lib/twitpic.rb
39
+ - spec/test.txt
40
+ - spec/twitpic_spec.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/jugyo/twitpic
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --main
46
+ - README.rdoc
47
+ - --exclude
48
+ - spec
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: twitpic
66
+ rubygems_version: 1.3.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Library for TwitPic API.
70
+ test_files: []
71
+