Class: Sharepoint::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/sharepoint-ruby.rb,
lib/sharepoint-object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url, site_name) ⇒ Site

Returns a new instance of Site.



78
79
80
81
82
83
84
85
86
# 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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



31
32
33
# File 'lib/sharepoint-ruby.rb', line 31

def name
  @name
end

#protocolObject

Returns the value of attribute protocol.



29
30
31
# File 'lib/sharepoint-ruby.rb', line 29

def protocol
  @protocol
end

#server_urlObject (readonly)

Returns the value of attribute server_url.



28
29
30
# File 'lib/sharepoint-ruby.rb', line 28

def server_url
  @server_url
end

#sessionObject

Returns the value of attribute session.



30
31
32
# File 'lib/sharepoint-ruby.rb', line 30

def session
  @session
end

#urlObject

Returns the value of attribute url.



29
30
31
# File 'lib/sharepoint-ruby.rb', line 29

def url
  @url
end

#verboseObject

Returns the value of attribute verbose.



32
33
34
# File 'lib/sharepoint-ruby.rb', line 32

def verbose
  @verbose
end

Class Method Details

.make_object_from_data(instance, data) ⇒ Object

and return the corresponding Sharepoint::Object.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Method Details

#api_path(uri) ⇒ Object



92
93
94
# File 'lib/sharepoint-ruby.rb', line 92

def api_path uri
  "#{@protocol}://#{@url}/_api/web/#{uri}"
end

#authentication_pathObject



88
89
90
# File 'lib/sharepoint-ruby.rb', line 88

def authentication_path
  "#{@protocol}://#{@server_url}/_forms/default.aspx?wa=wsignin1.0"
end

#context_infoObject



100
101
102
# File 'lib/sharepoint-ruby.rb', line 100

def context_info
  query :get, ''
end

#filter_path(uri) ⇒ Object



96
97
98
# File 'lib/sharepoint-ruby.rb', line 96

def filter_path uri
  uri
end

#form_digestObject

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.



107
108
109
110
111
112
113
114
# 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) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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