Class: Aperitiiif::Asset
- Inherits:
-
Object
- Object
- Aperitiiif::Asset
- Defined in:
- lib/aperitiiif/asset.rb
Overview
to do has smell :reek:TooManyMethods
Constant Summary collapse
- TARGET_EXT =
'.jpg'
Instance Attribute Summary collapse
-
#parent_id ⇒ Object
readonly
Returns the value of attribute parent_id.
Instance Method Summary collapse
- #annotation ⇒ Object
- #annotation_url ⇒ Object
- #build_id ⇒ Object
- #build_target ⇒ Object
- #canvas ⇒ Object
- #canvas_url ⇒ Object
- #full_resource_url ⇒ Object
- #height ⇒ Object
- #id ⇒ Object
-
#initialize(parent_id, source, config) ⇒ Asset
constructor
A new instance of Asset.
- #resource ⇒ Object
- #service ⇒ Object
- #service_url ⇒ Object
- #source_mime ⇒ Object
- #target ⇒ Object
- #target_mime ⇒ Object
- #target_written? ⇒ Boolean
- #thumbnail_url ⇒ Object
- #validate_config ⇒ Object
- #validate_source ⇒ Object
- #width ⇒ Object
- #write_to_target ⇒ Object
Constructor Details
#initialize(parent_id, source, config) ⇒ Asset
Returns a new instance of Asset.
16 17 18 19 20 21 22 23 |
# File 'lib/aperitiiif/asset.rb', line 16 def initialize(parent_id, source, config) @parent_id = parent_id @source = source @config = config validate_source validate_config end |
Instance Attribute Details
#parent_id ⇒ Object (readonly)
Returns the value of attribute parent_id.
12 13 14 |
# File 'lib/aperitiiif/asset.rb', line 12 def parent_id @parent_id end |
Instance Method Details
#annotation ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/aperitiiif/asset.rb', line 106 def annotation opts = { '@id' => annotation_url, 'on' => canvas_url, 'resource' => resource } IIIF::Presentation::Annotation.new opts end |
#annotation_url ⇒ Object
70 71 72 |
# File 'lib/aperitiiif/asset.rb', line 70 def annotation_url @annotation_url ||= "#{@config.presentation_api_url}/annotation/#{id}.json" end |
#build_id ⇒ Object
82 83 84 85 86 |
# File 'lib/aperitiiif/asset.rb', line 82 def build_id id = Utils.basename_no_ext @source id.prepend "#{@parent_id}_" unless id == @parent_id id.prepend "#{@config.namespace}_" end |
#build_target ⇒ Object
90 |
# File 'lib/aperitiiif/asset.rb', line 90 def build_target = "#{@config.image_build_dir}/#{id}#{TARGET_EXT}" |
#canvas ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/aperitiiif/asset.rb', line 125 def canvas opts = { '@id' => canvas_url, 'label' => id, 'width' => width, 'height' => height, 'thumbnail' => thumbnail_url, 'images' => [annotation] } IIIF::Presentation::Canvas.new opts end |
#canvas_url ⇒ Object
62 63 64 |
# File 'lib/aperitiiif/asset.rb', line 62 def canvas_url @canvas_url ||= "#{@config.presentation_api_url}/canvas/#{id}.json" end |
#full_resource_url ⇒ Object
74 75 76 |
# File 'lib/aperitiiif/asset.rb', line 74 def full_resource_url @full_resource_url ||= "#{@config.image_api_url}/#{id}/full/full/0/default.jpg" end |
#height ⇒ Object
49 50 51 |
# File 'lib/aperitiiif/asset.rb', line 49 def height @height ||= Vips::Image.new_from_file(@source).height end |
#id ⇒ Object
37 38 39 |
# File 'lib/aperitiiif/asset.rb', line 37 def id @id ||= build_id end |
#resource ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/aperitiiif/asset.rb', line 115 def resource opts = { '@id' => full_resource_url, '@type' => 'dcterms:Image', 'service' => service, 'format' => target_mime } IIIF::Presentation::Resource.new opts end |
#service ⇒ Object
99 100 101 102 103 104 |
# File 'lib/aperitiiif/asset.rb', line 99 def service { '@context' => 'http://iiif.io/api/image/2/context.json', '@id' => service_url } end |
#service_url ⇒ Object
78 79 80 |
# File 'lib/aperitiiif/asset.rb', line 78 def service_url @service_url ||= "#{@config.image_api_url}/#{id}" end |
#source_mime ⇒ Object
58 59 60 |
# File 'lib/aperitiiif/asset.rb', line 58 def source_mime @source_mime ||= Utils.mime @source end |
#target ⇒ Object
41 42 43 |
# File 'lib/aperitiiif/asset.rb', line 41 def target @target ||= build_target end |
#target_mime ⇒ Object
53 54 55 56 |
# File 'lib/aperitiiif/asset.rb', line 53 def target_mime write_to_target @target_mime ||= Utils.mime target end |
#target_written? ⇒ Boolean
88 |
# File 'lib/aperitiiif/asset.rb', line 88 def target_written? = File.file? target |
#thumbnail_url ⇒ Object
66 67 68 |
# File 'lib/aperitiiif/asset.rb', line 66 def thumbnail_url @thumbnail_url ||= "#{@config.image_api_url}/#{id}/full/250,/0/default.jpg" end |
#validate_config ⇒ Object
30 31 32 33 34 35 |
# File 'lib/aperitiiif/asset.rb', line 30 def validate_config raise Aperitiiif::Error, "No value found for 'namespace'. Check your config?" if @config.namespace.empty? raise Aperitiiif::Error, "No value found for 'image_build_dir'. Check your config?" if @config.image_build_dir.empty? raise Aperitiiif::Error, "No value found for 'presentation_api_url'. Check your config?" if @config.presentation_api_url.empty? raise Aperitiiif::Error, "No value found for 'image_api_url'. Check your config?" if @config.image_api_url.empty? end |
#validate_source ⇒ Object
25 26 27 28 |
# File 'lib/aperitiiif/asset.rb', line 25 def validate_source raise Aperitiiif::Error, "Asset source #{@source} does not exist" unless File.file? @source raise Aperitiiif::Error, "Asset source #{@source} is not a valid format" unless Utils.valid_source? @source end |
#width ⇒ Object
45 46 47 |
# File 'lib/aperitiiif/asset.rb', line 45 def width @width ||= Vips::Image.new_from_file(@source).width end |
#write_to_target ⇒ Object
92 93 94 95 96 97 |
# File 'lib/aperitiiif/asset.rb', line 92 def write_to_target return false if target_written? FileUtils.mkdir_p @config.image_build_dir Vips::Image.new_from_file(@source).write_to_file target end |