Skip to content

Commit

Permalink
More Specs for DataMappings module.
Browse files Browse the repository at this point in the history
  • Loading branch information
tszolar committed May 12, 2014
1 parent 0e11dfe commit 9b2f7cf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
13 changes: 8 additions & 5 deletions lib/kosapi_client/entity/data_mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@ def convert_type(value, type)
raise "Unknown type #{type} to convert value #{value} to."
end

def convert_array(value, type)
if value.is_a?(Array)
value.map { |it| convert_type(it, type) }
# Converts values of array type to proper domain objects.
# It checks whether the value is really an array, because
# when API returns a single value it does not get parsed
# into an array.
def convert_array(values, type)
if values.is_a?(Array)
values.map { |it| convert_type(it, type) }
else
convert_type(value, type)
convert_type(values, type)
end
end


end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/kosapi_client/http_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def each(&block)
items.each(&block)
end


def detect_type
type_str = sample_entry[:xsi_type]
extract_type(type_str)
Expand Down
16 changes: 16 additions & 0 deletions spec/kosapi_client/entity/data_mappings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@
expect(parsed.foo).to eq :bar
end

it 'throws error when required attribute is missing' do
dummy_class.map_data :foo, String, required: true
expect { dummy_class.parse({}) }.to raise_error(RuntimeError)
end

it 'sets attribute to nil when not required attribute is missing' do
dummy_class.map_data :foo, String
parsed = dummy_class.parse({})
expect(parsed.foo).to be_nil
end

it 'throws error on type without parse method' do
dummy_class.map_data :foo, Object
expect{ dummy_class.parse({foo: 'bar'}) }.to raise_error(RuntimeError)
end

end

end

0 comments on commit 9b2f7cf

Please sign in to comment.