jugyo-twitpic 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
File without changes
data/README.rdoc ADDED
@@ -0,0 +1,54 @@
1
+ = TwitPic
2
+
3
+ == DESCRIPTION:
4
+
5
+ Library for TwitPic API.
6
+
7
+ == INSTALL:
8
+
9
+  gem install twitpic
10
+
11
+ == SYNOPSIS:
12
+
13
+ Use ad command line tool:
14
+
15
+ twitpic Picture.png
16
+
17
+ Use as library:
18
+
19
+ twitpic = TwitPic.new(username, password)
20
+ twitpic.upload('Picture.png', 'My Picture')
21
+
22
+ == REQUIREMENTS:
23
+
24
+ * mime-types
25
+ * highline
26
+
27
+ == TODO:
28
+
29
+ * Error handling
30
+
31
+ == LICENSE:
32
+
33
+ (The MIT License)
34
+
35
+ Copyright (c) 2008-2009 jugyo
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining
38
+ a copy of this software and associated documentation files (the
39
+ 'Software'), to deal in the Software without restriction, including
40
+ without limitation the rights to use, copy, modify, merge, publish,
41
+ distribute, sublicense, and/or sell copies of the Software, and to
42
+ permit persons to whom the Software is furnished to do so, subject to
43
+ the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be
46
+ included in all copies or substantial portions of the Software.
47
+
48
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
49
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
50
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
51
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
52
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
53
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
54
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,69 @@
1
+ $:.unshift File.dirname(__FILE__) + '/lib'
2
+ require 'twitpic'
3
+ require 'spec/rake/spectask'
4
+ require 'rake/clean'
5
+ require 'rake/gempackagetask'
6
+ require 'rake/rdoctask'
7
+
8
+ name = 'twitpic'
9
+ version = TwitPic::VERSION
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("{bin,lib,spec}/**/*")
17
+ s.add_dependency("mime-types", ">= 1.15")
18
+ s.add_dependency("highline", ">= 1.5.1")
19
+ s.add_dependency("rubytter", ">= 0.8.0")
20
+ s.executables = ["twitpic"]
21
+ s.authors = %w(jugyo)
22
+ s.email = 'jugyo.org@gmail.com'
23
+ s.homepage = 'http://github.com/jugyo/twitpic'
24
+ s.rubyforge_project = 'twitpic'
25
+ s.has_rdoc = true
26
+ s.rdoc_options = ["--main", "README.rdoc", "--exclude", "spec"]
27
+ s.extra_rdoc_files = ["README.rdoc", "History.txt"]
28
+ end
29
+
30
+ Rake::GemPackageTask.new(spec) do |p|
31
+ p.need_tar = true
32
+ end
33
+
34
+ task :gemspec do
35
+ filename = "#{name}.gemspec"
36
+ open(filename, 'w') do |f|
37
+ f.write spec.to_ruby
38
+ end
39
+ puts <<-EOS
40
+ Successfully generated gemspec
41
+ Name: #{name}
42
+ Version: #{version}
43
+ File: #{filename}
44
+ EOS
45
+ end
46
+
47
+ task :install => [ :package ] do
48
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
49
+ end
50
+
51
+ task :uninstall => [ :clean ] do
52
+ sh %{sudo gem uninstall #{name}}
53
+ end
54
+
55
+ desc 'run all specs'
56
+ Spec::Rake::SpecTask.new do |t|
57
+ t.spec_files = FileList['spec/**/*_spec.rb']
58
+ t.spec_opts = ['-c']
59
+ end
60
+
61
+ Rake::RDocTask.new do |t|
62
+ t.rdoc_dir = 'rdoc'
63
+ t.title = "rest-client, fetch RESTful resources effortlessly"
64
+ t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
65
+ t.options << '--charset' << 'utf-8'
66
+ t.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
67
+ end
68
+
69
+ CLEAN.include [ 'pkg', 'rdoc' ]
data/bin/twitpic ADDED
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'twitpic'
5
+ require 'rubytter'
6
+ require 'optparse'
7
+ require "highline"
8
+
9
+ username = nil
10
+ password = nil
11
+ message = nil
12
+ file = nil
13
+ screencapture = nil
14
+
15
+ OptionParser.new do |o|
16
+ o.version = TwitPic::VERSION
17
+ o.banner = "Usage: #{o.program_name} [-u USERNAME -p PASSWORD -m MESSAGE] [FILE | -s]"
18
+ o.on('-u', '--username=USERNAME') {|v| username = v}
19
+ o.on('-p', '--password=PASSWORD') {|v| password = v}
20
+ o.on('-m', '--message=MESSAGE') {|v| message = v}
21
+ o.on('-s', '--screencapture') {|v| screencapture = true}
22
+ o.on('-c', '--conf=CONFNAME') { |v|
23
+ begin
24
+ conf = Pit.get v
25
+ username = conf["username"]
26
+ password = conf["password"]
27
+ rescue
28
+ puts 'You don\'t have pit module. Please install pit.'
29
+ puts 'for example: gem install pit'
30
+ end
31
+ }
32
+ o.permute!(ARGV)
33
+ file = ARGV.first
34
+ if file.nil? && screencapture.nil?
35
+ puts o.help
36
+ exit!
37
+ end
38
+ end
39
+
40
+ if file.nil? && screencapture
41
+ file = '/tmp/twitpic_screencapture.png'
42
+ File.delete(file) if File.exists?(file)
43
+ puts 'Please capture screen!'
44
+ system 'screencapture', '-i', '-f', file
45
+ unless File.exists?(file)
46
+ puts 'Aboat!'
47
+ exit!
48
+ end
49
+ end
50
+
51
+ # try pit
52
+ if username.nil? && password.nil?
53
+ begin
54
+ require 'pit'
55
+ puts 'Load twitter config from pit...'
56
+ config = Pit.get('twitter')
57
+ username = config['username']
58
+ password = config['password']
59
+ rescue LoadError
60
+ end
61
+ end
62
+
63
+ # try highline
64
+ highline = HighLine.new
65
+ username = highline.ask('USERNAME: ') unless username
66
+ password = highline.ask('PASSWORD: ') {|q| q.echo = '*'} unless password
67
+ message = highline.ask('MESSAGE: ') unless message
68
+
69
+ puts 'Uploading...'
70
+ url = TwitPic.new(username, password).upload(file)[:mediaurl]
71
+ puts " => \"#{url}\""
72
+
73
+ message.strip!
74
+ post_message = "#{message} #{url}"
75
+ puts "Post to Twitter..."
76
+ Rubytter.new(username, password).update(post_message) if message
77
+ puts " => \"#{post_message}\""
data/lib/twitpic.rb ADDED
@@ -0,0 +1,78 @@
1
+ require "net/https"
2
+ require "uri"
3
+ require 'mime/types'
4
+ require "rexml/document"
5
+
6
+ class TwitPic
7
+ VERSION = '0.3.0'
8
+
9
+ class APIError < StandardError; end
10
+
11
+ def initialize(username, password)
12
+ @username = username
13
+ @password = password
14
+ end
15
+
16
+ def upload(file_path, message = nil, boundary = "----------------------------TwitPic#{rand(1000000000000)}")
17
+ parts = {
18
+ :username => @username,
19
+ :password => @password
20
+ }
21
+ parts[:message] = message if message && !message.empty?
22
+
23
+ body = create_body(parts, file_path, boundary)
24
+
25
+ url =
26
+ if parts[:message]
27
+ 'http://twitpic.com/api/uploadAndPost'
28
+ else
29
+ 'http://twitpic.com/api/upload'
30
+ end
31
+
32
+ post(url, body, boundary)
33
+ end
34
+
35
+ def post(url, body, boundary)
36
+ uri = URI.parse(url)
37
+ http = Net::HTTP.new(uri.host, uri.port)
38
+ xml = http.start do |http|
39
+ headers = {"Content-Type" => "multipart/form-data; boundary=" + boundary}
40
+ response = http.post(uri.path, body, headers)
41
+ response.body
42
+ end
43
+ parse_response(xml)
44
+ end
45
+
46
+ def parse_response(xml)
47
+ doc = REXML::Document.new(xml)
48
+ error_element = REXML::XPath.match(doc, "/rsp/err").first
49
+ if error_element
50
+ raise APIError, error_element.attribute('code').value + ': ' + error_element.attribute('code').value
51
+ else
52
+ result = {}
53
+ [:statusid, :userid, :mediaid, :mediaurl].each do |key|
54
+ result[key] = REXML::XPath.match(doc, "/rsp/#{key}").first.text rescue nil
55
+ end
56
+ result
57
+ end
58
+ end
59
+
60
+ def create_body(parts, file_path, boundary)
61
+ parts[:media] = open(file_path, 'rb').read
62
+ body = ''
63
+ [:media, :username, :password, :message].each do |key|
64
+ value = parts[key]
65
+ next unless value
66
+ body << "--#{boundary}\r\n"
67
+ if key == :media
68
+ body << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{File.basename(file_path)}\"\r\n"
69
+ body << "Content-Type: #{MIME::Types.type_for(file_path).first.content_type}\r\n"
70
+ else
71
+ body << "Content-Disposition: form-data; name=\"#{key}\"\r\n"
72
+ end
73
+ body << "\r\n"
74
+ body << "#{value}\r\n"
75
+ end
76
+ body << "--#{boundary}--\r\n"
77
+ end
78
+ end
data/spec/test.txt ADDED
@@ -0,0 +1,2 @@
1
+ test
2
+ test
@@ -0,0 +1,88 @@
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 'response data is valid' do
36
+ before(:each) do
37
+ @xml = <<-XML
38
+ <?xml version="1.0" encoding="UTF-8"?>
39
+ <rsp status="ok">
40
+ <statusid>1111</statusid>
41
+ <userid>11111</userid>
42
+ <mediaid>abc123</mediaid>
43
+ <mediaurl>http://twitpic.com/abc123</mediaurl>
44
+ </rsp>
45
+ XML
46
+ end
47
+
48
+ it 'should parse xml' do
49
+ result = @twitpic.parse_response(@xml)
50
+ result.should == {:statusid=>"1111", :userid=>"11111", :mediaid=>"abc123", :mediaurl=>"http://twitpic.com/abc123"}
51
+ end
52
+ end
53
+
54
+ describe 'response data is invalid' do
55
+ before(:each) do
56
+ @xml = <<-XML
57
+ <?xml version="1.0" encoding="UTF-8"?>
58
+ <rsp stat="fail">
59
+ <err code="1001" msg="Invalid twitter username or password" />
60
+ </rsp>
61
+ XML
62
+ end
63
+
64
+ it 'should raise error' do
65
+ lambda {@twitpic.parse_response(@xml)}.should raise_error(TwitPic::APIError)
66
+ end
67
+ end
68
+
69
+ describe 'with message' do
70
+ it 'should call method "post"' do
71
+ file_path = File.dirname(__FILE__) + '/test.txt'
72
+ @twitpic.should_receive(:post).once do |url, body, boundary|
73
+ url.should == 'http://twitpic.com/api/uploadAndPost'
74
+ end
75
+ @twitpic.upload(file_path, 'test')
76
+ end
77
+ end
78
+
79
+ describe 'without message' do
80
+ it 'should call method "post"' do
81
+ file_path = File.dirname(__FILE__) + '/test.txt'
82
+ @twitpic.should_receive(:post).once do |url, body, boundary|
83
+ url.should == 'http://twitpic.com/api/upload'
84
+ end
85
+ @twitpic.upload(file_path)
86
+ end
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jugyo-twitpic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - jugyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-06 00:00:00 -07:00
13
+ default_executable: twitpic
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
+ - !ruby/object:Gem::Dependency
26
+ name: highline
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.1
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rubytter
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.8.0
44
+ version:
45
+ description: Library for TwitPic API.
46
+ email: jugyo.org@gmail.com
47
+ executables:
48
+ - twitpic
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - README.rdoc
53
+ - History.txt
54
+ files:
55
+ - Rakefile
56
+ - README.rdoc
57
+ - History.txt
58
+ - bin/twitpic
59
+ - lib/twitpic.rb
60
+ - spec/test.txt
61
+ - spec/twitpic_spec.rb
62
+ has_rdoc: false
63
+ homepage: http://github.com/jugyo/twitpic
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --main
67
+ - README.rdoc
68
+ - --exclude
69
+ - spec
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project: twitpic
87
+ rubygems_version: 1.2.0
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Library for TwitPic API.
91
+ test_files: []
92
+