Class: Sharepoint::Site
- Inherits:
-
Object
- Object
- Sharepoint::Site
- Defined in:
- lib/sharepoint-ruby.rb,
lib/sharepoint-object.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#server_url ⇒ Object
readonly
Returns the value of attribute server_url.
-
#session ⇒ Object
Returns the value of attribute session.
-
#url ⇒ Object
Returns the value of attribute url.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #api_path(uri) ⇒ Object
- #authentication_path ⇒ Object
- #context_info ⇒ Object
- #filter_path(uri) ⇒ Object
-
#form_digest ⇒ Object
Sharepoint uses ‘X-RequestDigest’ as a CSRF security-like.
-
#initialize(server_url, site_name, prefix: "sites") ⇒ Site
constructor
A new instance of Site.
-
#make_object_from_data(data) ⇒ Object
Uses sharepoint’s __metadata field to solve which Ruby class to instantiate, and return the corresponding Sharepoint::Object.
- #make_object_from_response(data) ⇒ Object
- #query(method, uri, body = nil, skip_json = false, &block) ⇒ Object
Constructor Details
#initialize(server_url, site_name, prefix: "sites") ⇒ Site
Returns a new instance of Site.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sharepoint-ruby.rb', line 16 def initialize server_url, site_name, prefix: "sites" @server_url = server_url @name = site_name uri_prefix = unless prefix.empty? then prefix + '/' else '' end @url = "#{@server_url}/#{uri_prefix}#{@name}" @session = Session.new self @web_context = nil @protocol = 'https' @verbose = false end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/sharepoint-ruby.rb', line 13 def name @name end |
#protocol ⇒ Object
Returns the value of attribute protocol.
11 12 13 |
# File 'lib/sharepoint-ruby.rb', line 11 def protocol @protocol end |
#server_url ⇒ Object (readonly)
Returns the value of attribute server_url.
10 11 12 |
# File 'lib/sharepoint-ruby.rb', line 10 def server_url @server_url end |
#session ⇒ Object
Returns the value of attribute session.
12 13 14 |
# File 'lib/sharepoint-ruby.rb', line 12 def session @session end |
#url ⇒ Object
Returns the value of attribute url.
11 12 13 |
# File 'lib/sharepoint-ruby.rb', line 11 def url @url end |
#verbose ⇒ Object
Returns the value of attribute verbose.
14 15 16 |
# File 'lib/sharepoint-ruby.rb', line 14 def verbose @verbose end |
Instance Method Details
#api_path(uri) ⇒ Object
31 32 33 |
# File 'lib/sharepoint-ruby.rb', line 31 def api_path uri "#{@protocol}://#{@url}/_api/web/#{uri}" end |
#authentication_path ⇒ Object
27 28 29 |
# File 'lib/sharepoint-ruby.rb', line 27 def authentication_path "#{@protocol}://#{@server_url}/_forms/default.aspx?wa=wsignin1.0" end |
#context_info ⇒ Object
39 40 41 |
# File 'lib/sharepoint-ruby.rb', line 39 def context_info query :get, '' end |
#filter_path(uri) ⇒ Object
35 36 37 |
# File 'lib/sharepoint-ruby.rb', line 35 def filter_path uri uri end |
#form_digest ⇒ Object
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.
46 47 48 49 50 51 52 53 |
# File 'lib/sharepoint-ruby.rb', line 46 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 |
#make_object_from_data(data) ⇒ Object
Uses sharepoint’s __metadata field to solve which Ruby class to instantiate, and return the corresponding Sharepoint::Object.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sharepoint-ruby.rb', line 105 def make_object_from_data data 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, false) end if constant.const_defined? type_name klass = constant.const_get type_name rescue nil klass.new self, data else Sharepoint::GenericSharepointObject.new type_name, self, data end end |
#make_object_from_response(data) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sharepoint-ruby.rb', line 86 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sharepoint-ruby.rb', line 55 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. 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 curl.headers["Authorization"] = "Bearer " + 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 if !(skip_json || (result.body_str.nil? || result.body_str.empty?)) begin data = JSON.parse result.body_str raise Sharepoint::DataError.new data, uri, body unless data['error'].nil? make_object_from_response data rescue JSON::ParserError => e raise Sharepoint::RequestError.new("Exception with body=#{body}, e=#{e.inspect}, #{e.backtrace.inspect}, response=#{result.body_str}") end elsif result.status.to_i >= 400 raise Sharepoint::RequestError.new("#{method.to_s.upcase} #{uri} responded with #{result.status}") else result.body_str end end |