Class: Intranet::Pictures::JsonDbProvider
- Inherits:
-
Object
- Object
- Intranet::Pictures::JsonDbProvider
- Defined in:
- lib/intranet/pictures/json_db_provider.rb
Overview
Provides pictures data and pictures listings from a database file in JSON format.
Structure of the JSON database
See the example below.
The title of the pictures gallery is indicated in title
Pictures are described individually as a hash in the pictures array. The mandatory keys in this hash are:
-
uri: location of the picture, relative to the JSON file -
title: description of the picture -
datetime: date and time of the picture, as a string in ISO8601 format -
heightandwidth: size in pixels of the picture
Additional keys may be added (for instance: event, city, region/country, …); they will be used to group pictures sharing the same value of a given key. Some groups may be defined in the groups hash to add a thumbnail picture and a brief text description for them. The possible keys in this hash are:
-
id: unique identifier of the group, mandatory -
brief: optional short text associated to the group name -
uri: optional group thumbnail, relative to the JSON file
Instance Method Summary collapse
-
#group_brief(key, value) ⇒ String
Returns the brief text of the group whose type is
keyand namedvalue. -
#group_thumbnail(key, value) ⇒ String, Blob
Returns the thumbnail picture of the group whose type is
keyand namedvalue. -
#initialize(json_file) ⇒ JsonDbProvider
constructor
Initializes a new pictures data provider.
-
#list_pictures(selector = {}, sort_by = nil, asc = true) ⇒ Array<Hash{'title'=>String, 'datetime'=>String, 'height'=>Integer, 'width'=>Integer, ...}>
Returns the list of the pictures matching
selector. -
#picture(selector = {}) ⇒ String, Blob
Returns the picture file matching
selector. -
#title ⇒ String
Returns the pictures gallery title.
Constructor Details
#initialize(json_file) ⇒ JsonDbProvider
Initializes a new pictures data provider.
51 52 53 54 55 56 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 51 def initialize(json_file) @json_dir = File.dirname(json_file) # also works for URLs @json_file = json_file @json_time = Time.at(0) load_json end |
Instance Method Details
#group_brief(key, value) ⇒ String
Returns the brief text of the group whose type is key and named value.
124 125 126 127 128 129 130 131 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 124 def group_brief(key, value) load_json group = select_group(key, value) raise KeyError unless group.size == 1 return '' if group.first['brief'].nil? group.first.fetch('brief') end |
#group_thumbnail(key, value) ⇒ String, Blob
Returns the thumbnail picture of the group whose type is key and named value.
108 109 110 111 112 113 114 115 116 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 108 def group_thumbnail(key, value) load_json group = select_group(key, value) raise KeyError unless group.size == 1 return nil if group.first['uri'].nil? path = File.join(@json_dir, group.first.fetch('uri')) read_image_file(path) end |
#list_pictures(selector = {}, sort_by = nil, asc = true) ⇒ Array<Hash{'title'=>String, 'datetime'=>String, 'height'=>Integer, 'width'=>Integer, ...}>
Returns the list of the pictures matching selector. Results are returned ordered by sort_by in ascending order if asc, and in descending order otherwise.
78 79 80 81 82 83 84 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 78 def list_pictures(selector = {}, sort_by = nil, asc = true) load_json pics = select_pictures(selector).map { |p| p.except('uri') } pics.sort_by! { |p| p.fetch(sort_by) } unless sort_by.nil? pics.reverse! unless asc pics end |
#picture(selector = {}) ⇒ String, Blob
Returns the picture file matching selector.
92 93 94 95 96 97 98 99 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 92 def picture(selector = {}) load_json pic = select_pictures(selector) raise KeyError unless pic.size == 1 path = File.join(@json_dir, pic.first.fetch('uri')) read_image_file(path) end |
#title ⇒ String
Returns the pictures gallery title.
61 62 63 64 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 61 def title load_json @json.fetch('title').to_s end |