Class: DropboxApi::Metadata::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dropbox_api/metadata/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata) ⇒ Base

Takes in a hash containing all the attributes required to initialize the object.

Each hash entry should have a key which identifies a field and its value, so a valid call would be something like this:

DropboxApi::Metadata::File.new({
  "name" => "a.jpg",
  "path_lower" => "/a.jpg",
  "path_display" => "/a.jpg",
  "id" => "id:evvfE6q6cK0AAAAAAAAB2w",
  "client_modified" => "2016-10-19T17:17:34Z",
  "server_modified" => "2016-10-19T17:17:34Z",
  "rev" => "28924061bdd",
  "size" => 396317
})

Parameters:

  • metadata (Hash)

Raises:

  • (ArgumentError)

    If a required attribute is missing.


33
34
35
36
37
# File 'lib/dropbox_api/metadata/base.rb', line 33

def initialize()
  self.class.fields.keys.each do |field_name|
    self[field_name] = [field_name.to_s]
  end
end

Class Attribute Details

.fieldsObject (readonly)

Returns the value of attribute fields


4
5
6
# File 'lib/dropbox_api/metadata/base.rb', line 4

def fields
  @fields
end

Class Method Details

.field(name, type, *options) ⇒ Object


6
7
8
9
10
11
# File 'lib/dropbox_api/metadata/base.rb', line 6

def field(name, type, *options)
  @fields ||= {}
  @fields[name] = DropboxApi::Metadata::Field.new(type, options)

  attr_reader name
end

Instance Method Details

#serialized_field(field_name) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dropbox_api/metadata/base.rb', line 45

def serialized_field(field_name)
  value = send field_name
  case value
  when Time
    value.utc.strftime("%FT%TZ")
  when DropboxApi::Metadata::Base
    value.to_hash
  else
    value
  end
end

#to_hashObject


39
40
41
42
43
# File 'lib/dropbox_api/metadata/base.rb', line 39

def to_hash
  Hash[self.class.fields.keys.map do |field_name|
    [field_name.to_s, serialized_field(field_name)]
  end.select { |k, v| !v.nil? }]
end