Class: AvrijAnalyticsUtility::APIConnector
- Inherits:
-
Object
- Object
- AvrijAnalyticsUtility::APIConnector
- Defined in:
- lib/AvrijAnalyticsUtility/APIConnector.rb
Instance Method Summary collapse
-
#authenticate(authentication_endpoint) ⇒ Object
Request for API authentication.
-
#getRequest(apiEndpoint, headerHash, queryHash) ⇒ Object
Perform API request.
-
#initialize(base_uri) ⇒ APIConnector
constructor
Constructor.
-
#setBody(bodyHash) ⇒ Object
Set request body hash.
-
#setHeader(headerHash) ⇒ Object
Set request header hash.
-
#setQuery(queryHash) ⇒ Object
Set request query hash.
Constructor Details
#initialize(base_uri) ⇒ APIConnector
Constructor
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 8 def initialize(base_uri) @base_uri = base_uri @username = nil @password = nil @auth_token = nil @auth_value = nil @header_hash = nil @query_hash = nil @body_hash = nil = nil end |
Instance Method Details
#authenticate(authentication_endpoint) ⇒ Object
Request for API authentication
Attributes
-
authentication_endpoint- API authentication end point
Examples
- authentication_endpoint
-
given the complete uri : www.company.com/auth
-
the base uri is : www.company.com
-
the authentication endpoint is : auth
Steps for Authentication
myConnector = APIConnector.new(base_uri) auth_Header =
"Authorization" => "Bearer undefined"
auth_body =
"username" => "myusername",
"password" => "mypassword"
myConnector.setHeader(auth_Header) myConnector.setBody(auth_body) if myConnector.authenticate(auth_endpoint)
<todo>
end
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 126 def authenticate(authentication_endpoint) authentication_uri = @base_uri + "/" + authentication_endpoint = {headers: @header_hash, body: @body_hash} authentication_result = HTTParty.post(authentication_uri, ) if authentication_result.code == 200 # success @auth_token = authentication_result.parsed_response["token"] @auth_value = "Bearer " + @auth_token @header_hash = { "authorization" => @auth_value } result = true else result = false end return result end |
#getRequest(apiEndpoint, headerHash, queryHash) ⇒ Object
Perform API request
Attributes
-
apiEndpoint- API end point -
headerHash- Hash definition of header options -
queryHash- Hash definition of query options
Examples
- apiEndpoint
-
Given the complete browser’s uri : www.company.com:8080/api/endpoint1/endpoint1_1
-
given the base uri : www.company.com:8080
-
then endpoint is : api/endpoint1/endpoint1_1
- headerHash
-
my_header:
"Key1" => "value1", "key2" => "value2"
- headerQuery
-
my_query
"key1" => "value1", "key2" => "value2"
44 45 46 47 48 49 50 51 |
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 44 def getRequest(apiEndpoint,headerHash,queryHash) @header_hash = headerHash @query_hash = queryHash request_uri = @base_uri +"/" + apiEndpoint = {headers: @header_hash, query: @query_hash } result = HTTParty.get(request_uri,) return result end |
#setBody(bodyHash) ⇒ Object
Set request body hash
Attributes
-
bodyHash- Hash definition of body options
Examples
- bodyHash
-
my_body:
"Key1" => "value1", "key2" => "value2"
94 95 96 |
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 94 def setBody(bodyHash) @body_hash = bodyHash end |
#setHeader(headerHash) ⇒ Object
Set request header hash
Attributes
-
headerHash- Hash definition of header options
Examples
- headerHash
-
my_header:
"Key1" => "value1", "key2" => "value2"
64 65 66 |
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 64 def setHeader(headerHash) @header_hash = headerHash end |
#setQuery(queryHash) ⇒ Object
Set request query hash
Attributes
-
queryHash- Hash definition of query options
Examples
- queryHash
-
my_query:
"Key1" => "value1", "key2" => "value2"
79 80 81 |
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 79 def setQuery(queryHash) @query_hash = queryHash end |