class OpsourceClient::Client
Constants
- API_ENDPOINT
Attributes
admin_password[RW]
admin_username[RW]
api_endpoint[RW]
organization_id[RW]
Public Class Methods
new()
click to toggle source
# File lib/opsource_client/client.rb, line 16 def initialize self.api_endpoint = API_ENDPOINT self.organization_id = '123456' self.admin_username = 'USER' self.admin_password = 'PWD' end
Public Instance Methods
all_natrules(params)
click to toggle source
# File lib/opsource_client/network/natrule.rb, line 4 def all_natrules(params) req_params = {:net_id => ''}.merge(params) self.get("/network/#{req_params[:net_id]}/natrule", nil) end
api_base_url()
click to toggle source
# File lib/opsource_client/client.rb, line 23 def api_base_url "#{api_endpoint}/#{@org_id}" end
create_natrule(params)
click to toggle source
# File lib/opsource_client/network/natrule.rb, line 9 def create_natrule(params) req_params = {:net_id => '', :sourceIp => '', :name => ''}.merge(params) self.post("/network/#{req_params[:net_id]}/natrule", create_natrule_request_xml(req_params)) end
create_natrule_request_xml(params)
click to toggle source
# File lib/opsource_client/network/natrule.rb, line 19 def create_natrule_request_xml(params) xml = xml_header xml += '<NatRule xmlns="http://oec.api.opsource.net/schemas/network">' xml += "<name>#{params[:name]}</name> <sourceIp>#{params[:sourceIp]}</sourceIp>" xml += "</NatRule>" xml end
delete(params)
click to toggle source
# File lib/opsource_client/network/natrule.rb, line 14 def delete(params) req_params = {:net_id => '', :natrule_id => ''}.merge(params) self.get("/network/#{req_params[:net_id]}/natrule/#{req_params[:natrule_id]}?delete", nil) end
Protected Instance Methods
get(endpoint, request_xml = nil)
click to toggle source
# File lib/opsource_client/client.rb, line 56 def get(endpoint, request_xml = nil) res = request(:get, endpoint, request_xml) handle_response res end
handle_response(response)
click to toggle source
# File lib/opsource_client/client.rb, line 61 def handle_response(response) begin h = symbolize_response(response.body) rescue OpsourceClient::Exceptions::RequestError => e raise OpsourceClient::Exceptions::ApiError.new("Received an Invalid Response. This might mean you sent an invalid request or Opsource is having issues.") rescue => e raise OpsourceClient::Exceptions::ApiError.new("There was an error while trying to connect to Opsource - #{e.inspect}") end h end
post(endpoint, request_xml = nil)
click to toggle source
# File lib/opsource_client/client.rb, line 51 def post(endpoint, request_xml = nil) res = request(:post, endpoint, request_xml) handle_response res end
request(type, endpoint, body = nil)
click to toggle source
# File lib/opsource_client/client.rb, line 29 def request(type, endpoint, body = nil) begin uri = URI.parse(api_base_url + endpoint) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true # construct the call data and access token req = Net::HTTP.const_get(type.to_s.camelize).new(uri.request_uri, initheader = {'Content-Type' =>'application/json', 'User-Agent' => 'WePay Ruby SDK'}) req.content_type = "text/xml" if body req.body = body if body req.basic_auth @username, @password response = http.request(req) rescue TimeoutError => error raise OpsourceClient::Exceptions::ConnectionError.new("Timeout error. Invalid Api URL or Opsource server is probably down: \"#{@url}\"") rescue SocketError, Errno::ECONNREFUSED => se raise OpsourceClient::Exceptions::ConnectionError.new("Socket error. Could not connect to Opsource server.") end response end
symbolize_keys(arg)
click to toggle source
# File lib/opsource_client/client.rb, line 84 def symbolize_keys(arg) case arg when Array arg.map { |elem| symbolize_keys elem } when Hash Hash[ arg.map { |key, value| k = key.is_a?(String) ? key.to_sym : key v = symbolize_keys value [k,v] }] else arg end end
symbolize_response(response)
click to toggle source
# File lib/opsource_client/client.rb, line 72 def symbolize_response(response) begin # we'll not use 'KeyToSymbol' because it doesn't symbolize the keys for node attributes opts = { 'ForceArray' => false, 'ForceContent' => false } # hash = XmlSimple.xml_in(response, opts) hash.delete_if {|k, v| k =~ /(xmlns|xmlns:ns)/ } #removing the namespaces from response return symbolize_keys(hash) rescue Exception => e raise OpsourceClient::Exceptions::RequestError.new("Impossible to convert XML to hash. Error: #{e.message}") end end
xml_header()
click to toggle source
# File lib/opsource_client/client.rb, line 100 def xml_header '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' end