Skip to content
Open
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
6 changes: 4 additions & 2 deletions lib/prismic/json_parsers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def integration_fields_parser(json)
Prismic::Fragments::IntegrationField.new(json['value'])
end

def boolean_field_parser(json)
def boolean_field_parser(json)
Prismic::Fragments::BooleanField.new(json['value'])
end

Expand Down Expand Up @@ -84,7 +84,7 @@ def text_parser(json)
Prismic::Fragments::Text.new(json['value'])
end

def separator_parser(json)
def separator_parser(_)
Prismic::Fragments::Separator.new('')
end

Expand All @@ -97,6 +97,8 @@ def web_link_parser(json)
end

def date_parser(json)
return Prismic::Fragments::Text.new('') if json['value'].empty?

Prismic::Fragments::Date.new(Time.parse(json['value']))
end

Expand Down
45 changes: 32 additions & 13 deletions spec/json_parsers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,38 @@
end

describe 'date_parser' do
before do
raw_json = <<json
{
"type": "date",
"value": "2013-09-19"
}
json
@json = JSON.load(raw_json)
context 'when date response' do
before do
raw_json = <<~JSON
{
"type": "date",
"value": "2013-09-19"
}
JSON
@json = JSON.load(raw_json)
end

it "correctly parses Date objects" do
date = Prismic::JsonParser.date_parser(@json)
date.value.should == Time.new(2013, 9, 19)
end
end

it "correctly parses Date objects" do
date = Prismic::JsonParser.date_parser(@json)
date.value.should == Time.new(2013, 9, 19)
context 'when date response not valid date' do
before do
raw_json = <<~JSON
{
"type": "date",
"value": ""
}
JSON
@json = JSON.load(raw_json)
end

it "returns empty string for the value" do
date = Prismic::JsonParser.date_parser(@json)
date.value.should == ''
end
end
end

Expand Down Expand Up @@ -496,7 +515,7 @@

end

describe 'boolean_field_parser' do
describe 'boolean_field_parser' do
before do
raw_json = <<json
{
Expand All @@ -511,4 +530,4 @@
bool = Prismic::JsonParser.boolean_field_parser(@json)
bool.value.should == true
end
end
end