class SFRest::Collection
Find colletions, return their data.
Public Class Methods
@param [SFRest::Connection] conn
# File lib/sfrest/collection.rb, line 7 def initialize(conn) @conn = conn end
Public Instance Methods
adds site(s) to a site collection @param [int] id of the collection to which to add sites @param [int|Array] sites nid or array of site nids. @return [Hash{ “id” => Integer,
"name" => String, "time" => "2016-10-28T09:25:26+00:00", "site_ids_added" => Array, "added" => Boolean, "message" => String }]
# File lib/sfrest/collection.rb, line 124 def add_sites(id, sites) sites = Array(sites) payload = { 'site_ids' => sites }.to_json current_path = "/api/v1/collections/#{id}/add" @conn.post(current_path, payload) end
Extract the site data for 'key' based on the site result object @param [Hash] res result from a request to /collections @param [String] name @param [String] key one of the user data returned (id, name, domain…) @return [Object] Integer, String, Array, Hash depending on the collection data
# File lib/sfrest/collection.rb, line 36 def collection_data_from_results(res, name, key) collections = res['collections'] collections.each do |collection| return collection[key] if collection['name'] == name end nil end
Gets the complete list of collections Makes multiple requests to the factory to get all the collections on the factory @return [Hash{'count' => Integer, 'sites' => Hash}]
# File lib/sfrest/collection.rb, line 61 def collection_list page = 1 not_done = true count = 0 while not_done current_path = '/api/v1/collections?page='.dup << page.to_s res = @conn.get(current_path) if res['collections'] == [] not_done = false elsif !res['message'].nil? return { 'message' => res['message'] } elsif page == 1 count = res['count'] collections = res['collections'] else res['collections'].each do |collection| collections << collection end end page += 1 end { 'count' => count, 'collections' => collections } end
create a site collection @param [String] name site collection name @param [int|Array] sites nid or array of site nids. First nid is primary @param [int|Array] groups gid or array of group ids. @param [String] internal_domain_prefix optional the prefix for the internal domain
defaults to name
@return [Hash { “id” => Integer, “name” => String,
"time" => "2016-11-25T13:18:44+00:00", "internal_domain" => String }]
# File lib/sfrest/collection.rb, line 94 def create(name, sites, groups, internal_domain_prefix = nil) sites = Array(sites) groups = Array(groups) current_path = '/api/v1/collections' payload = { 'name' => name, 'site_ids' => sites, 'group_ids' => groups, 'internal_domain_prefix' => internal_domain_prefix }.to_json @conn.post(current_path, payload) end
deletes a site collection performs the same action as deleting in the UI @param [Integer] id the id of the site collection to delete @return [Hash{ “id” => Integer,
"time" => "2016-10-28T09:25:26+00:00", "deleted" => Boolean, "message" => String }]
# File lib/sfrest/collection.rb, line 110 def delete(id) current_path = "/api/v1/collections/#{id}" @conn.delete(current_path) end
gets the site id of the 1st one found using the api @return [Integer] the site id
# File lib/sfrest/collection.rb, line 53 def first_collection_id res = @conn.get('/api/v1/collections') res['collections'].first['id'] end
Gets the site data for a specific id @param [Integer] id the site id @return [Hash]
# File lib/sfrest/collection.rb, line 47 def get_collection_data(id) @conn.get("/api/v1/collections/#{id}") end
gets the site ID for the site named sitename will page through all the sites available searching for the site @param [String] name the name of the site @return [Integer] the id of sitename
# File lib/sfrest/collection.rb, line 15 def get_collection_id(name) pglimit = 100 res = @conn.get("/api/v1/collections&limit=#{pglimit}") count = res['count'].to_i id = collection_data_from_results(res, name, 'id') return id if id pages = (count / pglimit) + 1 2.upto(pages) do |i| res = @conn.get("/api/v1/collections&limit=#{pglimit}?page=#{i}") id = collection_data_from_results(res, name, 'id') return id if id end nil end
removes site(s) from a site collection @param [int] id of the collection from which to remove sites @param [int|Array] sites nid or array of site nids. @return [Hash{ “id” => Integer,
"name" => String, "time" => "2016-10-28T09:25:26+00:00", "site_ids_removed" => Array, "removed" => Boolean, "message" => String }]
# File lib/sfrest/collection.rb, line 140 def remove_sites(id, sites) sites = Array(sites) payload = { 'site_ids' => sites }.to_json current_path = "/api/v1/collections/#{id}/remove" @conn.post(current_path, payload) end
sets a site to be a primary site in a site collection @param [int] id of the collection where the primary site is being changed @param [int] site nid to become the primary site @return [Hash{ “id” => Integer,
"name" => String, "time" => "2016-10-28T09:25:26+00:00", "primary_site_id": Integer, "switched" => Boolean, "message" => String }]
# File lib/sfrest/collection.rb, line 156 def set_primary_site(id, site) payload = { 'site_id' => site }.to_json current_path = "/api/v1/collections/#{id}/set-primary" @conn.post(current_path, payload) end