class Sharepoint::Site

Attributes

name[RW]
protocol[RW]
server_url[R]
session[RW]
url[RW]
verbose[RW]

Public Class Methods

make_object_from_data(instance, data) click to toggle source

Uses sharepoint's __metadata field to solve which Ruby class to instantiate, and return the corresponding Sharepoint::Object.

# File lib/sharepoint-ruby.rb, line 54
def make_object_from_data instance, data
  return data unless data.is_a? Hash

  type_name  = data['__metadata']['type'].gsub(/^SP\./, '')
                                         .gsub(/^Collection\(Edm\.String\)/, 'CollectionString')
                                         .gsub(/^Collection\(Edm\.Int32\)/, 'CollectionInteger')
  type_parts = type_name.split '.'
  type_name  = type_parts.pop
  constant   = Sharepoint
  type_parts.each do |part| constant = constant.const_get part end

  klass      = constant.const_get type_name rescue nil
  if klass
    klass.new instance, data
  # Patch for Sharepoint 2013 on-prem, missing period between list name
  # and object type.
  elsif data['__metadata']['type'] =~ /SP\.Data\..+Item/
    Sharepoint::ListItem.new instance, data
  else
    Sharepoint::GenericSharepointObject.new type_name, instance, data
  end
end
make_object_from_response(instance, data) click to toggle source
# File lib/sharepoint-ruby.rb, line 35
def make_object_from_response instance, data
  if data['d']['results'].nil?
    data['d'] = data['d'][data['d'].keys.first] if data['d']['__metadata'].nil?
    if not data['d'].nil?
      make_object_from_data instance, data['d']
    else
      nil
    end
  else
    array = Array.new
    data['d']['results'].each do |result|
      array << (make_object_from_data instance, result)
    end
    array
  end
end
new(server_url, site_name) click to toggle source
# File lib/sharepoint-ruby.rb, line 78
def initialize server_url, site_name
  @server_url  = server_url
  @name        = site_name
  @url         = "#{@server_url}/#{@name}"
  @session     = Session.new self
  @web_context = nil
  @protocol    = 'https'
  @verbose     = false
end

Public Instance Methods

api_path(uri) click to toggle source
# File lib/sharepoint-ruby.rb, line 92
def api_path uri
  "#{@protocol}://#{@url}/_api/web/#{uri}"
end
authentication_path() click to toggle source
# File lib/sharepoint-ruby.rb, line 88
def authentication_path
  "#{@protocol}://#{@server_url}/_forms/default.aspx?wa=wsignin1.0"
end
context_info() click to toggle source
# File lib/sharepoint-ruby.rb, line 100
def context_info
  query :get, ''
end
filter_path(uri) click to toggle source
# File lib/sharepoint-ruby.rb, line 96
def filter_path uri
  uri
end
form_digest() click to toggle source

Sharepoint uses 'X-RequestDigest' as a CSRF security-like. The form_digest method acquires a token or uses a previously acquired token if it is still supposed to be valid.

# File lib/sharepoint-ruby.rb, line 107
def form_digest
  if @web_context.nil? or (not @web_context.is_up_to_date?)
    @getting_form_digest = true
    @web_context         = query :post, "#{@protocol}://#{@url}/_api/contextinfo"
    @getting_form_digest = false
  end
  @web_context.form_digest_value
end
query(method, uri, body = nil, skip_json=false, &block) click to toggle source
# File lib/sharepoint-ruby.rb, line 116
def query method, uri, body = nil, skip_json=false, &block
  uri        = if uri =~ /^http/ then uri else api_path(uri) end
  arguments  = [ uri ]
  arguments << body if method != :get
  result = Curl::Easy.send "http_#{method}", *arguments do |curl|
    curl.headers["Cookie"]          = @session.cookie
    curl.headers["Accept"]          = "application/json;odata=verbose"
    if method != :get
      curl.headers["Content-Type"]    = curl.headers["Accept"]
      if session.instance_of?(Sharepoint::HttpAuth::Session)
        curl.headers["X-RequestDigest"] = form_digest unless @getting_form_digest == true
      else
        curl.headers["X-RequestDigest"] = form_digest unless @getting_form_digest == true
        curl.headers["Authorization"] = "Bearer " + form_digest unless @getting_form_digest == true
      end
    end
    curl.verbose = @verbose
    @session.send :curl, curl unless not @session.methods.include? :curl
    block.call curl           unless block.nil?
  end
  if !(skip_json || (result.body_str.nil? || result.body_str.empty?))
    begin
      data = JSON.parse result.body_str
      raise Sharepoint::SPException.new data, uri, body unless data['error'].nil?
      self.class.make_object_from_response self, data
    rescue JSON::ParserError => e
      raise SharepointError.new("Exception with body=#{body}, e=#{e.inspect}, #{e.backtrace.inspect}, response=#{result.body_str}")
    end
  elsif result.status.to_i >= 400
    raise SharepointError.new("#{method.to_s.upcase} #{uri} responded with #{result.status}")
  else
    result.body_str
  end
end