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.
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 (string)title: description of the picture (string)datetime: date and time of the picture, in ISO8601 format (string)heightandwidth: size in pixels of the picture (integers)
Additional keys may be added (for instance: event, city, region/country, ...); they will be used to select pictures sharing the same value of a given key, or to group them. Values may either be singular string elements (for instance: "My wonderful event") or array of strings (for instance: [ "Casino", "Las Vegas", "Urban photography" ]).
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 (string)brief: optional short text associated to the group name (string)uri: optional group thumbnail, relative to the JSON file (string)
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 the given selectors.
-
#picture(selector = {}) ⇒ String, Blob
Returns the picture file matching
selector.
Constructor Details
#initialize(json_file) ⇒ JsonDbProvider
Initializes a new pictures data provider.
52 53 54 55 56 57 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 52 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.
129 130 131 132 133 134 135 136 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 129 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.
113 114 115 116 117 118 119 120 121 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 113 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 the given selectors. Selectors are key/value strings pairs restricting the pictures to be returned:
- if a picture has the given key, its value is a singular string and is strictly equal
to the given value, it is returned;
- if a picture has the given key, its value is an array of strings and ONE of the
elements of this array is equal to the given value, it is returned;
- all other pictures are not returned.
Results are sorted according to the value of the key sort_by, in ascending order if
asc, and in descending order otherwise. Sorting by a key whose value is not a singular
string is not defined.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 79 def list_pictures(selector = {}, sort_by = nil, asc = true) load_json pics = select_pictures(selector).map { |p| p.except('uri') } unless sort_by.nil? raise KeyError if pics.any? { |p| p.fetch(sort_by).respond_to?('any?') } pics.sort_by! { |p| p.fetch(sort_by) } end pics.reverse! unless asc pics end |
#picture(selector = {}) ⇒ String, Blob
Returns the picture file matching selector.
97 98 99 100 101 102 103 104 |
# File 'lib/intranet/pictures/json_db_provider.rb', line 97 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 |