Class: SFRest::Site
- Inherits:
-
Object
- Object
- SFRest::Site
- Defined in:
- lib/sfrest/site.rb
Overview
Find sites, create a site,
Instance Method Summary collapse
-
#backup ⇒ Object
accessors for backups/restore so that you can do site.backup.list_backups.
-
#cache_clear(site_id) ⇒ Hash
Clears the caches for a site.
-
#create_site(sitename, group_id, install_profile = nil, codebase = nil) ⇒ Object
(also: #create)
Creates a site.
-
#delete(site_id) ⇒ Hash
Deletes a site.
-
#first_site_id ⇒ Integer
gets the site id of the 1st one found using the api.
-
#get_site_data(site_id) ⇒ Hash
Gets the site data for a specific site id.
-
#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.
-
#initialize(conn) ⇒ Site
constructor
A new instance of Site.
-
#site_data_from_results(res, sitename, key) ⇒ Object
Extract the site data for ‘key’ based on the site result object.
-
#site_list(**opts) ⇒ Hash{'count' => Integer, 'sites' => Hash}
Gets the complete list of sites.
Constructor Details
#initialize(conn) ⇒ Site
Returns a new instance of Site.
7 8 9 |
# File 'lib/sfrest/site.rb', line 7 def initialize(conn) @conn = conn end |
Instance Method Details
#backup ⇒ Object
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.
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.
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.
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_id ⇒ Integer
gets the site id of the 1st one found using the api
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
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
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
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.
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 |