Class: ActiveSP::Connection

Inherits:
Object
  • Object
show all
Includes:
PersistentCachingConfig, Root, Util
Defined in:
lib/activesp/connection.rb,
lib/activesp/root.rb

Overview

This class is uses to configure the connection to a SharePoint repository. This is the starting point for doing anything with SharePoint.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PersistentCachingConfig

#configure_persistent_cache

Methods included from Root

#group, #groups, #roles, #root, #user, #users

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • options (Hash) (defaults to: {})

    The connection options

Options Hash (options):

  • :root (String)

    The URL of the root site

  • :login (String) — default: nil

    The login

  • :password (String) — default: nil

    The password associated with the given login. Is mandatory if the login is specified. Also can’t be “password” as that is inherently insafe. We don’t explicitly check for this, but it can’t be that.



52
53
54
55
56
57
58
59
60
61
# File 'lib/activesp/connection.rb', line 52

def initialize(options = {})
  options = options.dup
  @root_url = options.delete(:root) or raise ArgumentError, "missing :root option"
  @login = options.delete(:login)
  @password = options.delete(:password)
  @trace = options.delete(:trace)
  options.empty? or raise ArgumentError, "unknown options #{options.keys.map { |k| k.inspect }.join(", ")}"
  cache = nil
  configure_persistent_cache { |c| cache ||= c }
end

Instance Attribute Details

#loginObject (readonly)

TODO: create profile



46
47
48
# File 'lib/activesp/connection.rb', line 46

def 
  @login
end

#passwordObject (readonly)

TODO: create profile



46
47
48
# File 'lib/activesp/connection.rb', line 46

def password
  @password
end

#root_urlObject (readonly)

TODO: create profile



46
47
48
# File 'lib/activesp/connection.rb', line 46

def root_url
  @root_url
end

#traceObject (readonly)

TODO: create profile



46
47
48
# File 'lib/activesp/connection.rb', line 46

def trace
  @trace
end

Instance Method Details

#fetch(url) ⇒ String

Fetches the content at the given URL using the login and password with which this connection was constructed, if any. Always uses the GET method. Supports only HTTP as protocol at the time of writing. This is useful for fetching content files from the server.

Parameters:

  • url (String)

    The URL to fetch

Returns:

  • (String)

    The content fetched from the URL



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/activesp/connection.rb', line 107

def fetch(url)
  # TODO: support HTTPS too
  @open_params ||= begin
    u = URL(@root_url)
    [u.host, u.port]
  end
  Net::HTTP.start(*@open_params) do |http|
    request = Net::HTTP::Get.new(URL(url).full_path.gsub(/ /, "%20"))
    request.ntlm_auth(@login, @password) if @login
    response = http.request(request)
    # if Net::HTTPFound === response
    #   response = fetch(response["location"])
    # end
    # response
  end
end

#find_by_key(key) ⇒ Base?

Finds the object with the given key

Parameters:

  • key (String)

    The key of the object to find

Returns:

  • (Base, nil)

    The object with the given key, or nil if no object with the given key is found



66
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
95
96
97
98
99
# File 'lib/activesp/connection.rb', line 66

def find_by_key(key)
  type, trail = decode_key(key)
  case type[0]
  when ?S
    ActiveSP::Site.new(self, trail[0] == "" ? @root_url : ::File.join(@root_url, trail[0]), trail[1].to_i)
  when ?L
    ActiveSP::List.new(find_by_key(trail[0]), trail[1])
  when ?U
    ActiveSP::User.new(root, trail[0])
  when ?G
    ActiveSP::Group.new(root, trail[0])
  when ?R
    ActiveSP::Role.new(root, trail[0])
  when ?A
    find_by_key(trail[0]).field(trail[1])
  when ?P
    ActiveSP::PermissionSet.new(find_by_key(trail[0]))
  when ?F
    list = find_by_key(trail[0])
    ActiveSP::Folder.new(list, trail[1])
  when ?I
    list = find_by_key(trail[0])
    ActiveSP::Item.new(list, trail[1])
  when ?T
    parent = find_by_key(trail[0])
    if ActiveSP::List === parent
      ActiveSP::ContentType.new(parent.site, parent, trail[1])
    else
      ActiveSP::ContentType.new(parent, nil, trail[1])
    end
  else
    raise "not yet #{key.inspect}"
  end
end