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.
24 25 26 |
# File 'lib/adp/api_connection.rb', line 24 def initialize( config = nil ) self.connection_configuration = config; end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
21 22 23 |
# File 'lib/adp/api_connection.rb', line 21 def access_token @access_token end |
#connection_configuration ⇒ Object
Returns the value of attribute connection_configuration.
18 19 20 |
# File 'lib/adp/api_connection.rb', line 18 def connection_configuration @connection_configuration end |
#state ⇒ Object
Returns the value of attribute state.
20 21 22 |
# File 'lib/adp/api_connection.rb', line 20 def state @state end |
#token_expiration ⇒ Object
Returns the value of attribute token_expiration.
19 20 21 |
# File 'lib/adp/api_connection.rb', line 19 def token_expiration @token_expiration end |
Instance Method Details
#connect ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/adp/api_connection.rb', line 28 def connect if self.connection_configuration.nil? raise ConnectionException, "Configuration is empty or not found" end self.access_token = get_access_token() end |
#disconnect ⇒ Object
37 38 39 |
# File 'lib/adp/api_connection.rb', line 37 def disconnect self.access_token = nil end |
#get_access_token ⇒ Object
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 92 |
# File 'lib/adp/api_connection.rb', line 54 def get_access_token token = self.access_token; result = nil; if is_connected_indicator? if self.connection_configuration.nil? raise ConnectionException, "Config error: Configuration is empty or not found" end if (self.connection_configuration.grantType.nil?) raise ConnectionException, "Config error: Grant Type is empty or not known" end if (self.connection_configuration.tokenServerURL.nil?) raise ConnectionException, "Config error: tokenServerURL is empty or not known" end if (self.connection_configuration.clientID.nil?) raise ConnectionException, "Config error: clientID is empty or not known" end if (self.connection_configuration.clientSecret.nil?) raise ConnectionException, "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 ConnectionException, "Connection error: #{result['error_description']}" end token end |
#get_adp_data(product_url) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/adp/api_connection.rb', line 95 def get_adp_data(product_url) raise ConnectionException, "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 ConnectionException, "Connection error: #{data['error']}, #{data['error_description']}" unless data["error"].nil? return data end |
#is_connected_indicator? ⇒ Boolean
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/adp/api_connection.rb', line 42 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
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 153 154 155 156 157 158 159 160 161 |
# File 'lib/adp/api_connection.rb', line 116 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"]}") useragent = "adp-connection-ruby/#{Adp::Connection::VERSION}" 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); log.debug("User agent: #{useragent}") 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 http.cert_store = OpenSSL::X509::Store.new http.cert_store.add_file(self.connection_configuration.sslCaPath) 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.initialize_http_header({"User-Agent" => useragent }) request["Content-Type"] = content_type # add credentials if available request["Authorization"] = unless .nil? response = JSON.parse(http.request(request).body) end |