Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small code fixes. #11

Merged
merged 2 commits into from
Jan 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ Metrics/MethodLength:
Style/AlignParameters:
EnforcedStyle: with_fixed_indentation

Style/Alias:
Enabled: false

Style/BracesAroundHashParameters:
Enabled: false

Expand Down
22 changes: 19 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
bundler_args: --without development doc
language: ruby
sudo: false

before_install:
- gem update --system 2.6.8
- gem --version
- gem install bundler --version 1.13.7 --no-rdoc --no-ri
- bundle --version

install: bundle _1.13.7_ install --without development doc

script: bundle _1.13.7_ exec rake

env:
global:
- JRUBY_OPTS="$JRUBY_OPTS --debug"
language: ruby

rvm:
- jruby-9.1.7.0
- 2.0.0
- 2.1
- 2.2
- 2.3.3
- 2.4.0

matrix:
fast_finish: true
sudo: false

branches:
only:
- master
19 changes: 11 additions & 8 deletions lib/http/form_data/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,29 @@ class File < Part
# Default MIME type
DEFAULT_MIME = "application/octet-stream"

# @deprecated Use #content_type instead
alias mime_type content_type

# @see DEFAULT_MIME
# @param [String, StringIO, File] file_or_io Filename or IO instance.
# @param [#to_h] opts
# @option opts [#to_s] :content_type (DEFAULT_MIME)
# Value of Content-Type header
# @option opts [#to_s] :mime_type (DEFAULT_MIME)
# Alias for :content_type
# @option opts [#to_s] :filename
# When `file` is a String, defaults to basename of `file`.
# When `file` is a File, defaults to basename of `file`.
# When `file` is a StringIO, defaults to `"stream-{object_id}"`
def initialize(file_or_io, opts = {})
@file_or_io = file_or_io

opts = FormData.ensure_hash opts
opts = FormData.ensure_hash(opts)

@mime_type = opts.fetch(:mime_type) do
opts.fetch(:content_type) { DEFAULT_MIME }
if opts.key? :mime_type
warn "[DEPRECATED] :mime_type option deprecated, use :content_type"
opts[:content_type] = opts[:mime_type]
end
@filename = opts.fetch :filename do

@file_or_io = file_or_io
@content_type = opts.fetch(:content_type, DEFAULT_MIME).to_s
@filename = opts.fetch :filename do
case file_or_io
when String then ::File.basename file_or_io
when ::File then ::File.basename file_or_io.path
Expand Down
9 changes: 3 additions & 6 deletions lib/http/form_data/part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ module FormData
# body = "Message"
# FormData::Part.new body, :content_type => 'foobar.txt; charset="UTF-8"'
class Part
attr_reader :mime_type, :filename

alias_method :content_type, :mime_type
attr_reader :content_type, :filename

# @param [#to_s] body
# @param [String] content_type Value of Content-Type header
# @param [String] mime_type Alias for content_type
# @param [String] filename Value of filename parameter
def initialize(body, content_type: nil, mime_type: nil, filename: nil)
def initialize(body, content_type: nil, filename: nil)
@body = body.to_s
@mime_type = mime_type || content_type
@content_type = content_type
@filename = filename
end

Expand Down
15 changes: 7 additions & 8 deletions spec/lib/http/form_data/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,21 @@
end
end

describe "#mime_type" do
subject { described_class.new(StringIO.new, opts).mime_type }
describe "#content_type" do
subject { described_class.new(StringIO.new, opts).content_type }

it { is_expected.to eq "application/octet-stream" }

context "when it was given with options" do
let(:opts) { { :mime_type => "application/json" } }
let(:opts) { { :content_type => "application/json" } }
it { is_expected.to eq "application/json" }
end
end

describe "#content_type" do
it "should be an alias of #mime_type" do
expect(described_class.instance_method(:content_type)).to(
eq(described_class.instance_method(:mime_type))
)
describe "#mime_type" do
it "should be an alias of #content_type" do
expect(described_class.instance_method(:mime_type))
.to eq(described_class.instance_method(:content_type))
end
end
end
14 changes: 3 additions & 11 deletions spec/lib/http/form_data/part_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,14 @@
end
end

describe "#mime_type" do
subject { described_class.new(body, opts).mime_type }
describe "#content_type" do
subject { described_class.new(body, opts).content_type }

it { is_expected.to eq nil }

context "when it was given with options" do
let(:opts) { { :mime_type => "application/json" } }
let(:opts) { { :content_type => "application/json" } }
it { is_expected.to eq "application/json" }
end
end

describe "#content_type" do
it "should be an alias of #mime_type" do
expect(described_class.instance_method(:content_type)).to(
eq(described_class.instance_method(:mime_type))
)
end
end
end