twitpic 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -0
- data/Rakefile +1 -0
- data/bin/twitpic +33 -2
- data/lib/twitpic.rb +19 -2
- data/spec/twitpic_spec.rb +34 -0
- metadata +16 -4
data/README.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -16,6 +16,7 @@ spec = Gem::Specification.new do |s|
|
|
16
16
|
s.files = %w(Rakefile README.rdoc History.txt) + Dir.glob("{bin,lib,spec}/**/*")
|
17
17
|
s.add_dependency("mime-types", ">= 1.15")
|
18
18
|
s.add_dependency("highline", ">= 1.5.1")
|
19
|
+
s.add_dependency("rubytter", ">= 0.8.0")
|
19
20
|
s.executables = ["twitpic"]
|
20
21
|
s.authors = %w(jugyo)
|
21
22
|
s.email = 'jugyo.org@gmail.com'
|
data/bin/twitpic
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'twitpic'
|
5
|
+
require 'rubytter'
|
5
6
|
require 'optparse'
|
6
7
|
require "highline"
|
7
8
|
|
@@ -18,6 +19,16 @@ OptionParser.new do |o|
|
|
18
19
|
o.on('-p', '--password=PASSWORD') {|v| password = v}
|
19
20
|
o.on('-m', '--message=MESSAGE') {|v| message = v}
|
20
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
|
+
}
|
21
32
|
o.permute!(ARGV)
|
22
33
|
file = ARGV.first
|
23
34
|
if file.nil? && screencapture.nil?
|
@@ -29,7 +40,7 @@ end
|
|
29
40
|
if file.nil? && screencapture
|
30
41
|
file = '/tmp/twitpic_screencapture.png'
|
31
42
|
File.delete(file) if File.exists?(file)
|
32
|
-
puts 'Please capture screen
|
43
|
+
puts 'Please capture screen!'
|
33
44
|
system 'screencapture', '-i', '-f', file
|
34
45
|
unless File.exists?(file)
|
35
46
|
puts 'Aboat!'
|
@@ -37,10 +48,30 @@ if file.nil? && screencapture
|
|
37
48
|
end
|
38
49
|
end
|
39
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
|
40
64
|
highline = HighLine.new
|
41
65
|
username = highline.ask('USERNAME: ') unless username
|
42
66
|
password = highline.ask('PASSWORD: ') {|q| q.echo = '*'} unless password
|
43
67
|
message = highline.ask('MESSAGE: ') unless message
|
44
68
|
|
45
69
|
puts 'Uploading...'
|
46
|
-
TwitPic.new(username, password).upload(file
|
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
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
require "net/https"
|
2
2
|
require "uri"
|
3
3
|
require 'mime/types'
|
4
|
+
require "rexml/document"
|
4
5
|
|
5
6
|
class TwitPic
|
6
|
-
VERSION = '0.
|
7
|
+
VERSION = '0.3.0'
|
8
|
+
|
9
|
+
class APIError < StandardError; end
|
7
10
|
|
8
11
|
def initialize(username, password)
|
9
12
|
@username = username
|
@@ -37,7 +40,21 @@ class TwitPic
|
|
37
40
|
response = http.post(uri.path, body, headers)
|
38
41
|
response.body
|
39
42
|
end
|
40
|
-
xml
|
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
|
41
58
|
end
|
42
59
|
|
43
60
|
def create_body(parts, file_path, boundary)
|
data/spec/twitpic_spec.rb
CHANGED
@@ -32,6 +32,40 @@ password\r
|
|
32
32
|
EOS
|
33
33
|
end
|
34
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
|
+
|
35
69
|
describe 'with message' do
|
36
70
|
it 'should call method "post"' do
|
37
71
|
file_path = File.dirname(__FILE__) + '/test.txt'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twitpic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jugyo
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06
|
12
|
+
date: 2009-07-06 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -32,6 +32,16 @@ dependencies:
|
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 1.5.1
|
34
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:
|
35
45
|
description: Library for TwitPic API.
|
36
46
|
email: jugyo.org@gmail.com
|
37
47
|
executables:
|
@@ -51,6 +61,8 @@ files:
|
|
51
61
|
- spec/twitpic_spec.rb
|
52
62
|
has_rdoc: true
|
53
63
|
homepage: http://github.com/jugyo/twitpic
|
64
|
+
licenses: []
|
65
|
+
|
54
66
|
post_install_message:
|
55
67
|
rdoc_options:
|
56
68
|
- --main
|
@@ -74,9 +86,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
86
|
requirements: []
|
75
87
|
|
76
88
|
rubyforge_project: twitpic
|
77
|
-
rubygems_version: 1.3.
|
89
|
+
rubygems_version: 1.3.4
|
78
90
|
signing_key:
|
79
|
-
specification_version:
|
91
|
+
specification_version: 3
|
80
92
|
summary: Library for TwitPic API.
|
81
93
|
test_files: []
|
82
94
|
|