iiif_url 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0619253ce49f6b4de78e3743e69d2507ce5ae99d
4
+ data.tar.gz: c7c71e6ec3ddb6d6c349590e129ceb87f7e5f58e
5
+ SHA512:
6
+ metadata.gz: 988c43d7edaa08efd7470329c58ebd2d2ecdedc2a9fa0f715d5b4cec2b2ab4cdadba01117e7361ae143004334bcc68af6a36ea56d6fd5e564a2b2c98c2f1cdc6
7
+ data.tar.gz: feec5e94df362c77e59a61f32a828c00ffd75be4b9b555a16b221894d4e78c6d7b43cdbebde73f63cddf5742a83d8bd1d96d2826ee258b5baa876d08ec4548d4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in iiif_url.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 North Carolina State University
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,257 @@
1
+ # IIIF URL
2
+
3
+ Create and parse IIIF Image API URLs
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'iiif_url'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install iiif_url
20
+
21
+ ## Usage
22
+
23
+ Here's the simplest case of creating a IIIF URL with all options. By default only the path is given without the scheme, server, port, or IIIF prefix.
24
+
25
+ ```ruby
26
+ iiif_base_url = "http://example.edu/prefix"
27
+ options = {
28
+ identifier: 'abc',
29
+ region: 'full',
30
+ size: 'full',
31
+ rotation: 0,
32
+ quality: 'default',
33
+ format: 'jpg'
34
+ }
35
+ url = IiifUrl.from_options(options)
36
+ # => "/abc/full/full/0/default.jpg"
37
+ full_url = File.join(IIIF_BASE_URL, url)
38
+ # => "http://example.edu/prefix/full/full/0/default.jpg"
39
+ ```
40
+
41
+ If the constant `IIIF_URL_BASE_URL` is defined then it will form a full url automatically:
42
+
43
+ ```ruby
44
+ IiifUrl.set_base_url("http://example.edu/prefix")
45
+ options = {
46
+ identifier: 'abc',
47
+ region: 'full',
48
+ size: 'full',
49
+ rotation: 0,
50
+ quality: 'default',
51
+ format: 'jpg'
52
+ }
53
+ url = IiifUrl.from_options(options)
54
+ # => "http://example.edu/prefix/abc/full/full/0/default.jpg"
55
+ ```
56
+
57
+ You can also pass in the base URL in with the options, which will override any value set for the base URL.
58
+
59
+ ```ruby
60
+ IiifUrl.set_base_url("http://example.edu/prefix")
61
+ options = {
62
+ identifier: 'abc',
63
+ base_url: "http://example.org",
64
+ region: 'full',
65
+ size: 'full',
66
+ rotation: 0,
67
+ quality: 'default',
68
+ format: 'jpg'
69
+ }
70
+ url = IiifUrl.from_options(options)
71
+ # => "http://example.org/abc/full/full/0/default.jpg"
72
+ ```
73
+
74
+ If the base URL is set you can prevent it being used and just return the path portion by setting the `base_url` option key to `false`:
75
+
76
+ ```ruby
77
+ IiifUrl.set_base_url("http://example.edu/prefix")
78
+ options = {
79
+ identifier: 'abc',
80
+ base_url: false,
81
+ region: 'full',
82
+ size: 'full',
83
+ rotation: 0,
84
+ quality: 'default',
85
+ format: 'jpg'
86
+ }
87
+ url = IiifUrl.from_options(options)
88
+ # => "/abc/full/full/0/default.jpg"
89
+ ```
90
+
91
+ A more complicated region and size:
92
+
93
+ ```ruby
94
+ options = {
95
+ identifier: 'abc',
96
+ region: {
97
+ x: 0,
98
+ y: 0,
99
+ w: 1000,
100
+ h: 1200
101
+ },
102
+ size: {w: 300},
103
+ rotation: 0,
104
+ quality: 'default',
105
+ format: 'jpg'
106
+ }
107
+ url = IiifUrl.from_options(options)
108
+ # => "/abc/0,0,1000,1200/300,/0/default.jpg"
109
+ ```
110
+
111
+ To use a percent region or percent size, you must prefix the keys like this:
112
+
113
+ ```ruby
114
+ options = {
115
+ identifier: 'abc',
116
+ region: {
117
+ pctx: 10,
118
+ pcty: 10,
119
+ pctw: 80,
120
+ pcth: 80
121
+ },
122
+ size: {pct: 50}
123
+ }
124
+ url = IiifUrl.from_options(options)
125
+ # => "/abc/pct:10,10,80,80/pct:50/0/default.jpg"
126
+ ```
127
+
128
+ If no identifier is passed in, then only the IIIF URL path will be returned:
129
+
130
+ ```ruby
131
+ options = {
132
+ size: {pct: 50}
133
+ }
134
+ url = IiifUrl.from_options(options)
135
+ # => "/full/pct:50/0/default.jpg"
136
+ ```
137
+
138
+ Even if a base_url is given if there is no identifier, then only the IIIF URL path will be returned:
139
+
140
+ ```ruby
141
+ options = {
142
+ base_url: "http://example.org/prefix/",
143
+ size: {pct: 50}
144
+ }
145
+ url = IiifUrl.from_options(options)
146
+ # => "/full/pct:50/0/default.jpg"
147
+ ```
148
+
149
+
150
+ ## Defaults
151
+
152
+ You only need to specify values that are different from the defaults. The defaults are as specified:
153
+
154
+ | parameter | value |
155
+ |:-----------|:----------|
156
+ | identifier | null |
157
+ | region | "full" |
158
+ | size | "full" |
159
+ | rotation | "0" |
160
+ | quality | "default" |
161
+ | format | "jpg" |
162
+
163
+ ```ruby
164
+ options = {
165
+ identifier: 'abc',
166
+ size: {w: 600}
167
+ }
168
+ url = IiifUrl.from_options(options)
169
+ # => "/abc/full/600,/0/default.jpg"
170
+ ```
171
+
172
+ And without an identifier:
173
+
174
+ ```ruby
175
+ options = {
176
+ size: {w: 600}
177
+ }
178
+ url = IiifUrl.from_options(options)
179
+ # => "/full/600,/0/default.jpg"
180
+ ```
181
+
182
+ ## Chainable
183
+
184
+ There may be cases where you do not have all of the options you need so you want to pass around a IiifUrl to add more options.
185
+
186
+ ```ruby
187
+ url = IiifUrl.new
188
+ url.region({x:100, y:200, w: 300, h: 300})
189
+ url.size({w: 150}).format('png')
190
+ url.to_s
191
+ # => "/100,200,300,300/150,/0/default.png"
192
+ ```
193
+
194
+ You can also pass in some initial options and then add on others:
195
+
196
+ ```ruby
197
+ url = IiifUrl.new({size: {w: 100}})
198
+ url.identifier('abc')
199
+ url.format('png')
200
+ url.rotation(180)
201
+ url.to_s
202
+ # => "/abc/full/100,/180/default.png"
203
+ ```
204
+
205
+ ## Parser
206
+
207
+ IIIF URLs are parsed by segments including: region, size, rotation, quality, and format. The region and size segments can also be parsed into a string or hash. Rotation is always parsed into a hash. Quality and format are always a string.
208
+
209
+ Simple case for region and size parsed into strings:
210
+
211
+ ```ruby
212
+ options = IiifUrl.parse("/full/full/0/default.png")
213
+ # => {region: "full", size: "full", rotation: {degrees: 0, mirror: false}, quality: 'default', format: 'png'}
214
+ ```
215
+
216
+ With an identifier:
217
+
218
+ ```ruby
219
+ options = IiifUrl.parse("/abc/full/full/0/default.png")
220
+ # => {identifier: "abc", region: "full", size: "full", rotation: {degrees: 0, mirror: false}, quality: 'default', format: 'png'}
221
+ ```
222
+
223
+ Parameterized region and size:
224
+
225
+ ```ruby
226
+ options = IiifUrl.parse("/0,100,200,300/75,/0/default.jpg")
227
+ # => {identifier: nil, region: {x:0, y:100, w: 200, h: 300}, size: {w: 75, h: nil}, rotation: {degrees: 0, mirror: false}, quality: "default", format: "jpg" }
228
+ ```
229
+
230
+ Parse a full URL:
231
+
232
+ ```ruby
233
+ options = IiifUrl.parse("http://example.org/prefix/abc/0,100,200,300/75,/0/default.jpg")
234
+ # => {identifier: abc, region: {x:0, y:100, w: 200, h: 300}, size: {w: 75, h: nil}, rotation: {degrees: 0, mirror: false}, quality: "default", format: "jpg" }
235
+ ```
236
+
237
+ ## Validation
238
+
239
+ No validation is done for creating or parsing a URL. This allows for extensions to the IIIF Image API.
240
+
241
+ ## Development
242
+
243
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
244
+
245
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
246
+
247
+ ## Contributing
248
+
249
+ Bug reports and pull requests are welcome on GitHub at https://github.com/NCSU-Libraries/iiif_url.
250
+
251
+ ## Authors
252
+
253
+ Jason Ronallo
254
+
255
+ ## License
256
+
257
+ The gem is copyright North Carolina State University and is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "iiif_url"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/iiif_url.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iiif_url/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "iiif_url"
8
+ spec.version = IiifUrl::VERSION
9
+ spec.authors = ["Jason Ronallo"]
10
+ spec.email = ["jronallo@gmail.com"]
11
+
12
+ spec.summary = %q{Create and parse IIIF Image API URLs}
13
+ spec.description = %q{Create IIIF Image API URLs from parameters; parse IIIF Image API URLs into parameters.}
14
+ spec.homepage = "https://github.com/NCSU-Libraries/iiif_url"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "colorize"
26
+ end
data/lib/iiif_url.rb ADDED
@@ -0,0 +1,147 @@
1
+ require "iiif_url/version"
2
+
3
+ class IiifUrl
4
+
5
+ @@base_url = ""
6
+
7
+ def initialize(options={})
8
+ @options = options
9
+ end
10
+
11
+ def identifier(identifier)
12
+ @options[:identifier] = identifier
13
+ self
14
+ end
15
+
16
+ def region(region)
17
+ @options[:region] = region
18
+ self
19
+ end
20
+
21
+ def size(size)
22
+ @options[:size] = size
23
+ self
24
+ end
25
+
26
+ def rotation(rotation)
27
+ @options[:rotation] = rotation
28
+ self
29
+ end
30
+
31
+ def quality(quality)
32
+ @options[:quality] = quality
33
+ self
34
+ end
35
+
36
+ def format(format)
37
+ @options[:format] = format
38
+ self
39
+ end
40
+
41
+ def to_s
42
+ IiifUrl.from_options(@options)
43
+ end
44
+
45
+ def self.set_base_url(base_url)
46
+ @@base_url = base_url
47
+ end
48
+
49
+ def self.from_options(options={})
50
+ base_url = options[:base_url]
51
+ if base_url == false
52
+ base_url = ''
53
+ elsif base_url.nil?
54
+ base_url = @@base_url
55
+ end
56
+
57
+ region = options[:region] || "full"
58
+ if region.is_a? Hash
59
+ if region[:x]
60
+ region = "#{region[:x]},#{region[:y]},#{region[:w]},#{region[:h]}"
61
+ elsif region[:pctx]
62
+ region = "pct:#{region[:pctx]},#{region[:pcty]},#{region[:pctw]},#{region[:pcth]}"
63
+ end
64
+ end
65
+
66
+ size = options[:size] || "full"
67
+ if size.is_a? Hash
68
+ if size[:w] || size[:h]
69
+ size = "#{size[:w]},#{size[:h]}"
70
+ elsif size[:pct]
71
+ size = "pct:#{size[:pct]}"
72
+ end
73
+ end
74
+
75
+ rotation = options[:rotation] || 0
76
+ if rotation.is_a? Hash
77
+ if rotation[:mirror]
78
+ rotation = "!#{rotation[:degrees]}"
79
+ else
80
+ rotation = "#{rotation[:degrees]}"
81
+ end
82
+ end
83
+
84
+ quality = options[:quality] || "default"
85
+ format = options[:format] || "jpg"
86
+
87
+ path = "/#{region}/#{size}/#{rotation}/#{quality}.#{format}"
88
+ if options[:identifier]
89
+ File.join(base_url, options[:identifier], path)
90
+ else
91
+ path
92
+ end
93
+ end
94
+
95
+ def self.parse(url)
96
+ url_parts = url.split('/')
97
+ quality_format = url_parts.pop
98
+ quality, format = quality_format.split('.')
99
+
100
+ rotation_string = url_parts.pop
101
+ rotation = if rotation_string.include?('!')
102
+ degrees = rotation_string.sub('!', '')
103
+ {degrees: degrees.to_i, mirror: true}
104
+ else
105
+ {degrees: rotation_string.to_i, mirror: false}
106
+ end
107
+
108
+ size_string = url_parts.pop
109
+ size = if size_string.include?(',')
110
+ w, h = size_string.split(',')
111
+ w = w.to_i if !w.nil?
112
+ h = h.to_i if !h.nil?
113
+ {w: w, h: h}
114
+ elsif size_string.include?('pct')
115
+ pct, size = size_string.split(':')
116
+ {pct: size.to_f}
117
+ else
118
+ size_string
119
+ end
120
+
121
+ region_string = url_parts.pop
122
+ region = if region_string.include?(',')
123
+ if region_string.include?('pct')
124
+ pctx, pcty, pctw, pcth = region_string.split(',')
125
+ pct, pctx = pctx.split(':')
126
+ {pctx: pctx.to_f, pcty: pcty.to_f, pctw: pctw.to_f, pcth: pcth.to_f}
127
+ else
128
+ x, y, w, h = region_string.split(',')
129
+ {x: x.to_i, y: y.to_i, w: w.to_i, h: h.to_i}
130
+ end
131
+ else
132
+ region_string
133
+ end
134
+
135
+ identifier = url_parts.pop
136
+ identifier = nil if identifier == ''
137
+
138
+ {
139
+ identifier: identifier,
140
+ region: region,
141
+ size: size,
142
+ rotation: rotation,
143
+ quality: quality,
144
+ format: format
145
+ }
146
+ end
147
+ end
@@ -0,0 +1,3 @@
1
+ class IiifUrl
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iiif_url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason Ronallo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Create IIIF Image API URLs from parameters; parse IIIF Image API URLs
70
+ into parameters.
71
+ email:
72
+ - jronallo@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - MIT-LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - iiif_url.gemspec
86
+ - lib/iiif_url.rb
87
+ - lib/iiif_url/version.rb
88
+ homepage: https://github.com/NCSU-Libraries/iiif_url
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.8
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Create and parse IIIF Image API URLs
112
+ test_files: []