Class: CiscoWebex::Rest
- Inherits:
-
Object
- Object
- CiscoWebex::Rest
- Defined in:
- lib/REST/Rest.rb
Constant Summary collapse
- BASE_URL =
"https://webexapis.com"
Class Method Summary collapse
-
.delete(auth_token, uri, params = nil) ⇒ Object
basid REST DELETE to Webex teams.
-
.find_next_page(headers) ⇒ Object
find next request for paging through GET requests.
-
.get(auth_token, uri, params = nil, limit = 100) ⇒ Object
basid REST GET to Webex teams.
-
.head(auth_token, uri, params = nil) ⇒ Object
basid REST GET to Webex teams.
-
.parse_params(params, return_format = "json") ⇒ Object
format params for passing as needed json/urlencoded.
-
.patch(auth_token, uri, params) ⇒ Object
basic REST PUT for Webex.
-
.post(auth_token, uri, params) ⇒ Object
basic REST POST for Webex.
-
.put(auth_token, uri, params) ⇒ Object
basic REST PUT for Webex.
- .send_rest(rest, request, limit = 100) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Rest
constructor
A new instance of Rest.
Constructor Details
#initialize ⇒ Rest
Returns a new instance of Rest.
10 11 12 |
# File 'lib/REST/Rest.rb', line 10 def initialize() puts "Initializing Webex::Teams::Rest not required..." end |
Class Method Details
.delete(auth_token, uri, params = nil) ⇒ Object
basid REST DELETE to Webex teams
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/REST/Rest.rb', line 134 def self.delete(auth_token, uri, params=nil) # set REST client headers, body etc if params.class == String uri = "#{uri}/#{params}" params = {} elsif params.to_h.has_key?("id") uri = "#{uri}/#{params.to_h['id']}" params = {} end uri = URI("#{BASE_URL}#{uri}") uri.query = parse_params(params, "www-encoded") if params # encode and set params if required https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Delete.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" return send_rest(https, request) # send REST call end |
.find_next_page(headers) ⇒ Object
find next request for paging through GET requests
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/REST/Rest.rb', line 15 def self.find_next_page(headers) if headers.has_key?('link') uri = URI(headers['link'].split(";")[0].tr("<>", "")) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Get.new(uri) request["Authorization"] = "Bearer #{@auth_token}" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" return [ https, request ] else return false end end |
.get(auth_token, uri, params = nil, limit = 100) ⇒ Object
basid REST GET to Webex teams
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/REST/Rest.rb', line 94 def self.get(auth_token, uri, params=nil, limit=100) @auth_token = auth_token if params.class == String uri = "#{uri}/#{params}" elsif params.to_h.has_key?("id") uri = "#{uri}/#{params.to_h['id']}" end # set REST client headers, body etc uri = URI("#{BASE_URL}#{uri}") uri.query = parse_params(params, "www-encoded") if (params.class == Hash || params.class == OpenStruct) # encode and set params if required https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Get.new(uri) request["Authorization"] = "Bearer #{@auth_token}" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" return send_rest(https, request, limit) # send REST call end |
.head(auth_token, uri, params = nil) ⇒ Object
basid REST GET to Webex teams
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/REST/Rest.rb', line 114 def self.head(auth_token, uri, params=nil) # set REST client headers, body etc if params.class == String uri = "#{uri}/#{params}" elsif params.to_h.has_key?("id") uri = "#{uri}/#{params.to_h['id']}" end uri = URI("#{BASE_URL}#{uri}") uri.query = parse_params(params, "www-encoded") if params # encode and set params if required https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Head.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" return send_rest(https, request) # send REST call end |
.parse_params(params, return_format = "json") ⇒ Object
format params for passing as needed json/urlencoded
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/REST/Rest.rb', line 31 def self.parse_params(params, return_format="json") return params if ( return_format == "json" && params.class == String && JSON.parse(params) ) # if already json then just send back if params.class == OpenStruct params = params.to_h end for param in params.each() if param[1] == nil || ( params[1].class == String && param[1] == "" ) || ( ( params.class == Array || params.class == Hash ) && params.length == 0 ) params.delete(param[0]) end end case return_format when "json" params = params.to_json when "www-encoded" params = URI.encode_www_form(params) end return params end |
.patch(auth_token, uri, params) ⇒ Object
basic REST PUT for Webex
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/REST/Rest.rb', line 198 def self.patch(auth_token, uri, params) # build REST client/headers/body etc if params.class != Hash || params.length == 0 STDERR.puts "CiscoWevex::Rest.patch(): Must supply `params` as hash for update" return False elsif params.to_h.has_key?("id") uri = "#{uri}/#{params['id']}" end uri = URI("#{BASE_URL}#{uri}") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Patch.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["Content-Type"] = "application/json" request["Accept"] = "*/*" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" request.body = parse_params(params) # format params as json body return send_rest(https, request) # send REST call end |
.post(auth_token, uri, params) ⇒ Object
basic REST POST for Webex
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/REST/Rest.rb', line 156 def self.post(auth_token, uri, params) if params.class != Hash || params.length == 0 STDERR.puts "CiscoWebex::Rest.post(): Must supply `params` as hash for post" return False end # build REST client/headers/body etc uri = URI("#{BASE_URL}#{uri}") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Post.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["Content-Type"] = "application/json" request["Accept"] = "*/*" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" request.body = parse_params(params) # format params as json body return send_rest(https, request) # send REST call end |
.put(auth_token, uri, params) ⇒ Object
basic REST PUT for Webex
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/REST/Rest.rb', line 176 def self.put(auth_token, uri, params) if params.class != Hash || params.length == 0 STDERR.puts "CiscoWebex::Rest.put(): Must supply `params` as hash for update" return False elsif params.to_h.has_key?("id") uri = "#{uri}/#{params['id']}" end uri = URI("#{BASE_URL}#{uri}") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Put.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["Content-Type"] = "application/json" request["Accept"] = "*/*" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" request.body = parse_params(params) # format params as json body return send_rest(https, request) # send REST call end |
.send_rest(rest, request, limit = 100) ⇒ Object
56 57 58 59 60 61 62 63 64 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 90 91 |
# File 'lib/REST/Rest.rb', line 56 def self.send_rest(rest, request, limit=100) # loop adjusts for throttling if needed results = [] while true response = rest.request(request) case response.code when "204" return true when "200" # if REST success then parse body to JSON response, headers = [ JSON.parse(response.body), response.each_header.to_h ] if response.has_key?("items") && response['items'].length() > 0 results.concat(response['items']) next_page = CiscoWebex::Rest.find_next_page(headers) if next_page rest, request = next_page else return results # return array of hashes end else return response end when "429" # sleep for throttling time STDERR.puts "\tThrottled CiscoWebex::Rest(): #{response.each_header.to_h['Retry-After'] + 1}sec..." sleep(response.each_header.to_h['Retry-After'] + 1) else STDERR.puts "Error: CiscoWebex::Rest(): #{response} - #{response.body}" return false end end end |