Class: IIIF::Presentation::ImageResource

Inherits:
Resource show all
Defined in:
lib/iiif/presentation/image_resource.rb

Constant Summary collapse

TYPE =
'dctypes:Image'
IMAGE_API_DEFAULT_PARAMS =
'/full/!200,200/0/default.jpg'
IMAGE_API_CONTEXT =
'http://iiif.io/api/image/2/context.json'
DEFAULT_FORMAT =
'image/jpeg'

Constants included from HashBehaviours

HashBehaviours::SIMPLE_SELF_RETURNERS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#required_keys, #string_only_keys

Methods inherited from AbstractResource

#abstract_resource_only_keys, #any_type_keys, #array_only_keys, #hash_only_keys, #legal_viewing_direction_values, #legal_viewing_hint_values, #required_keys, #string_only_keys, #to_ordered_hash

Methods inherited from Service

#abstract_resource_only_keys, #any_type_keys, #array_only_keys, from_ordered_hash, #hash_only_keys, parse, #required_keys, #string_only_keys, #to_json, #to_ordered_hash, #validate

Methods included from HashBehaviours

#clear, #merge, #merge!, #reject!, #select, #select!

Constructor Details

#initialize(hsh = {}) ⇒ ImageResource

Returns a new instance of ImageResource.



15
16
17
18
# File 'lib/iiif/presentation/image_resource.rb', line 15

def initialize(hsh={})
  hsh['@type'] = 'dcterms:Image' unless hsh.has_key? '@type'
  super(hsh)
end

Class Method Details

.create_image_api_image_resource(params = {}) ⇒ Object

Create a new ImageResource that includes a IIIF Image API Service See iiif.io/api/presentation/2.0/#image-resources

Params

* :service_id (required) - The base URI for the image on the image 
    server.
* :resource_id - The id for the resource; if supplied this should 
    resolve to an actual image. Default: 
    "#{:service_id}/full/!200,200/0/default.jpg"
* :format - The format of the image that is returned when 
     `:resource_id` is resolved. Default: 'image/jpeg'
* :height (Integer)
* :profile (String)
* :width (Integer) - If width, height, and profile are not supplied, 
    this method will try to get the info from the server (based on 
    :resource_id) and raise an Exception if this is not possible for
     some reason.
* :copy_info (bool)- Even if width and height are supplied, try to 
    get the info.json from the server and copy it in. Default: false

Raises:

* KeyError if `:service_id` is not supplied
* Expections related to HTTP problems if a call to an image server fails

The result is something like this:

{

"@id":"http://www.example.org/iiif/book1/res/page1.jpg",
"@type":"dctypes:Image",
"format":"image/jpeg",
"service": {
  "@context": "http://iiif.io/api/image/2/context.json",
  "@id":"http://www.example.org/images/book1-page1",
  "profile":"http://iiif.io/api/image/2/profiles/level2.json",
},
"height":2000,
"width":1500

}



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/iiif/presentation/image_resource.rb', line 63

def create_image_api_image_resource(params={})

  service_id = params.fetch(:service_id)
  resource_id_default = "#{service_id}#{IMAGE_API_DEFAULT_PARAMS}"
  resource_id = params.fetch(:resource_id, resource_id_default)
  format = params.fetch(:format, DEFAULT_FORMAT)
  height = params.fetch(:height, nil)
  profile = params.fetch(:profile, nil)
  width = params.fetch(:width, nil)
  copy_info = params.fetch(:copy_info, false)
  
  have_whp = [width, height, profile].all? { |prop| !prop.nil? }

  remote_info = get_info(service_id) if !have_whp || copy_info

  resource = self.new
  resource['@id'] = resource_id
  resource.format = format
  resource.width = width.nil? ? remote_info['width'] : width
  resource.height = height.nil? ? remote_info['height'] : height
  resource.service = Service.new
  if copy_info
    resource.service.merge!(remote_info)
  else
    resource.service['@context'] = IMAGE_API_CONTEXT
    resource.service['@id'] = service_id
    if profile.nil?
      if remote_info['profile'].kind_of?(Array)
        resource.service['profile'] = remote_info['profile'][0]
      else
        resource.service['profile'] = remote_info['profile']
      end
    else
      resource.service['profile'] = profile
    end
  end
  return resource
end

Instance Method Details

#int_only_keysObject



11
12
13
# File 'lib/iiif/presentation/image_resource.rb', line 11

def int_only_keys
  super + %w{ width height }
end