class Sharepoint::Object

Attributes

fields[RW]
parent[RW]

Public Class Methods

new(site, data) click to toggle source
Calls superclass method Sharepoint::ObjectProperties::new
# File lib/sharepoint-object.rb, line 100
def initialize site, data
  @parent = nil
  super site, data
end

Protected Class Methods

belongs_to(resource_name) click to toggle source
# File lib/sharepoint-object.rb, line 78
def belongs_to resource_name
  resource_name = resource_name.to_s
  class_name    = (self.name).split('::').last.downcase
  method_name   = class_name.pluralize
  define_singleton_method "all_from_#{resource_name}" do |resource|
    resource.site.query :get, "#{resource.__metadata['uri']}/#{method_name}"
  end
  define_singleton_method "get_from_#{resource_name}" do |resource, name|
    resource.site.query :get, "#{resource.__metadata['uri']}/#{method_name}('#{URI.encode name}')"
  end
  define_method "create_uri" do
    unless self.parent.nil?
      "#{self.parent.__metadata['uri']}/#{method_name}"
    else
      method_name
    end
  end
end
field(name, options = {}) click to toggle source
# File lib/sharepoint-object.rb, line 23
def field name, options = {}
  options[:access] ||= [ :read, :write ]
  @fields ||= []
  @fields << { name: name, access: options[:access], default: options[:default] }
end
method(name, method_params = {}) click to toggle source
# File lib/sharepoint-object.rb, line 29
def method name, method_params = {}
  method_params[:http_method]    ||= :post
  method_params[:endpoint]       ||= name.to_s.camelize
  method_params[:default_params] ||= Hash.new
  define_method name, ->(params = Hash.new) do
    action = "#{__metadata['uri']}/#{method_params[:endpoint]}"
    body   = nil
    # Set default parameters
    (method_params[:default_params].each do |key,value|
      params[key] == value if params[key].nil?
    end)
    if (method_params[:http_method] == :get) and (params.class < Hash) and (params.keys.count > 0)
      # if method is get, Fill action with parameters
      action     += '('
      (params.each do |key,value|
        action += ',' unless params.keys.first == key
        action += key + '='
        action += (if (value.class < String) or (value.class < Symbol)
         "'#{(URI.encode value.gsub("'", %q(\\\')))}'"
        else
          value
        end)
      end)
      action += ')'
    else
      # if method is post, send parameters in the body
      body = (params.class < Hash ? params.to_json : params)
    end
    # Call action
    @site.query method_params[:http_method], action, body, method_params[:skip_json]
  end
end
sharepoint_resource(options = {}) click to toggle source
# File lib/sharepoint-object.rb, line 62
def sharepoint_resource options = {}
  options[:method_name]   ||= (self.name).split('::').last.downcase + 's'
  options[:getter]        ||= options[:method_name]
  options[:get_from_name] ||= options[:getter]
  Sharepoint::Site.send :define_method, options[:method_name] do
    self.query :get, options[:getter].to_s
  end unless options[:no_root_collection] == true
  Sharepoint::Site.send :define_method, (self.name).split('::').last.downcase do |id|
    if id =~ /^[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}$/
      self.query :get, "#{options[:getter]}(guid'#{id}')"
    else
      self.query :get, "#{options[:get_from_name]}('#{URI.encode id}')"
    end
  end
end

Public Instance Methods

copy(new_object = nil) click to toggle source
# File lib/sharepoint-object.rb, line 133
def copy new_object = nil
  updating     = !new_object.nil?
  new_object ||= self.class.new @site
  self.class.fields.each do |field|
    next unless @data.keys.include? field[:name].to_s
    next if (field[:access] & [ :write, :initialize ]).count == 0
    value = @data[field[:name].to_s]
    if updating == false
      new_object.data[field[:name].to_s]         = value
    elsif new_object.data[field[:name].to_s] != value
      new_object.updated_data[field[:name].to_s] = value
    end
  end
  new_object
end
destroy() click to toggle source
# File lib/sharepoint-object.rb, line 126
def destroy
  @site.query :post, resource_uri do |curl|
    curl.headers['X-HTTP-Method'] = 'DELETE'
    curl.headers['If-Match']      = __metadata['etag']
  end
end
guid() click to toggle source
# File lib/sharepoint-object.rb, line 105
def guid
  return @guid unless @guid.nil?
  __metadata['id'].scan(/guid'([^']+)'/) do ||
    @guid = $1
    break
  end
  @guid
end
reload() click to toggle source
# File lib/sharepoint-object.rb, line 114
def reload
  @site.query :get, __metadata['uri']
end
save() click to toggle source
# File lib/sharepoint-object.rb, line 118
def save
  if @data['__metadata'].nil? or @data['__metadata']['id'].nil?
    create
  elsif @updated_data.keys.count > 0
    update
  end
end

Private Instance Methods

create() click to toggle source
# File lib/sharepoint-object.rb, line 166
def create
  @site.query :post, create_uri, @data.to_json
end
create_uri() click to toggle source
# File lib/sharepoint-object.rb, line 162
def create_uri
  sharepoint_typename.downcase.pluralize
end
resource_uri() click to toggle source
# File lib/sharepoint-object.rb, line 158
def resource_uri
  @data['__metadata']['uri'].gsub(/^https:\/\/[^\/]+\/_api\/web\//i, '')
end
sharepoint_typename() click to toggle source
# File lib/sharepoint-object.rb, line 150
def sharepoint_typename
  if self.is_a?(Sharepoint::GenericSharepointObject)
    @generic_type_name
  else
    self.class.name.split('::').last
  end
end
update() click to toggle source
# File lib/sharepoint-object.rb, line 170
def update
  @updated_data['__metadata'] ||= @data['__metadata']
  @site.query :post, resource_uri, @updated_data.to_json do |curl|
    curl.headers['X-HTTP-Method'] = 'MERGE'
    curl.headers['If-Match']      = __metadata['etag']
  end
  @updated_data = Hash.new
end