class SFRest::Site

Find sites, create a site,

Public Class Methods

new(conn) click to toggle source

@param [SFRest::Connection] conn

# File lib/sfrest/site.rb, line 7
def initialize(conn)
  @conn = conn
end

Public Instance Methods

backup() click to toggle source

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

# File lib/sfrest/site.rb, line 121
def backup
  @conn.backup
end
cache_clear(site_id) click to toggle source

Clears the caches for a site. @param [Integer] site_id The id of the site to be cleared @return [Hash]

# 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(sitename, group_id, install_profile = nil, codebase = nil)

Creates a site. Alias for create_site @param [String] sitename The name of the site to create @param [Integer] group_id The Id of the group the site is to be a member of @param [String] install_profile The install profile to use when creating the site @param [Integer] codebase The codebase index to use in installs.

Alias for: create_site
create_site(sitename, group_id, install_profile = nil, codebase = nil) click to toggle source

Creates a site. @param [String] sitename The name of the site to create. @param [Integer] group_id The Id of the group the site is to be a member of. @param [String] install_profile The install profile to use when creating the site. @param [Integer] codebase The codebase index to use in installs.

# 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
Also aliased as: create
delete(site_id) click to toggle source

Deletes a site. @param [Integer] site_id The id of the stie to be deleted @return [Hash]

# 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_id() click to toggle source

gets the site id of the 1st one found using the api @return [Integer] the site id

# 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) click to toggle source

Gets the site data for a specific site id @param [Integer] site_id the site id @return [Hash]

# 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) click to toggle source

gets the site ID for the site named sitename will page through all the sites available searching for the site @param [String] sitename the name of the site @return [Integer] the id of sitename

# 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) click to toggle source

Extract the site data for 'key' based on the site result object @param [Hash] res result from a request to /sites @param [String] sitename @param [String] key one of the user data returned (id, site, domain…) @return [Object] Integer, String, Array, Hash depending on the site data

# 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) click to toggle source

Gets the complete list of sites.

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

@param [Hash] opts query parameters to be used in this request. @return [Hash{'count' => Integer, 'sites' => Hash}]

# 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