Class: IiifUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/iiif_url.rb,
lib/iiif_url/version.rb

Constant Summary collapse

VERSION =
"0.0.2"
@@base_url =
""

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ IiifUrl

Returns a new instance of IiifUrl.



7
8
9
# File 'lib/iiif_url.rb', line 7

def initialize(params={})
  @params = params
end

Class Method Details

.from_params(params = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
# File 'lib/iiif_url.rb', line 49

def self.from_params(params={})
  base_url = params[:base_url]
  if base_url == false
    base_url = ''
  elsif base_url.nil?
    base_url = @@base_url
  end

  region = params[:region] || "full"
  if region.is_a? Hash
    if region[:x]
      region = "#{region[:x]},#{region[:y]},#{region[:w]},#{region[:h]}"
    elsif region[:pctx]
      region = "pct:#{region[:pctx]},#{region[:pcty]},#{region[:pctw]},#{region[:pcth]}"
    end
  end

  size = params[:size] || "full"
  if size.is_a? Hash
    if size[:w] || size[:h]
      size = "#{size[:w]},#{size[:h]}"
    elsif size[:pct]
      size = "pct:#{size[:pct]}"
    end
  end

  rotation = params[:rotation] || 0
  if rotation.is_a? Hash
    if rotation[:mirror]
      rotation = "!#{rotation[:degrees]}"
    else
      rotation = "#{rotation[:degrees]}"
    end
  end

  quality = params[:quality] || "default"
  format = params[:format] || "jpg"

  path = "/#{region}/#{size}/#{rotation}/#{quality}.#{format}"
  if params[:identifier]
    File.join(base_url, params[:identifier], path)
  else
    path
  end
end

.parse(url) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/iiif_url.rb', line 95

def self.parse(url)
  url_parts = url.split('/')
  quality_format = url_parts.pop
  quality, format = quality_format.split('.')

  rotation_string = url_parts.pop
  rotation = if rotation_string.include?('!')
    degrees = rotation_string.sub('!', '')
    {degrees: degrees.to_i, mirror: true}
  else
    {degrees: rotation_string.to_i, mirror: false}
  end

  size_string = url_parts.pop
  size = if size_string.include?(',')
    w, h = size_string.split(',')
    w = w.to_i if !w.nil?
    h = h.to_i if !h.nil?
    {w: w, h: h}
  elsif size_string.include?('pct')
    pct, size = size_string.split(':')
    {pct: size.to_f}
  else
    size_string
  end

  region_string = url_parts.pop
  region = if region_string.include?(',')
    if region_string.include?('pct')
      pctx, pcty, pctw, pcth = region_string.split(',')
      pct, pctx = pctx.split(':')
      {pctx: pctx.to_f, pcty: pcty.to_f, pctw: pctw.to_f, pcth: pcth.to_f}
    else
      x, y, w, h = region_string.split(',')
      {x: x.to_i, y: y.to_i, w: w.to_i, h: h.to_i}
    end
  else
    region_string
  end

  identifier = url_parts.pop
  identifier = nil if identifier == ''

  {
    identifier: identifier,
    region: region,
    size: size,
    rotation: rotation,
    quality: quality,
    format: format
  }
end

.set_base_url(base_url) ⇒ Object



45
46
47
# File 'lib/iiif_url.rb', line 45

def self.set_base_url(base_url)
  @@base_url = base_url
end

Instance Method Details

#format(format) ⇒ Object



36
37
38
39
# File 'lib/iiif_url.rb', line 36

def format(format)
  @params[:format] = format
  self
end

#identifier(identifier) ⇒ Object



11
12
13
14
# File 'lib/iiif_url.rb', line 11

def identifier(identifier)
  @params[:identifier] = identifier
  self
end

#quality(quality) ⇒ Object



31
32
33
34
# File 'lib/iiif_url.rb', line 31

def quality(quality)
  @params[:quality] = quality
  self
end

#region(region) ⇒ Object



16
17
18
19
# File 'lib/iiif_url.rb', line 16

def region(region)
  @params[:region] = region
  self
end

#rotation(rotation) ⇒ Object



26
27
28
29
# File 'lib/iiif_url.rb', line 26

def rotation(rotation)
  @params[:rotation] = rotation
  self
end

#size(size) ⇒ Object



21
22
23
24
# File 'lib/iiif_url.rb', line 21

def size(size)
  @params[:size] = size
  self
end

#to_sObject



41
42
43
# File 'lib/iiif_url.rb', line 41

def to_s
  IiifUrl.from_params(@params)
end