Class: SFRest::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrest/collection.rb

Overview

Find colletions, return their data.

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ Collection

Returns a new instance of Collection.

Parameters:



7
8
9
# File 'lib/sfrest/collection.rb', line 7

def initialize(conn)
  @conn = conn
end

Instance Method Details

#add_sites(id, sites) ⇒ Hash{ "id" => Integer, "name" => String, "time" => "2016-10-28T09:25:26+00:00", "site_ids_added" => Array, "added" => Boolean, "message" => String }

adds site(s) to a site collection

Parameters:

  • id (int)

    of the collection to which to add sites

  • sites (int|Array)

    nid or array of site nids.

Returns:

  • (Hash{ "id" => Integer, "name" => String, "time" => "2016-10-28T09:25:26+00:00", "site_ids_added" => Array, "added" => Boolean, "message" => String })


124
125
126
127
128
129
# 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

#collection_data_from_results(res, name, key) ⇒ Object

Extract the site data for ‘key’ based on the site result object

Parameters:

  • res (Hash)

    result from a request to /collections

  • name (String)
  • key (String)

    one of the user data returned (id, name, domain…)

Returns:

  • (Object)

    Integer, String, Array, Hash depending on the collection data



36
37
38
39
40
41
42
# 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

#collection_listHash{'count' => Integer, 'sites' => Hash}

Gets the complete list of collections Makes multiple requests to the factory to get all the collections on the factory

Returns:

  • (Hash{'count' => Integer, 'sites' => Hash})


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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(name, sites, groups, internal_domain_prefix = nil) ⇒ Hash { "id" => Integer, "name" => String, "time" => "2016-11-25T13:18:44+00:00", "internal_domain" => String }

create a site collection

Parameters:

  • name (String)

    site collection name

  • sites (int|Array)

    nid or array of site nids. First nid is primary

  • groups (int|Array)

    gid or array of group ids.

  • internal_domain_prefix (String) (defaults to: nil)

    optional the prefix for the internal domain defaults to name

Returns:

  • (Hash { "id" => Integer, "name" => String, "time" => "2016-11-25T13:18:44+00:00", "internal_domain" => String })


94
95
96
97
98
99
100
101
# 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

#delete(id) ⇒ Hash{ "id" => Integer, "time" => "2016-10-28T09:25:26+00:00", "deleted" => Boolean, "message" => String }

deletes a site collection performs the same action as deleting in the UI

Parameters:

  • id (Integer)

    the id of the site collection to delete

Returns:

  • (Hash{ "id" => Integer, "time" => "2016-10-28T09:25:26+00:00", "deleted" => Boolean, "message" => String })


110
111
112
113
# File 'lib/sfrest/collection.rb', line 110

def delete(id)
  current_path = "/api/v1/collections/#{id}"
  @conn.delete(current_path)
end

#first_collection_idInteger

gets the site id of the 1st one found using the api

Returns:

  • (Integer)

    the site id



53
54
55
56
# File 'lib/sfrest/collection.rb', line 53

def first_collection_id
  res = @conn.get('/api/v1/collections')
  res['collections'].first['id']
end

#get_collection_data(id) ⇒ Hash

Gets the site data for a specific id

Parameters:

  • id (Integer)

    the site id

Returns:

  • (Hash)


47
48
49
# File 'lib/sfrest/collection.rb', line 47

def get_collection_data(id)
  @conn.get("/api/v1/collections/#{id}")
end

#get_collection_id(name) ⇒ Integer

gets the site ID for the site named sitename will page through all the sites available searching for the site

Parameters:

  • name (String)

    the name of the site

Returns:

  • (Integer)

    the id of sitename



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

#remove_sites(id, sites) ⇒ Hash{ "id" => Integer, "name" => String, "time" => "2016-10-28T09:25:26+00:00", "site_ids_removed" => Array, "removed" => Boolean, "message" => String }

removes site(s) from a site collection

Parameters:

  • id (int)

    of the collection from which to remove sites

  • sites (int|Array)

    nid or array of site nids.

Returns:

  • (Hash{ "id" => Integer, "name" => String, "time" => "2016-10-28T09:25:26+00:00", "site_ids_removed" => Array, "removed" => Boolean, "message" => String })


140
141
142
143
144
145
# 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

#set_primary_site(id, site) ⇒ Hash{ "id" => Integer, "name" => String, "time" => "2016-10-28T09:25:26+00:00", "primary_site_id": Integer, "switched" => Boolean, "message" => String }

sets a site to be a primary site in a site collection

Parameters:

  • id (int)

    of the collection where the primary site is being changed

  • site (int)

    nid to become the primary site

Returns:

  • (Hash{ "id" => Integer, "name" => String, "time" => "2016-10-28T09:25:26+00:00", "primary_site_id": Integer, "switched" => Boolean, "message" => String })


156
157
158
159
160
# 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