Skip to content

Commit

Permalink
Int coerce doesn’t fail if already int
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorfinnell committed Nov 11, 2018
1 parent 9bdcfc8 commit 2aaf83a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hqtrivia
version: 0.2.4
version: 0.2.5

authors:
- Taylor Finnell <[email protected]>
Expand Down
17 changes: 17 additions & 0 deletions spec/hqtrivia/model/int_coerce_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "../../spec_helper"

module HqTrivia::Model
describe IntCoerce do
describe "from_json" do
it "works with strings" do
pull = JSON::PullParser.new("\"1\"")
IntCoerce.from_json(pull).should eq(1)
end

it "works with ints" do
pull = JSON::PullParser.new("1")
IntCoerce.from_json(pull).should eq(1)
end
end
end
end
6 changes: 5 additions & 1 deletion src/hqtrivia/model/int_coerce.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ module HqTrivia
module Model
class IntCoerce
def self.from_json(json : JSON::PullParser)
json.read_string.to_i
if val = json.read?(Int32)
val
else
json.read_string.to_i
end
end

def self.to_json(value, builder)
Expand Down
2 changes: 1 addition & 1 deletion src/hqtrivia/version.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module HqTrivia
# Current HqTrivia version
VERSION = "0.2.4"
VERSION = "0.2.5"
end

0 comments on commit 2aaf83a

Please sign in to comment.