diff --git a/lib/iiif/presentation/abstract_resource.rb b/lib/iiif/presentation/abstract_resource.rb index 0096959..0f9a213 100644 --- a/lib/iiif/presentation/abstract_resource.rb +++ b/lib/iiif/presentation/abstract_resource.rb @@ -57,7 +57,6 @@ def initialize(hsh={}) super(hsh) end - # Options: # * force: (true|false). Skips validations. # * include_context: (true|false). Adds the @context to the top of the @@ -71,9 +70,11 @@ def to_ordered_hash(opts={}) end super(opts) end + + # alias_method has to be included after to_ordered_hash is defined + alias_method :as_json, :to_ordered_hash end - end end diff --git a/lib/iiif/service.rb b/lib/iiif/service.rb index a21e530..c754300 100644 --- a/lib/iiif/service.rb +++ b/lib/iiif/service.rb @@ -196,6 +196,9 @@ def to_ordered_hash(opts={}) export_hash end + # alias_method has to be included after to_ordered_hash is defined + alias_method :as_json, :to_ordered_hash + def self.from_ordered_hash(hsh, default_klass=IIIF::OrderedHash) # Create a new object (new_object) type = nil diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bdb8b63..2a9147a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,9 +1,11 @@ require 'iiif/presentation' require 'simplecov' require 'coveralls' -Dir["#{File.dirname(__FILE__)}/unit/iiif/presentation/shared_examples/*.rb"].each do |f| + +Dir["#{File.absolute_path(__dir__)}/support/**/*.rb"].each do |f| require f end + require 'vcr' VCR.configure do |c| diff --git a/spec/unit/iiif/presentation/shared_examples/abstract_resource_only_keys.rb b/spec/support/shared_examples/abstract_resource_only_keys.rb similarity index 100% rename from spec/unit/iiif/presentation/shared_examples/abstract_resource_only_keys.rb rename to spec/support/shared_examples/abstract_resource_only_keys.rb diff --git a/spec/unit/iiif/presentation/shared_examples/any_type_keys.rb b/spec/support/shared_examples/any_type_keys.rb similarity index 100% rename from spec/unit/iiif/presentation/shared_examples/any_type_keys.rb rename to spec/support/shared_examples/any_type_keys.rb diff --git a/spec/unit/iiif/presentation/shared_examples/array_only_keys.rb b/spec/support/shared_examples/array_only_keys.rb similarity index 100% rename from spec/unit/iiif/presentation/shared_examples/array_only_keys.rb rename to spec/support/shared_examples/array_only_keys.rb diff --git a/spec/unit/iiif/presentation/shared_examples/int_only_keys.rb b/spec/support/shared_examples/int_only_keys.rb similarity index 100% rename from spec/unit/iiif/presentation/shared_examples/int_only_keys.rb rename to spec/support/shared_examples/int_only_keys.rb diff --git a/spec/support/shared_examples/json_format.rb b/spec/support/shared_examples/json_format.rb new file mode 100644 index 0000000..3e2cc33 --- /dev/null +++ b/spec/support/shared_examples/json_format.rb @@ -0,0 +1,8 @@ +shared_examples 'it has symmetric as_json and to_json methods' do + describe "#{described_class}.as_json" do + it 'should return a json representation of the object as a ruby hash' do + obj = described_class.new(fixed_values) + expect(obj.as_json).to eq JSON.parse(obj.to_json) + end + end +end diff --git a/spec/unit/iiif/presentation/shared_examples/string_only_keys.rb b/spec/support/shared_examples/string_only_keys.rb similarity index 100% rename from spec/unit/iiif/presentation/shared_examples/string_only_keys.rb rename to spec/support/shared_examples/string_only_keys.rb diff --git a/spec/unit/iiif/presentation/annotation_spec.rb b/spec/unit/iiif/presentation/annotation_spec.rb index 5b9881e..397660b 100644 --- a/spec/unit/iiif/presentation/annotation_spec.rb +++ b/spec/unit/iiif/presentation/annotation_spec.rb @@ -1,7 +1,10 @@ describe IIIF::Presentation::Annotation do + let(:fixed_values) { {} } + describe "#{described_class}.define_methods_for_abstract_resource_only_keys" do it_behaves_like 'it has the appropriate methods for abstract_resource_only_keys' end + it_behaves_like 'it has symmetric as_json and to_json methods' end diff --git a/spec/unit/iiif/presentation/canvas_spec.rb b/spec/unit/iiif/presentation/canvas_spec.rb index fb65955..b34f203 100644 --- a/spec/unit/iiif/presentation/canvas_spec.rb +++ b/spec/unit/iiif/presentation/canvas_spec.rb @@ -42,5 +42,6 @@ end end + it_behaves_like 'it has symmetric as_json and to_json methods' end diff --git a/spec/unit/iiif/presentation/collection_spec.rb b/spec/unit/iiif/presentation/collection_spec.rb index 4fc1a68..c3f0a1d 100644 --- a/spec/unit/iiif/presentation/collection_spec.rb +++ b/spec/unit/iiif/presentation/collection_spec.rb @@ -49,6 +49,7 @@ describe '#validate' do end + it_behaves_like 'it has symmetric as_json and to_json methods' end diff --git a/spec/unit/iiif/presentation/image_resource_spec.rb b/spec/unit/iiif/presentation/image_resource_spec.rb index 70013e8..45411fb 100644 --- a/spec/unit/iiif/presentation/image_resource_spec.rb +++ b/spec/unit/iiif/presentation/image_resource_spec.rb @@ -10,4 +10,15 @@ it_behaves_like 'it has the appropriate methods for integer-only keys' end + let(:fixed_values) do + { + "@context" => "http://iiif.io/api/presentation/2/context.json", + "@id" => "http://www.example.org/iiif/image", + "label" => "p. 1", + "height" => 1000, + "width" => 750, + } + end + + it_behaves_like 'it has symmetric as_json and to_json methods' end diff --git a/spec/unit/iiif/presentation/layer_spec.rb b/spec/unit/iiif/presentation/layer_spec.rb index e9da757..072b3c2 100644 --- a/spec/unit/iiif/presentation/layer_spec.rb +++ b/spec/unit/iiif/presentation/layer_spec.rb @@ -34,5 +34,7 @@ it_behaves_like 'it has the appropriate methods for any-type keys' end + it_behaves_like 'it has symmetric as_json and to_json methods' + end diff --git a/spec/unit/iiif/presentation/manifest_spec.rb b/spec/unit/iiif/presentation/manifest_spec.rb index 4aa60a8..550558d 100644 --- a/spec/unit/iiif/presentation/manifest_spec.rb +++ b/spec/unit/iiif/presentation/manifest_spec.rb @@ -11,9 +11,9 @@ def initialize(hsh={}) let(:fixed_values) do { - 'type' => 'a:SubClass', - 'id' => 'http://example.com/prefix/manifest/123', - 'context' => IIIF::Presentation::CONTEXT, + '@type' => 'a:SubClass', + '@id' => 'http://example.com/prefix/manifest/123', + '@context' => IIIF::Presentation::CONTEXT, 'label' => 'Book 1', 'description' => 'A longer description of this example book. It should give some real information.', 'thumbnail' => { @@ -86,4 +86,5 @@ def initialize(hsh={}) it_behaves_like 'it has the appropriate methods for any-type keys' end + it_behaves_like 'it has symmetric as_json and to_json methods' end diff --git a/spec/unit/iiif/presentation/range_spec.rb b/spec/unit/iiif/presentation/range_spec.rb index 1cb257a..af9f8e5 100644 --- a/spec/unit/iiif/presentation/range_spec.rb +++ b/spec/unit/iiif/presentation/range_spec.rb @@ -38,6 +38,7 @@ describe '#validate' do end + it_behaves_like 'it has symmetric as_json and to_json methods' end diff --git a/spec/unit/iiif/presentation/resource_spec.rb b/spec/unit/iiif/presentation/resource_spec.rb index 3c46c42..98555a3 100644 --- a/spec/unit/iiif/presentation/resource_spec.rb +++ b/spec/unit/iiif/presentation/resource_spec.rb @@ -1,5 +1,13 @@ describe IIIF::Presentation::Resource do + let(:fixed_values) do + { + '@id' => 'http://www.example.org/iiif/book1/resource', + # '@type' => 'sc:Range', + 'label' => 'Introduction' + } + end + describe "#{described_class}.define_methods_for_abstract_resource_only_keys" do it_behaves_like 'it has the appropriate methods for abstract_resource_only_keys' end @@ -12,5 +20,6 @@ it_behaves_like 'it has the appropriate methods for string-only keys' end + it_behaves_like 'it has symmetric as_json and to_json methods' end diff --git a/spec/unit/iiif/presentation/sequence_spec.rb b/spec/unit/iiif/presentation/sequence_spec.rb index 526f99d..560e93b 100644 --- a/spec/unit/iiif/presentation/sequence_spec.rb +++ b/spec/unit/iiif/presentation/sequence_spec.rb @@ -105,6 +105,7 @@ def initialize(hsh={}) end end - + it_behaves_like 'it has symmetric as_json and to_json methods' + end diff --git a/spec/unit/iiif/service_spec.rb b/spec/unit/iiif/service_spec.rb index be7d1ca..86460b3 100644 --- a/spec/unit/iiif/service_spec.rb +++ b/spec/unit/iiif/service_spec.rb @@ -1,4 +1,6 @@ describe IIIF::Service do + + let(:fixed_values) { {} } describe 'self#get_descendant_class_by_jld_type' do before do @@ -25,4 +27,6 @@ def self.singleton_class? end end + it_behaves_like 'it has symmetric as_json and to_json methods' + end