klarrimore-routengn 0.23 → 0.24

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,80 +1,122 @@
1
- # Takes a hash of string and file parameters and returns a string of text
2
- # formatted to be sent as a multipart form post.
3
- #
4
- # Author:: Cody Brimhall <mailto:cbrimhall@ucdavis.edu>
5
- # Created:: 22 Feb 2008
6
-
7
- require 'rubygems'
8
- require 'mime/types'
9
- require 'cgi'
10
-
11
-
12
- module Multipart
13
- VERSION = "1.0.0" unless const_defined?(:VERSION)
14
-
15
- # Formats a given hash as a multipart form post
16
- # If a hash value responds to :string or :read messages, then it is
17
- # interpreted as a file and processed accordingly; otherwise, it is assumed
18
- # to be a string
19
- class Post
20
- # We have to pretend like we're a web browser...
21
- USERAGENT = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6" unless const_defined?(:USERAGENT)
22
- BOUNDARY = "0123456789ABLEWASIEREISAWELBA9876543210" unless const_defined?(:BOUNDARY)
23
- CONTENT_TYPE = "multipart/form-data; boundary=#{ BOUNDARY }" unless const_defined?(:CONTENT_TYPE)
24
- HEADER = { "Content-Type" => CONTENT_TYPE, "User-Agent" => USERAGENT } unless const_defined?(:HEADER)
25
-
26
- def self.prepare_query(params)
27
- fp = []
28
-
29
- params.each do |k, v|
30
- # Are we trying to make a file parameter?
31
- if v.respond_to?(:path) and v.respond_to?(:read) then
32
- fp.push(FileParam.new(k, v.path, v.read))
33
- # We must be trying to make a regular parameter
34
- else
35
- fp.push(StringParam.new(k, v))
36
- end
37
- end
38
-
39
- # Assemble the request body using the special multipart format
40
- query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
41
- return query, HEADER
42
- end
43
- end
44
-
45
- private
46
-
47
- # Formats a basic string key/value pair for inclusion with a multipart post
48
- class StringParam
49
- attr_accessor :k, :v
50
-
51
- def initialize(k, v)
52
- @k = k
53
- @v = v
54
- end
55
-
56
- def to_multipart
57
- return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
58
- end
59
- end
60
-
61
- # Formats the contents of a file or string for inclusion with a multipart
62
- # form post
63
- class FileParam
64
- attr_accessor :k, :filename, :content
65
-
66
- def initialize(k, filename, content)
67
- @k = k
68
- @filename = filename
69
- @content = content
70
- end
71
-
72
- def to_multipart
73
- # If we can tell the possible mime-type from the filename, use the
74
- # first in the list; otherwise, use "application/octet-stream"
75
- mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
76
- return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{ filename }\"\r\n" +
77
- "Content-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n"
78
- end
79
- end
1
+ module RouteNGN
2
+ module HTTP
3
+ class Multipart
4
+
5
+ def initialize( file_names )
6
+ @file_names = file_names
7
+ end
8
+
9
+ def post( to_url )
10
+ boundary = '----RubyMultipartClient' + rand(1000000).to_s + 'ZZZZZ'
11
+
12
+ parts = []
13
+ streams = []
14
+ @file_names.each do |param_name, filepath|
15
+ pos = filepath.rindex('/')
16
+ filename = filepath[pos + 1, filepath.length - pos]
17
+ parts << StringPart.new ( "--" + boundary + "\r\n" +
18
+ "Content-Disposition: form-data; name=\"" + param_name.to_s + "\"; filename=\"" + filename + "\"\r\n" +
19
+ "Content-Type: video/x-msvideo\r\n\r\n")
20
+ stream = File.open(filepath, "rb")
21
+ streams << stream
22
+ parts << StreamPart.new (stream, File.size(filepath))
23
+ end
24
+ parts << StringPart.new ( "\r\n--" + boundary + "--\r\n" )
25
+
26
+ post_stream = MultipartStream.new( parts )
27
+
28
+ url = URI.parse( to_url )
29
+ req = Net::HTTP::Post.new(url.path)
30
+ req.content_length = post_stream.size
31
+ req.content_type = 'multipart/form-data; boundary=' + boundary
32
+ req.body_stream = post_stream
33
+ res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
34
+
35
+ streams.each do |stream|
36
+ stream.close();
37
+ end
38
+
39
+ res
40
+ end
41
+
42
+ end
43
+
44
+ class StreamPart
45
+ def initialize( stream, size )
46
+ @stream, @size = stream, size
47
+ end
48
+
49
+ def size
50
+ @size
51
+ end
52
+
53
+ def read ( offset, how_much )
54
+ @stream.read ( how_much )
55
+ end
56
+ end
57
+
58
+ class StringPart
59
+ def initialize ( str )
60
+ @str = str
61
+ end
62
+
63
+ def size
64
+ @str.length
65
+ end
66
+
67
+ def read ( offset, how_much )
68
+ @str[offset, how_much]
69
+ end
70
+ end
71
+
72
+ class MultipartStream
73
+ def initialize( parts )
74
+ @parts = parts
75
+ @part_no = 0;
76
+ @part_offset = 0;
77
+ end
78
+
79
+ def size
80
+ total = 0
81
+ @parts.each do |part|
82
+ total += part.size
83
+ end
84
+ total
85
+ end
86
+
87
+ def read ( how_much )
88
+
89
+ if @part_no >= @parts.size
90
+ return nil;
91
+ end
92
+
93
+ how_much_current_part = @parts[@part_no].size - @part_offset
94
+
95
+ how_much_current_part = if how_much_current_part > how_much
96
+ how_much
97
+ else
98
+ how_much_current_part
99
+ end
100
+
101
+ how_much_next_part = how_much - how_much_current_part
102
+
103
+ current_part = @parts[@part_no].read(@part_offset, how_much_current_part )
104
+
105
+ if how_much_next_part > 0
106
+ @part_no += 1
107
+ @part_offset = 0
108
+ next_part = read ( how_much_next_part )
109
+ current_part + if next_part
110
+ next_part
111
+ else
112
+ ''
113
+ end
114
+ else
115
+ @part_offset += how_much_current_part
116
+ current_part
117
+ end
118
+ end
119
+ end
120
+ end
80
121
  end
122
+
@@ -62,6 +62,11 @@ module RouteNGN
62
62
  end
63
63
  end
64
64
 
65
+ define_method :upload do |*args|
66
+ uri, data, headers = args
67
+ raw = @connection.access_token.post uri, data, headers
68
+ end
69
+
65
70
  private
66
71
  def check_connection!
67
72
  raise ConnectionException.new unless @connection
@@ -2,6 +2,7 @@ class Record
2
2
  include RouteNGN::Mapper
3
3
 
4
4
  field :dialcode, :primary => true
5
+ field :account_id
5
6
  field :rn
6
7
  field :locale_id
7
8
  field :sent
@@ -8,9 +8,11 @@ module RouteNGN
8
8
  end
9
9
 
10
10
  module ClassMethods
11
- def upload
12
- # STUB
13
- puts "data, headers = Multipart::Post.prepare_query(\"title\" => 'title', \"uploaded_data\" => File.new(path))"
11
+ def upload(file, params = {})
12
+ data, headers = Multipart::Post.prepare_query("title" => 'title', "uploaded_data" => File.new(file))
13
+ puts data.inspect
14
+ puts headers.inspect
15
+ RouteNGN.connection.access_token.post("/upload#{base_url}", data, headers)
14
16
  end
15
17
  end
16
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klarrimore-routengn
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.23"
4
+ version: "0.24"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Larrimore