Class: Visor::Image::Meta

Inherits:
Object
  • Object
show all
Includes:
Common::Exception
Defined in:
lib/image/meta.rb

Overview

The API for the VISoR Meta Server. This class supports all image metadata manipulation operations.

This is the entry-point for the VISoR Image Server to communicate with the VISoR Meta Server, here are processed and logged all the calls to the meta server coming from it.

Constant Summary collapse

DEFAULT_HOST =
'0.0.0.0'
DEFAULT_PORT =
4567

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Meta

Initializes a new new VISoR Meta Image.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :host (String) — default: DEFAULT_HOST

    The host address where VISoR meta server resides.

  • :port (String) — default: DEFAULT_PORT

    The host port where VISoR meta server resides.

  • :ssl (String) — default: false

    If the connection should be made through HTTPS (SSL).



27
28
29
30
31
# File 'lib/image/meta.rb', line 27

def initialize(opts = {})
  @host = opts[:host] || DEFAULT_HOST
  @port = opts[:port] || DEFAULT_PORT
  @ssl  = opts[:ssl] || false
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



19
20
21
# File 'lib/image/meta.rb', line 19

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



19
20
21
# File 'lib/image/meta.rb', line 19

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



19
20
21
# File 'lib/image/meta.rb', line 19

def ssl
  @ssl
end

Instance Method Details

#delete_image(id) ⇒ Hash

Removes an image record based on its _id and returns its metadata.

Parameters:

  • id (String)

    The image’s _id which will be deleted.

Returns:

  • (Hash)

    The already deleted image metadata. This is useful for recover on accidental delete.

Raises:

  • (NotFound)

    If required image was not found.



142
143
144
145
# File 'lib/image/meta.rb', line 142

def delete_image(id)
  http = request.delete path: "/images/#{id}", head: delete_headers
  return_response(http)
end

#get_image(id) ⇒ Hash

Retrieves detailed image metadata of the image with the given id.

Parameters:

  • id (String)

    The wanted image’s _id.

Returns:

  • (Hash)

    The requested image metadata.

Raises:

  • (NotFound)

    If image not found.



99
100
101
102
# File 'lib/image/meta.rb', line 99

def get_image(id)
  http = request.get path: "/images/#{id}", head: get_headers
  return_response(http)
end

#get_images(query = {}, owner = nil) ⇒ Array

Retrieves brief metadata of all public images. Options for filtering the returned results can be passed in.

Parameters:

  • owner (nil) (defaults to: nil)
    String

    The user’s access key to look for its private images too.

  • query (Hash) (defaults to: {})

    a customizable set of options

Options Hash (query):

  • : (String)

    The image attribute value to filter returned results.

  • :sort (String) — default: "_id"

    The image attribute to sort returned results.

  • :dir (String) — default: "asc"

    The direction to sort results (“asc”/“desc”).

Returns:

  • (Array)

    All public images brief metadata. Just BRIEF fields are returned.

Raises:

  • (NotFound)

    If there are no public images registered on the server.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/image/meta.rb', line 46

def get_images(query = {}, owner=nil)
  http    = request.get path: '/images', query: query.merge({access: 'public'}), head: get_headers
  pub = return_response(http)
  priv = []
  if owner
    http = request.get path: '/images', query: query.merge({access: 'private', owner: owner}), head: get_headers
    begin
      priv = return_response(http)
    rescue => e
      nil
    end
  end
  pub + priv
end

#get_images_detail(query = {}, owner = nil) ⇒ Array

Retrieves detailed metadata of all public images.

Filtering and querying works the same as with #get_images. The only difference is the number of disclosed attributes.

Parameters:

  • owner (nil) (defaults to: nil)
    String

    The user’s access_key to look for its private images too.

  • query (Hash) (defaults to: {})

    a customizable set of options

Options Hash (query):

  • :attribute_name (String)

    The image attribute value to filter returned results.

  • :sort (String) — default: "_id"

    The image attribute to sort returned results.

  • :dir (String) — default: "asc"

    The direction to sort results (“asc”/“desc”).

Returns:

  • (Array)

    All public images detailed metadata. The DETAIL_EXC fields are excluded from results.

Raises:

  • (NotFound)

    If there are no public images registered on the server.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/image/meta.rb', line 76

def get_images_detail(query = {}, owner=nil)
  http = request.get path: '/images/detail', query: query, head: get_headers
  pub = return_response(http)
  priv = []
  if owner
    http = request.get path: '/images/detail', query: query.merge({access: 'private', owner: owner}), head: get_headers
    begin
      priv = return_response(http)
    rescue => e
      nil
    end
  end
  pub + priv
end

#post_image(meta) ⇒ Hash

Register a new image on the server with the given metadata and returns its metadata.

Parameters:

  • meta (Hash)

    The image metadata.

Returns:

  • (Hash)

    The already inserted image metadata.

Raises:

  • (Invalid)

    If image meta validation fails.



112
113
114
115
116
# File 'lib/image/meta.rb', line 112

def post_image(meta)
  body = prepare_body(meta)
  http = request.post path: '/images', body: body, head: post_headers
  return_response(http)
end

#put_image(id, meta) ⇒ Hash

Updates an image record with the given metadata and returns its metadata.

Parameters:

  • id (String)

    The image’s _id which will be updated.

  • meta (Hash)

    The image metadata.

Returns:

  • (Hash)

    The already updated image metadata.

Raises:

  • (Invalid)

    If image meta validation fails.

  • (NotFound)

    If required image was not found.



128
129
130
131
132
# File 'lib/image/meta.rb', line 128

def put_image(id, meta)
  body = prepare_body(meta)
  http = request.put path: "/images/#{id}", body: body, head: put_headers
  return_response(http)
end