twurl 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/twurl/cli.rb CHANGED
@@ -47,6 +47,8 @@ module Twurl
47
47
  Twurl.options.trace = false
48
48
  Twurl.options.data = {}
49
49
  Twurl.options.headers = {}
50
+ Twurl.options.upload = {}
51
+ Twurl.options.upload['file'] = []
50
52
 
51
53
  option_parser = OptionParser.new do |o|
52
54
  o.extend AvailableOptions
@@ -82,6 +84,9 @@ Supported Commands: #{SUPPORTED_COMMANDS.sort.join(', ')}
82
84
  help
83
85
  version
84
86
  proxy
87
+ file
88
+ filefield
89
+ base64
85
90
  end
86
91
  end
87
92
 
@@ -286,9 +291,34 @@ Supported Commands: #{SUPPORTED_COMMANDS.sort.join(', ')}
286
291
  options.proxy = proxy
287
292
  end
288
293
  end
294
+
295
+ def file
296
+ on('-f', '--file [path_to_file]', 'Specify the path to the file to upload') do |file|
297
+ if File.file?(file)
298
+ options.upload['file'] << file
299
+ else
300
+ CLI.puts "ERROR: File not found"
301
+ exit
302
+ end
303
+ end
304
+ end
305
+
306
+ def filefield
307
+ on('-F', '--file-field [field_name]', 'Specify the file field for the upload to be included within (default: media)') do |filefield|
308
+ options.upload['filefield'] = filefield
309
+ options.upload['filefield'] = 'media[]' if options.upload['filefield'].empty?
310
+ end
311
+ end
312
+
313
+ def base64
314
+ on('-b', '--base64', 'Encode the uploaded file as base64 (default: false)') do |base64|
315
+ options.upload['base64'] = base64
316
+ end
317
+ end
289
318
  end
290
319
  end
291
320
 
321
+
292
322
  class Options < OpenStruct
293
323
  DEFAULT_REQUEST_METHOD = 'get'
294
324
  DEFAULT_HOST = 'api.twitter.com'
@@ -75,7 +75,42 @@ module Twurl
75
75
  def perform_request_from_options(options, &block)
76
76
  request_class = METHODS.fetch(options.request_method.to_sym)
77
77
  request = request_class.new(options.path, options.headers)
78
- request.set_form_data(options.data) if options.data
78
+
79
+ if options.upload && options.upload['file'].count > 0
80
+ boundary = "00Twurl" + rand(1000000000000000000).to_s + "lruwT99"
81
+ multipart_body = []
82
+
83
+ options.data.each {|key, value|
84
+ multipart_body << "--#{boundary}\r\n"
85
+ multipart_body << "Content-Disposition: form-data; name=\"#{key}\"\r\n"
86
+ multipart_body << "\r\n"
87
+ multipart_body << value
88
+ multipart_body << "\r\n"
89
+ }
90
+
91
+ options.upload['file'].each {|filename|
92
+ multipart_body << "--#{boundary}\r\n"
93
+ multipart_body << "Content-Disposition: form-data; name=\"#{options.upload['filefield']}\"; filename=\"#{File.basename(filename)}\"\r\n"
94
+ multipart_body << "Content-Type: application/octet-stream\r\n"
95
+ multipart_body << "Content-Transfer-Encoding: base64\r\n" if options.upload['base64']
96
+ multipart_body << "\r\n"
97
+
98
+ if options.upload['base64']
99
+ enc = Base64.encode64(File.read(filename))
100
+ multipart_body << enc
101
+ else
102
+ multipart_body << File.read(filename)
103
+ end
104
+ }
105
+
106
+ multipart_body << "\r\n--#{boundary}--\r\n"
107
+
108
+ request.body = multipart_body.join
109
+ request['Content-Type'] = "multipart/form-data, boundary=\"#{boundary}\""
110
+ elsif options.data
111
+ request.set_form_data(options.data)
112
+ end
113
+
79
114
  request.oauth!(consumer.http, consumer, access_token)
80
115
  consumer.http.request(request, &block)
81
116
  end
data/lib/twurl/version.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module Twurl
2
2
  class Version
3
3
  MAJOR = 0 unless defined? Twurl::Version::MAJOR
4
- MINOR = 8 unless defined? Twurl::Version::MINOR
5
- PATCH = 3 unless defined? Twurl::Version::PATCH
4
+ MINOR = 9 unless defined? Twurl::Version::MINOR
5
+ PATCH = 0 unless defined? Twurl::Version::PATCH
6
6
  BETA = nil unless defined? Twurl::Version::BETA # Time.now.to_i.to_s
7
7
 
8
8
  class << self
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twurl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.9.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-07-12 00:00:00.000000000 Z
13
+ date: 2013-10-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: oauth