class RubyBox::Item
Public Class Methods
has_many(*keys)
click to toggle source
# 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)
click to toggle source
# File lib/ruby-box/item.rb, line 19 def self.has_many_paginated(*keys) keys.each {|key| @@has_many_paginated << key.to_s} end
new( session, raw_item )
click to toggle source
# File lib/ruby-box/item.rb, line 10 def initialize( session, raw_item ) @session = session @raw_item = raw_item end
Protected Class Methods
factory(session, entry)
click to toggle source
# File lib/ruby-box/item.rb, line 135 def self.factory(session, entry) type = entry['type'] ? entry['type'].split('_').map(&:capitalize).join('').to_sym : nil if RubyBox.constants.include? type RubyBox.const_get(type).new(session, entry) else RubyBox::Item.new(session, entry) end end
Public Instance Methods
as_json(opts={})
click to toggle source
# File lib/ruby-box/item.rb, line 129 def as_json(opts={}) @raw_item end
create()
click to toggle source
# 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
delete(opts={})
click to toggle source
# 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
method_missing(method, *args, &block)
click to toggle source
# 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
move_to( folder_id, name=nil )
click to toggle source
# 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_meta()
click to toggle source
# 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
update()
click to toggle source
# 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
Protected Instance Methods
has_mini_format?()
click to toggle source
# File lib/ruby-box/item.rb, line 144 def has_mini_format? false end
Private Instance Methods
many(key)
click to toggle source
# File lib/ruby-box/item.rb, line 150 def many(key) url = "#{RubyBox::API_URL}/#{resource_name}/#{id}/#{key}" resp = @session.get( url ) resp['entries'].map {|i| RubyBox::Item.factory(@session, i)} end
paginated(key, item_limit=100, offset=0, fields=nil)
click to toggle source
# File lib/ruby-box/item.rb, line 156 def paginated(key, item_limit=100, offset=0, fields=nil) Enumerator.new do |yielder| while true url = "#{RubyBox::API_URL}/#{resource_name}/#{id}/#{key}?limit=#{item_limit}&offset=#{offset}" url = "#{url}&fields=#{fields.map(&:to_s).join(',')}" if fields resp = @session.get( url ) resp['entries'].each do |entry| yielder.yield(RubyBox::Item.factory(@session, entry)) end offset += resp['entries'].count break if resp['offset'].to_i + resp['limit'].to_i >= resp['total_count'].to_i end end end
serialize()
click to toggle source
# File lib/ruby-box/item.rb, line 171 def serialize update_fields.inject({}) {|hash, field| hash[field] = @raw_item[field]; hash} end
update_fields()
click to toggle source
# File lib/ruby-box/item.rb, line 175 def update_fields ['name', 'description', 'parent'] end