Class: Sharepoint::Site

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url, site_name) ⇒ Site

Returns a new instance of Site



29
30
31
32
33
34
35
36
37
# File 'lib/sharepoint-ruby.rb', line 29

def initialize server_url, site_name
  @server_url  = server_url
  @name        = site_name
  @url         = "#{@server_url}/#{@name}"
  @session     = Session.new self
  @web_context = nil
  @protocole   = 'https'
  @verbose     = false
end

Instance Attribute Details

#nameObject

Returns the value of attribute name



26
27
28
# File 'lib/sharepoint-ruby.rb', line 26

def name
  @name
end

#protocoleObject

Returns the value of attribute protocole



24
25
26
# File 'lib/sharepoint-ruby.rb', line 24

def protocole
  @protocole
end

#server_urlObject (readonly)

Returns the value of attribute server_url



23
24
25
# File 'lib/sharepoint-ruby.rb', line 23

def server_url
  @server_url
end

#sessionObject

Returns the value of attribute session



25
26
27
# File 'lib/sharepoint-ruby.rb', line 25

def session
  @session
end

#urlObject

Returns the value of attribute url



24
25
26
# File 'lib/sharepoint-ruby.rb', line 24

def url
  @url
end

#verboseObject

Returns the value of attribute verbose



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

def verbose
  @verbose
end

Instance Method Details

#api_path(uri) ⇒ Object



43
44
45
# File 'lib/sharepoint-ruby.rb', line 43

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

#authentication_pathObject



39
40
41
# File 'lib/sharepoint-ruby.rb', line 39

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

#context_infoObject



51
52
53
# File 'lib/sharepoint-ruby.rb', line 51

def context_info
  query :get, ''
end

#filter_path(uri) ⇒ Object



47
48
49
# File 'lib/sharepoint-ruby.rb', line 47

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.



58
59
60
61
62
63
64
65
# File 'lib/sharepoint-ruby.rb', line 58

def form_digest
  if @web_context.nil? or (not @web_context.is_up_to_date?)
    @getting_form_digest = true
    @web_context         = query :post, "#{@protocole}://#{@server_url}/_api/contextinfo"
    @getting_form_digest = false
  end
  @web_context.form_digest_value
end

#make_object_from_data(data) ⇒ Object

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sharepoint-ruby.rb', line 115

def make_object_from_data data
  type_name  = data['__metadata']['type'].gsub(/^SP\./, '')
  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 self, data
  else
    Sharepoint::GenericSharepointObject.new type_name, self, data
  end
end

#make_object_from_response(data) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sharepoint-ruby.rb', line 96

def make_object_from_response 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 data['d']
    else
      nil
    end
  else
    array = Array.new
    data['d']['results'].each do |result|
      array << (make_object_from_data result)
    end
    array
  end
end

#query(method, uri, body = nil, skip_json = false, &block) ⇒ Object



67
68
69
70
71
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/sharepoint-ruby.rb', line 67

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"]
      curl.headers["X-RequestDigest"] = form_digest unless @getting_form_digest == true
    end
    curl.verbose = @verbose
    @session.send :curl, curl unless not @session.methods.include? :curl
    block.call curl           unless block.nil?
  end

  unless 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?
      make_object_from_response data
    rescue JSON::ParserError => e
      raise Exception.new("Exception with body=#{body}, e=#{e.inspect}, #{e.backtrace.inspect}, response=#{result.body_str}")
    end
  else
    result.body_str
  end
end