Class: Intranet::Pictures::JsonDbProvider

Inherits:
Object
  • Object
show all
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

  • height and width : 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

Examples:

Structure of the JSON database (not all mandatory are present for readability)

{
  "title": "gallery title",
  "groups": {
    "event": [
      { "id": "Party", "brief": "...", "uri": "party.jpg", ... },
      { ... }
    ],
    "city": [
      { "id": "Houston", "uri": "houston.png", ... },
      { ... }
    ]
  },
  "pictures": [
    { "uri": "dir/pic0.jpg", "datetime": "...", "event": "Party", "city": "Houston", ... },
    { ... }
  ]
}

Instance Method Summary collapse

Constructor Details

#initialize(json_file) ⇒ JsonDbProvider

Initializes a new pictures data provider.

Parameters:

  • json_file (String)

    The path to the JSON database file.



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.

Parameters:

  • key (String)

    The group type.

  • value (String)

    The group name.

Returns:

  • (String)

    The brief text of the group, or an empty string if no brief is available.

Raises:

  • KeyError If JSON file is malformed or if key/value do not designate exactly one group.



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.

Parameters:

  • key (String)

    The group type.

  • value (String)

    The group name.

Returns:

  • (String, Blob)

    The MIME type of the picture, and the picture file content. Nil may be returned if no thumbnail is available for that group.

Raises:

  • KeyError If JSON file is malformed, if key/value do not designate exactly one group, or if the group uri does not refer to an existing image file.



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.

Parameters:

  • selector (Hash<String,String>) (defaults to: {})

    The pictures selector, interpreted as a logical AND combination of all key/value pairs provided.

  • sort_by (String) (defaults to: nil)

    The picture field to sort the results by, or nil if results should be returned without particular sorting.

  • asc (Boolean) (defaults to: true)

    True to sort returned pictures in ascending order, False to sort them in descending order.

Returns:

  • (Array<Hash{'title'=>String, 'datetime'=>String, 'height'=>Integer, 'width'=>Integer, ...}>)

    The selected pictures.

Raises:

  • KeyError If JSON file is malformed, or if sort_by is not an existing picture field.



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.

Parameters:

  • selector (Hash<String,String>) (defaults to: {})

    The picture selector, which should return exactly one picture.

Returns:

  • (String, Blob)

    The MIME type of the picture, and the picture file content.

Raises:

  • KeyError If JSON file is malformed, if selector does not match exactly one picture, or if the picture uri does not refer to an existing image file.



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

#titleString

Returns the pictures gallery title.

Returns:

  • (String)

    The gallery title.

Raises:

  • KeyError If no title is defined in JSON file.



61
62
63
64
# File 'lib/intranet/pictures/json_db_provider.rb', line 61

def title
  load_json
  @json.fetch('title').to_s
end