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

Support ruby 2.4 #1543

Merged
merged 2 commits into from
Dec 28, 2016
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
12 changes: 11 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ sudo: false

matrix:
include:
- rvm: 2.3.3
- rvm: 2.4.0
script:
- bundle exec danger
- rvm: 2.4.0
gemfile: Gemfile
- rvm: 2.4.0
gemfile: gemfiles/rack_edge.gemfile
- rvm: 2.4.0
gemfile: gemfiles/rack_1.5.2.gemfile
- rvm: 2.4.0
gemfile: gemfiles/rails_edge.gemfile
- rvm: 2.4.0
gemfile: gemfiles/rails_5.gemfile
- rvm: 2.3.3
gemfile: Gemfile
- rvm: 2.3.3
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#### 0.19.1 (Next)
### 0.19.1 (Next)

#### Features

* [#1536](/~https://github.com/ruby-grape/grape/pull/1536): Updates `invalid_versioner_option` translation - [@Lavode](/~https://github.com/Lavode).
* [#1543](/~https://github.com/ruby-grape/grape/pull/1543): Support ruby 2.4 - [@LeFnord](/~https://github.com/LeFnord), [@namusyaka](/~https://github.com/namusyaka).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added @LeFnord .

* Your contribution here.

#### Fixes

* Your contribution here.

### 0.19.0 (12/18/2016)
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [Include Missing](#include-missing)
- [Parameter Validation and Coercion](#parameter-validation-and-coercion)
- [Supported Parameter Types](#supported-parameter-types)
- [Integer/Fixnum and Coercions](#integerfixnum-and-coercions)
- [Custom Types and Coercions](#custom-types-and-coercions)
- [Multipart File Parameters](#multipart-file-parameters)
- [First-Class `JSON` Types](#first-class-json-types)
Expand Down Expand Up @@ -799,6 +800,28 @@ The following are all valid types, supported out of the box by Grape:
* Rack::Multipart::UploadedFile (alias `File`)
* JSON

### Integer/Fixnum and Coercions

Please be aware that the behavior differs between Ruby 2.4 and earlier versions.
In Ruby 2.4, values consisting of numbers are converted to Integer, but in earlier versions it will be treated as Fixnum.

```ruby
params do
requires :integers, type: Hash do
requires :int, coerce: Integer
end
end
get '/int' do
params[:integers][:int].class
end

...

get '/int' integers: { int: '45' }
#=> Integer in ruby 2.4
#=> Fixnum in earlier ruby versions
```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Integer/Fixnum and Corcions section. Could you review this ?

### Custom Types and Coercions

Aside from the default set of supported types listed above, any class can be
Expand Down Expand Up @@ -2878,7 +2901,7 @@ end
The behaviour is then:

```bash
GET /123 # 'Fixnum'
GET /123 # 'Integer'
GET /foo # 400 error - 'blah is invalid'
```

Expand Down
4 changes: 2 additions & 2 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def status(status = nil)
when Symbol
raise ArgumentError, "Status code :#{status} is invalid." unless Rack::Utils::SYMBOL_TO_STATUS_CODE.keys.include?(status)
@status = Rack::Utils.status_code(status)
when Fixnum
when Integer
@status = status
when nil
return @status if @status
Expand All @@ -135,7 +135,7 @@ def status(status = nil)
200
end
else
raise ArgumentError, 'Status code must be Fixnum or Symbol.'
raise ArgumentError, 'Status code must be Integer or Symbol.'
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ class DummyFormatClass
end

get '/', id: '32'
expect(last_response.body).to eql 'first 32:Fixnum second'
expect(last_response.body).to eql "first 32:#{integer_class_name} second"
end

it 'adds a after filter' do
Expand Down Expand Up @@ -2506,7 +2506,7 @@ def static
end
end
describe 'status' do
it 'can be set to arbitrary Fixnum value' do
it 'can be set to arbitrary Integer value' do
subject.get '/foo' do
status 210
end
Expand Down
6 changes: 3 additions & 3 deletions spec/grape/dsl/inside_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ def initialize
.to raise_error(ArgumentError, 'Status code :foo_bar is invalid.')
end

it 'accepts unknown Fixnum status codes' do
it 'accepts unknown Integer status codes' do
expect { subject.status 210 }.to_not raise_error
end

it 'raises error if status is not a fixnum or symbol' do
it 'raises error if status is not a integer or symbol' do
expect { subject.status Object.new }
.to raise_error(ArgumentError, 'Status code must be Fixnum or Symbol.')
.to raise_error(ArgumentError, 'Status code must be Integer or Symbol.')
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/grape/dsl/request_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def self.imbue(key, value)
end

it 'abort if :with option value is not Symbol, String or Proc' do
expect { subject.rescue_from :all, with: 1234 }.to raise_error(ArgumentError, 'with: Fixnum, expected Symbol, String or Proc')
expect { subject.rescue_from :all, with: 1234 }.to raise_error(ArgumentError, "with: #{integer_class_name}, expected Symbol, String or Proc")
end

it 'abort if both :with option and block are passed' do
Expand Down
16 changes: 8 additions & 8 deletions spec/grape/validations/validators/allow_blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class API < Grape::API
get '/allow_float_blank'

params do
requires :val, type: Fixnum, allow_blank: true
requires :val, type: Integer, allow_blank: true
end
get '/allow_fixnum_blank'
get '/allow_integer_blank'

params do
requires :val, type: Symbol, allow_blank: true
Expand Down Expand Up @@ -173,9 +173,9 @@ class API < Grape::API
get '/allow_float_blank'

params do
requires :val, type: Fixnum, allow_blank: true
requires :val, type: Integer, allow_blank: true
end
get '/allow_fixnum_blank'
get '/allow_integer_blank'

params do
requires :val, type: Symbol, allow_blank: true
Expand Down Expand Up @@ -345,8 +345,8 @@ def app
expect(last_response.status).to eq(200)
end

it 'accepts empty when fixnum allow_blank' do
get '/custom_message/allow_fixnum_blank', val: ''
it 'accepts empty when integer allow_blank' do
get '/custom_message/allow_integer_blank', val: ''
expect(last_response.status).to eq(200)
end
end
Expand Down Expand Up @@ -483,8 +483,8 @@ def app
expect(last_response.status).to eq(200)
end

it 'accepts empty when fixnum allow_blank' do
get '/allow_fixnum_blank', val: ''
it 'accepts empty when integer allow_blank' do
get '/allow_integer_blank', val: ''
expect(last_response.status).to eq(200)
end
end
Expand Down
14 changes: 7 additions & 7 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class User

get '/int', int: '45'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum')
expect(last_response.body).to eq(integer_class_name)
end

context 'Array' do
Expand All @@ -189,7 +189,7 @@ class User

get '/array', arry: %w(1 2 3)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum')
expect(last_response.body).to eq(integer_class_name)
end

it 'Array of Bools' do
Expand Down Expand Up @@ -238,7 +238,7 @@ class User

get '/set', set: Set.new([1, 2, 3, 4]).to_a
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum')
expect(last_response.body).to eq(integer_class_name)
end

it 'Set of Bools' do
Expand Down Expand Up @@ -326,7 +326,7 @@ class User

get '/int', integers: { int: '45' }
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum')
expect(last_response.body).to eq(integer_class_name)
end
end

Expand Down Expand Up @@ -525,11 +525,11 @@ class User

get '/', splines: '{"x":"1","y":"woof"}'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum.String')
expect(last_response.body).to eq("#{integer_class_name}.String")

get '/', splines: '[{"x":1,"y":2},{"x":1,"y":"quack"}]'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum.Fixnum')
expect(last_response.body).to eq("#{integer_class_name}.#{integer_class_name}")

get '/', splines: '{"x":"4","y":"woof"}'
expect(last_response.status).to eq(400)
Expand Down Expand Up @@ -576,7 +576,7 @@ class User

get '/', a: '5'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum')
expect(last_response.body).to eq(integer_class_name)

get '/', a: 'anything else'
expect(last_response.status).to eq(200)
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

RSpec.configure do |config|
config.include Rack::Test::Methods
config.include Spec::Support::Helpers
config.raise_errors_for_deprecations!

config.before(:each) { Grape::Util::InheritableSetting.reset_global! }
Expand Down
10 changes: 8 additions & 2 deletions spec/support/basic_auth_encode_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
def encode_basic_auth(username, password)
'Basic ' + Base64.encode64("#{username}:#{password}")
module Spec
module Support
module Helpers
def encode_basic_auth(username, password)
'Basic ' + Base64.encode64("#{username}:#{password}")
end
end
end
end
18 changes: 10 additions & 8 deletions spec/support/content_type_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module ContentTypeHelpers
%w(put patch post delete).each do |method|
define_method :"#{method}_with_json" do |uri, params = {}, env = {}, &block|
params = params.to_json
env['CONTENT_TYPE'] ||= 'application/json'
send(method, uri, params, env, &block)
module Spec
module Support
module Helpers
%w(put patch post delete).each do |method|
define_method :"#{method}_with_json" do |uri, params = {}, env = {}, &block|
params = params.to_json
env['CONTENT_TYPE'] ||= 'application/json'
send(method, uri, params, env, &block)
end
end
end
end
end

include(ContentTypeHelpers)
11 changes: 11 additions & 0 deletions spec/support/integer_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Spec
module Support
module Helpers
INTEGER_CLASS_NAME = 0.to_i.class.to_s.freeze
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How clever!


def integer_class_name
INTEGER_CLASS_NAME
end
end
end
end
Loading