formdata 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: caccbe286e92c40d511e574f3d9b70fbe5be470e
4
+ data.tar.gz: 539bf1715974b63de330bdf292519c77e97e81fd
5
+ SHA512:
6
+ metadata.gz: 6b7f53822152b8d5254b5d2831ec7f5347b78c1fc74acf0d8ff78acf3817981c6c301e6a4d084390208d4577477432a8e172b255ecdfe340575ba93d4ead3582
7
+ data.tar.gz: f3b362f3d24b0d70c0d18cbfc1ee88194ffecd96c894882c74c0d8f8407d39717f9d61873413847e1fc5759584d9c36c58c673845f0f61b051bb773f6e513a61
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/tmp/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.8
6
+ - 2.2.4
7
+ - 2.3.0
8
+ script: "bundle exec rake"
9
+ before_install:
10
+ - uname -a
11
+ - gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nelson Darkwah Oppong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # FormData [![Build Status](https://travis-ci.org/nelsond/formdata.svg?branch=master)](https://travis-ci.org/nelsond/formdata)
2
+
3
+ Ruby gem to generate data in the same format as "multipart/form-data".
4
+ The gem is heavily inspired by the [JavaScript FormData API](https://developer.mozilla.org/en/docs/Web/API/FormData).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'formdata'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install formdata
21
+
22
+ ## Example usage
23
+
24
+ ```ruby
25
+ require 'formdata'
26
+ require 'stringio'
27
+ require 'net/http'
28
+
29
+ # create form data
30
+ f = FormData.new
31
+
32
+ # append some text values...
33
+ f.append('name', 'Richard')
34
+ f.append('surname', 'Feynman')
35
+
36
+ # ...and some files...
37
+ f.append('avatar-file', File.open('some/file.jpg'))
38
+ f.append('cv-file', File.open('some/file.pdf'), {
39
+ :content_type => 'application/pdf',
40
+ :filename => 'cv.pdf'
41
+ })
42
+ f.append('text-file', StringIO.new('test'), {
43
+ :content_tyoe => 'text/plain',
44
+ :filename => 'text.txt'
45
+ })
46
+
47
+ # ...or multiple values from a hash
48
+ f.append({
49
+ 'street' => '1200 E California Blvd',
50
+ 'city' => 'Pasadena',
51
+ 'zip' => '91125',
52
+ 'state' => 'CA',
53
+ 'map-file' => File.open('some/file.pdf')
54
+ })
55
+
56
+ # create a new net/http request
57
+ req = Net::HTTP::Post.new('/test')
58
+ req.content_type = f.content_type
59
+ req.content_length = f.size
60
+ req.body_stream = f
61
+
62
+ # send the request
63
+ http = Net::HTTP.new('localhost', 80)
64
+ http.request(req) # => ...response
65
+
66
+ # or use the request constructor
67
+ req = f.post_request('/test')
68
+ http = Net::HTTP.new('localhost', 80)
69
+ http.request(req) # => ...response
70
+ ```
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'formdata/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'formdata'
8
+ spec.version = FormData::VERSION
9
+ spec.authors = ['Nelson Darkwah Oppong']
10
+ spec.email = ['n@darkwahoppong.com']
11
+
12
+ spec.summary = 'Ruby gem to generate data in the same format as "multipart/form-data".'
13
+ spec.homepage = 'http://github.com/nelsond/formdata'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.11'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_development_dependency 'simplecov', '~> 0.11'
25
+ spec.add_development_dependency 'webmock', '~> 1.22'
26
+ spec.add_development_dependency 'sinatra', '~> 1.4.6'
27
+ end
@@ -0,0 +1,119 @@
1
+ require 'formdata/version'
2
+ require 'securerandom'
3
+ require 'tempfile'
4
+ require 'mkmf'
5
+ require 'net/http'
6
+
7
+ module FormData
8
+ def self.new
9
+ FormData.new
10
+ end
11
+
12
+ class FormData
13
+ attr_reader :boundary
14
+
15
+ def initialize
16
+ @id = SecureRandom.hex(10).freeze
17
+
18
+ @buffer = Tempfile.new(@id)
19
+ @buffer.binmode
20
+
21
+ @boundary = "----RubyFormBoundary#{@id}".freeze
22
+
23
+ @finalized = false
24
+ end
25
+
26
+ def append(name, value = nil, opts = {})
27
+ name.each { |n, v| append(n, v) } if name.is_a?(Hash)
28
+
29
+ if value.respond_to?(:read)
30
+ append_file(name, value, opts)
31
+ else
32
+ append_text(name, value)
33
+ end
34
+ end
35
+
36
+ def content_type
37
+ "multipart/form-data; boundary=#{@boundary}"
38
+ end
39
+
40
+ def size
41
+ @finalized ? @buffer.size : @buffer.size + epilogue.size
42
+ end
43
+ alias length size
44
+
45
+ def read(*args)
46
+ finalize! unless @finalized
47
+ @buffer.read(*args)
48
+ end
49
+
50
+ def eof?(*args)
51
+ return false unless @finalized
52
+ @buffer.eof?(*args)
53
+ end
54
+
55
+ def post_request(*args)
56
+ req = Net::HTTP::Post.new(*args)
57
+ req.content_type = content_type
58
+ req.content_length = size
59
+ req.body_stream = self
60
+
61
+ req
62
+ end
63
+
64
+ private
65
+
66
+ def append_text(name, text)
67
+ write_boundary
68
+ @buffer.write %(Content-Disposition: form-data; name="#{name}"\r\n\r\n)
69
+ @buffer.write text.to_s
70
+ end
71
+
72
+ def append_file(name, file, opts = {})
73
+ opts[:filename] ||= File.basename(file.path) if file.respond_to?(:path)
74
+
75
+ if file.respond_to?(:path) && File.readable?(file.path)
76
+ opts[:content_type] ||= guess_mime_type(file)
77
+ end
78
+
79
+ opts = {
80
+ content_type: 'application/octet-stream',
81
+ filename: 'unknown'
82
+ }.merge(opts)
83
+
84
+ write_boundary
85
+ @buffer.write %(Content-Disposition: form-data; name="#{name}"; filename="#{opts[:filename]}"\r\n)
86
+ @buffer.write %(Content-Type: #{opts[:content_type]}\r\n\r\n)
87
+
88
+ chunk_size = 1024**2
89
+ @buffer.write file.read(chunk_size) until file.eof?
90
+ file.close
91
+ end
92
+
93
+ def write_boundary
94
+ @buffer.write "\r\n" if size > 0
95
+ @buffer.write "--#{@boundary}\r\n"
96
+ end
97
+
98
+ def epilogue
99
+ "\r\n--#{@boundary}--\r\n"
100
+ end
101
+
102
+ def finalize!
103
+ @buffer.write epilogue
104
+ @buffer.rewind
105
+ @finalized = true
106
+ end
107
+
108
+ def guess_mime_type(file)
109
+ return unless find_executable0('file')
110
+
111
+ content_type = nil
112
+ IO.popen(['file', '--brief', '--mime-type', file.path]) do |m|
113
+ content_type = m.read.strip
114
+ end
115
+
116
+ $?.exitstatus == 0 ? content_type : nil
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,3 @@
1
+ module FormData
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formdata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nelson Darkwah Oppong
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.22'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.22'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sinatra
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.4.6
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.4.6
97
+ description:
98
+ email:
99
+ - n@darkwahoppong.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - formdata.gemspec
112
+ - lib/formdata.rb
113
+ - lib/formdata/version.rb
114
+ homepage: http://github.com/nelsond/formdata
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.5.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Ruby gem to generate data in the same format as "multipart/form-data".
138
+ test_files: []