twitbookis 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -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
+ As command line tool:
14
+
15
+ twitpic Picture.png
16
+
17
+ 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.
@@ -0,0 +1,70 @@
1
+ $:.unshift File.dirname(__FILE__) + '/lib'
2
+ require 'rubygems'
3
+ require 'twitpic'
4
+ require 'spec/rake/spectask'
5
+ require 'rake/clean'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+
9
+ name = 'twitpic'
10
+ version = TwitPic::VERSION
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = name
14
+ s.version = version
15
+ s.summary = "Library for TwitPic API."
16
+ s.description = "Library for TwitPic API."
17
+ s.files = %w(Rakefile README.rdoc History.txt) + Dir.glob("{bin,lib,spec}/**/*")
18
+ s.add_dependency("mime-types", ">= 1.15")
19
+ s.add_dependency("highline", ">= 1.5.1")
20
+ s.add_dependency("rubytter", ">= 0.8.0")
21
+ s.executables = ["twitpic"]
22
+ s.authors = %w(jugyo)
23
+ s.email = 'jugyo.org@gmail.com'
24
+ s.homepage = 'http://github.com/jugyo/twitpic'
25
+ s.rubyforge_project = 'twitpic'
26
+ s.has_rdoc = true
27
+ s.rdoc_options = ["--main", "README.rdoc", "--exclude", "spec"]
28
+ s.extra_rdoc_files = ["README.rdoc", "History.txt"]
29
+ end
30
+
31
+ Rake::GemPackageTask.new(spec) do |p|
32
+ p.need_tar = true
33
+ end
34
+
35
+ task :gemspec do
36
+ filename = "#{name}.gemspec"
37
+ open(filename, 'w') do |f|
38
+ f.write spec.to_ruby
39
+ end
40
+ puts <<-EOS
41
+ Successfully generated gemspec
42
+ Name: #{name}
43
+ Version: #{version}
44
+ File: #{filename}
45
+ EOS
46
+ end
47
+
48
+ task :install => [ :package ] do
49
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
50
+ end
51
+
52
+ task :uninstall => [ :clean ] do
53
+ sh %{sudo gem uninstall #{name}}
54
+ end
55
+
56
+ desc 'run all specs'
57
+ Spec::Rake::SpecTask.new do |t|
58
+ t.spec_files = FileList['spec/**/*_spec.rb']
59
+ t.spec_opts = ['-c']
60
+ end
61
+
62
+ Rake::RDocTask.new do |t|
63
+ t.rdoc_dir = 'rdoc'
64
+ t.title = "rest-client, fetch RESTful resources effortlessly"
65
+ t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
66
+ t.options << '--charset' << 'utf-8'
67
+ t.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
68
+ end
69
+
70
+ CLEAN.include [ 'pkg', 'rdoc' ]
@@ -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) || system('import', 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}\""
@@ -0,0 +1,82 @@
1
+ require "net/https"
2
+ require "uri"
3
+ require 'mime/types'
4
+ require "rexml/document"
5
+
6
+ class TwitPic
7
+ VERSION = '0.3.3'
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, 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, 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 content_type
61
+ 'image/jpeg'
62
+ end
63
+
64
+ def create_body(parts, file, boundary)
65
+ parts[:media] = file
66
+ body = ''
67
+ [:media, :username, :password, :message].each do |key|
68
+ value = parts[key]
69
+ next unless value
70
+ body << "--#{boundary}\r\n"
71
+ if key == :media
72
+ body << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"ActivePhoto\"\r\n"
73
+ body << "Content-Type: #{content_type}\r\n"
74
+ else
75
+ body << "Content-Disposition: form-data; name=\"#{key}\"\r\n"
76
+ end
77
+ body << "\r\n"
78
+ body << "#{value}\r\n"
79
+ end
80
+ body << "--#{boundary}--\r\n"
81
+ end
82
+ end
@@ -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,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitbookis
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 3
9
+ version: 0.3.3
10
+ platform: ruby
11
+ authors:
12
+ - jugyo
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-27 00:00:00 -07:00
18
+ default_executable: twitpic
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mime-types
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 15
30
+ version: "1.15"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: highline
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 1
42
+ - 5
43
+ - 1
44
+ version: 1.5.1
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: rubytter
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ - 8
57
+ - 0
58
+ version: 0.8.0
59
+ type: :runtime
60
+ version_requirements: *id003
61
+ description: Library for TwitPic API.
62
+ email: vegan.bookis@gmail.com
63
+ executables:
64
+ - twitpic
65
+ extensions: []
66
+
67
+ extra_rdoc_files:
68
+ - README.rdoc
69
+ - History.txt
70
+ files:
71
+ - Rakefile
72
+ - README.rdoc
73
+ - History.txt
74
+ - bin/twitpic
75
+ - lib/twitpic.rb
76
+ - spec/test.txt
77
+ - spec/twitpic_spec.rb
78
+ has_rdoc: true
79
+ homepage: http://github.com/jugyo/twitpic
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --main
85
+ - README.rdoc
86
+ - --exclude
87
+ - spec
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project: twitpic
107
+ rubygems_version: 1.3.6
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Library for TwitPic API.
111
+ test_files: []
112
+