Class: RubyBox::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-box/item.rb

Constant Summary collapse

@@has_many =
[]
@@has_many_paginated =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, raw_item) ⇒ Item

Returns a new instance of Item.


10
11
12
13
# File 'lib/ruby-box/item.rb', line 10

def initialize( session, raw_item )
  @session = session
  @raw_item = raw_item
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby-box/item.rb', line 72

def method_missing(method, *args, &block)
  key = method.to_s

  # Support has many and paginated has many relationships.
  return many(key) if @@has_many.include?(key)
  return paginated(key, args[0] || 100, args[1] || 0, args[2]) if @@has_many_paginated.include?(key)
  
  # update @raw_item hash if this appears to be a setter.
  setter = method.to_s.end_with?('=')
  key = key[0...-1] if setter
  @raw_item[key] = args[0] if setter and update_fields.include?(key)
  
  # we may have a mini version of the object loaded, fix this.
  reload_meta if @raw_item[key].nil? and has_mini_format?

  if @raw_item[key].kind_of?(Hash)
    return RubyBox::Item.factory(@session, @raw_item[key])
  elsif RubyBox::ISO_8601_TEST.match(@raw_item[key].to_s)
    return Time.parse(@raw_item[key])
  else
    return @raw_item[key]
  end
end

Class Method Details

.has_many(*keys) ⇒ Object


15
16
17
# File 'lib/ruby-box/item.rb', line 15

def self.has_many(*keys)
  keys.each {|key| @@has_many << key.to_s}
end

.has_many_paginated(*keys) ⇒ Object


19
20
21
# File 'lib/ruby-box/item.rb', line 19

def self.has_many_paginated(*keys)
  keys.each {|key| @@has_many_paginated << key.to_s}
end

Instance Method Details

#as_json(opts = {}) ⇒ Object


129
130
131
# File 'lib/ruby-box/item.rb', line 129

def as_json(opts={})
  @raw_item
end

#createObject


50
51
52
53
54
55
56
57
58
# File 'lib/ruby-box/item.rb', line 50

def create
  url = "#{RubyBox::API_URL}/#{resource_name}"
  uri = URI.parse(url)
  request = Net::HTTP::Post.new( uri.request_uri )
  request.body = JSON.dump(@raw_item)
  resp = @session.request(uri, request)
  @raw_item = resp
  self
end

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby-box/item.rb', line 98

def create_shared_link(opts={})
  raise UnshareableResource unless ['folder', 'file'].include?(type)

  opts = {
    access: 'open'
  }.merge(opts) if opts

  url = "#{RubyBox::API_URL}/#{resource_name}/#{id}"
  uri = URI.parse(url)

  request = Net::HTTP::Put.new(uri.path, {
    "Content-Type" => 'application/json'
  })

  request.body = JSON.dump({
    shared_link: opts
  })

  @raw_item = @session.request(uri, request)
  self
end

#delete(opts = {}) ⇒ Object


60
61
62
63
64
# File 'lib/ruby-box/item.rb', line 60

def delete(opts={})
  url = "#{RubyBox::API_URL}/#{resource_name}/#{id}"
  url = "#{url}#{Addressable::URI.new(:query_values => opts).to_s}" unless opts.keys.empty?
  resp = @session.delete( url )
end

120
121
122
# File 'lib/ruby-box/item.rb', line 120

def disable_shared_link(opts={})
  create_shared_link(nil)
end

#move_to(folder_id, name = nil) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby-box/item.rb', line 23

def move_to( folder_id, name=nil )
  # Allow either a folder_id or a folder object
  # to be passed in.
  folder_id = folder_id.id if folder_id.instance_of?(RubyBox::Folder)

  self.name = name if name
  self.parent = {"id" => folder_id}

  update
end

#reload_metaObject


66
67
68
69
70
# File 'lib/ruby-box/item.rb', line 66

def reload_meta
  url = "#{RubyBox::API_URL}/#{resource_name}/#{@raw_item['id']}"
  @raw_item = @session.get( url )
  self
end

124
125
126
127
# File 'lib/ruby-box/item.rb', line 124

def shared_link
  return nil unless @raw_item['shared_link']
  RubyBox::Item.factory(@session, @raw_item['shared_link'].merge('type' => 'shared_link'))
end

#updateObject


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby-box/item.rb', line 34

def update
  reload_meta unless etag

  url = "#{RubyBox::API_URL}/#{resource_name}/#{id}"
  uri = URI.parse(url)

  request = Net::HTTP::Put.new(uri.path, {
    "if-match" => etag,
    "Content-Type" => 'application/json'
  })
  request.body = JSON.dump(serialize)

  @raw_item = @session.request(uri, request)
  self
end