Class: Adp::Connection::ApiConnection
- Inherits:
-
Object
- Object
- Adp::Connection::ApiConnection
- Defined in:
- lib/adp/api_connection.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#connection_configuration ⇒ Object
Returns the value of attribute connection_configuration.
-
#state ⇒ Object
Returns the value of attribute state.
-
#token_expiration ⇒ Object
Returns the value of attribute token_expiration.
Instance Method Summary collapse
- #connect ⇒ Object
- #disconnect ⇒ Object
- #get_access_token ⇒ Object
- #get_adp_data(product_url) ⇒ Object
-
#initialize(config = nil) ⇒ ApiConnection
constructor
A new instance of ApiConnection.
- #is_connected_indicator? ⇒ Boolean
- #send_web_request(url, data = {}, authorization = nil, content_type = nil, method = nil) ⇒ Object
Constructor Details
#initialize(config = nil) ⇒ ApiConnection
Returns a new instance of ApiConnection.
23 24 25 |
# File 'lib/adp/api_connection.rb', line 23 def initialize( config = nil ) self.connection_configuration = config; end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
20 21 22 |
# File 'lib/adp/api_connection.rb', line 20 def access_token @access_token end |
#connection_configuration ⇒ Object
Returns the value of attribute connection_configuration.
17 18 19 |
# File 'lib/adp/api_connection.rb', line 17 def connection_configuration @connection_configuration end |
#state ⇒ Object
Returns the value of attribute state.
19 20 21 |
# File 'lib/adp/api_connection.rb', line 19 def state @state end |
#token_expiration ⇒ Object
Returns the value of attribute token_expiration.
18 19 20 |
# File 'lib/adp/api_connection.rb', line 18 def token_expiration @token_expiration end |
Instance Method Details
#connect ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/adp/api_connection.rb', line 27 def connect if self.connection_configuration.nil? raise ADPConnectionException, "Configuration is empty or not found" end self.access_token = get_access_token() end |
#disconnect ⇒ Object
36 37 38 |
# File 'lib/adp/api_connection.rb', line 36 def disconnect self.access_token = null; end |
#get_access_token ⇒ Object
53 54 55 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/adp/api_connection.rb', line 53 def get_access_token token = self.access_token; result = nil; if is_connected_indicator? if self.connection_configuration.nil? raise ADPConnectionException, "Config error: Configuration is empty or not found" end if (self.connection_configuration.grantType.nil?) raise ADPConnectionException, "Config error: Grant Type is empty or not known" end if (self.connection_configuration.tokenServerURL.nil?) raise ADPConnectionException, "Config error: tokenServerURL is empty or not known" end if (self.connection_configuration.clientID.nil?) raise ADPConnectionException, "Config error: clientID is empty or not known" end if (self.connection_configuration.clientSecret.nil?) raise ADPConnectionException, "Config error: clientSecret is empty or not known" end end data = { "client_id" => self.connection_configuration.clientID, "client_secret" => self.connection_configuration.clientSecret, "grant_type" => self.connection_configuration.grantType }; result = send_web_request(self.connection_configuration.tokenServerURL, data ); if result["error"].nil? then token = AccessToken.new(result) else raise ADPConnectionException, "Connection error: #{result['error_description']}" end token end |
#get_adp_data(product_url) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/adp/api_connection.rb', line 94 def get_adp_data(product_url) raise ADPConnectionException, "Connection error: can't get data, not connected" if (self.access_token.nil? || !is_connected_indicator?) = "#{self.access_token.token_type} #{self.access_token.token}" data = { "client_id" => self.connection_configuration.clientID, "client_secret" => self.connection_configuration.clientSecret, "grant_type" => self.connection_configuration.grantType, "code" => self.connection_configuration., "redirect_uri" => self.connection_configuration.redirectURL }; data = send_web_request(product_url, data, , 'application/json', 'GET') raise ADPConnectionException, "Connection error: #{data['error']}, #{data['error_description']}" unless data["error"].nil? return data end |
#is_connected_indicator? ⇒ Boolean
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/adp/api_connection.rb', line 41 def is_connected_indicator? is_connected = false; if (!self.access_token.nil?) # valid token to check if expired is_connected = true if Time.new() < self.access_token.expires_on end return is_connected end |
#send_web_request(url, data = {}, authorization = nil, content_type = nil, method = nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/adp/api_connection.rb', line 115 def send_web_request(url, data={}, =nil, content_type=nil, method=nil) data ||= {} content_type ||= "application/x-www-form-urlencoded" method ||= 'POST' log = Logger.new(STDOUT) log.level = Logger::DEBUG log.debug("URL: #{url}") log.debug("Client ID: #{data["client_id"]}") log.debug("Client Secret: #{data["client_secret"]}") log.debug("Grant Type: #{data["grant_type"]}") uri = URI.parse( url ); pem = File.read("#{self.connection_configuration.sslCertPath}"); key = File.read(self.connection_configuration.sslKeyPath); http = Net::HTTP.new(uri.host, uri.port); if (!self.connection_configuration.sslCertPath.nil?) http.use_ssl = true http.cert = OpenSSL::X509::Certificate.new( pem ); http.key = OpenSSL::PKey::RSA.new(key, self.connection_configuration.sslKeyPass); http.verify_mode = OpenSSL::SSL::VERIFY_PEER end if method.eql?('POST') request = Net::HTTP::Post.new(uri.request_uri) request.set_form_data( data ); else request = Net::HTTP::Get.new(uri.request_uri) end request["Content-Type"] = content_type # add credentials if available request["Authorization"] = unless .nil? response = JSON.parse(http.request(request).body) end |