Class: SFRest::Site

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

Overview

Find sites, create a site,

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ Site

Returns a new instance of Site.

Parameters:



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

def initialize(conn)
  @conn = conn
end

Instance Method Details

#backupObject

accessors for backups/restore so that you can do site.backup.list_backups



121
122
123
# File 'lib/sfrest/site.rb', line 121

def backup
  @conn.backup
end

#cache_clear(site_id) ⇒ Hash

Clears the caches for a site.

Parameters:

  • site_id (Integer)

    The id of the site to be cleared

Returns:

  • (Hash)


128
129
130
131
# File 'lib/sfrest/site.rb', line 128

def cache_clear(site_id)
  current_path = "/api/v1/sites/#{site_id}/cache-clear"
  @conn.post current_path, nil
end

#create_site(sitename, group_id, install_profile = nil, codebase = nil) ⇒ Object Also known as: create

Creates a site.

Parameters:

  • sitename (String)

    The name of the site to create.

  • group_id (Integer)

    The Id of the group the site is to be a member of.

  • install_profile (String) (defaults to: nil)

    The install profile to use when creating the site.

  • codebase (Integer) (defaults to: nil)

    The codebase index to use in installs.



96
97
98
99
100
101
# File 'lib/sfrest/site.rb', line 96

def create_site(sitename, group_id, install_profile = nil, codebase = nil)
  current_path = '/api/v1/sites'
  payload = { 'site_name' => sitename, 'group_ids' => [group_id],
              'install_profile' => install_profile, 'codebase' => codebase }.to_json
  @conn.post(current_path, payload)
end

#delete(site_id) ⇒ Hash

Deletes a site.

Parameters:

  • site_id (Integer)

    The id of the stie to be deleted

Returns:

  • (Hash)


114
115
116
117
# File 'lib/sfrest/site.rb', line 114

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

#first_site_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/site.rb', line 53

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

#get_site_data(site_id) ⇒ Hash

Gets the site data for a specific site id

Parameters:

  • site_id (Integer)

    the site id

Returns:

  • (Hash)


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

def get_site_data(site_id)
  @conn.get("/api/v1/sites/#{site_id}")
end

#get_site_id(sitename) ⇒ Integer

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

Parameters:

  • sitename (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/site.rb', line 15

def get_site_id(sitename)
  pglimit = 100
  res = @conn.get("/api/v1/sites&limit=#{pglimit}")
  sitecount = res['count'].to_i
  id = site_data_from_results(res, sitename, 'id')
  return id if id

  pages = (sitecount / pglimit) + 1
  2.upto(pages) do |i|
    res = @conn.get("/api/v1/sites&limit=#{pglimit}?page=#{i}")
    id = site_data_from_results(res, sitename, 'id')
    return id if id
  end
  nil
end

#site_data_from_results(res, sitename, key) ⇒ Object

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

Parameters:

  • res (Hash)

    result from a request to /sites

  • sitename (String)
  • key (String)

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

Returns:

  • (Object)

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



36
37
38
39
40
41
42
# File 'lib/sfrest/site.rb', line 36

def site_data_from_results(res, sitename, key)
  sites = res['sites']
  sites.each do |site|
    return site[key] if site['site'] == sitename
  end
  nil
end

#site_list(**opts) ⇒ Hash{'count' => Integer, 'sites' => Hash}

Gets the complete list of sites.

Makes multiple requests to the factory to get all the sites on the factory.

Parameters:

  • opts (Hash)

    query parameters to be used in this request.

Returns:

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


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sfrest/site.rb', line 65

def site_list(**opts)
  page = 1
  not_done = true
  count = 0
  sites = []
  while not_done
    opts['page'] = page
    current_path = "/api/v1/sites?#{URI.encode_www_form(opts)}"
    res = @conn.get(current_path)
    if res['sites'] == []
      not_done = false
    elsif !res['message'].nil?
      return { 'message' => res['message'] }
    elsif page == 1
      count = res['count']
      sites = res['sites']
    else
      res['sites'].each do |site|
        sites << site
      end
    end
    page += 1
  end
  { 'count' => count, 'sites' => sites }
end