class AvrijAnalyticsUtility::APIConnector

Public Class Methods

new(base_uri) click to toggle source

Constructor

# 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
  @request_options  = nil
end

Public Instance Methods

authenticate(authentication_endpoint) click to toggle source

Request for API authentication

Attributes

  • authentication_endpoint - API authentication end point

Examples

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

# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 169
def authenticate(authentication_endpoint)
    authentication_uri = @base_uri + "/" + authentication_endpoint
    authentication_options = {headers: @header_hash, body: @body_hash}
    authentication_result = HTTParty.post(authentication_uri, authentication_options)
    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
getAuthValue() click to toggle source

Returns the header authentication value including the token

# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 23
def getAuthValue
  return @auth_value
end
getRequest(apiEndpoint,headerHash,queryHash) click to toggle source

Perform API GET 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” #}

# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 52
def getRequest(apiEndpoint,headerHash,queryHash)
    @header_hash = headerHash
    @query_hash  = queryHash
    request_uri = @base_uri +"/" + apiEndpoint
    @request_options = {headers: @header_hash, query: @query_hash }
    result = HTTParty.get(request_uri,@request_options)
    return result
end
jsonString_to_jsonFile(jsonString,jsonOutputFile) click to toggle source

Send JSON string to JSON file

Attributes

  • jsonString - JSON string

  • jsonOutputFile - JSON output file

# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 193
def jsonString_to_jsonFile(jsonString,jsonOutputFile)
  File.open(jsonOutputFile,"w") do |f|
      f.write(jsonString.to_json)            
  end
end
postRequest(apiEndpoint,headerHash,queryHash) click to toggle source

Perform API POST 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” #}

# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 85
def postRequest(apiEndpoint,headerHash,queryHash)
    @header_hash = headerHash
    @query_hash  = queryHash
    request_uri = @base_uri +"/" + apiEndpoint
    @request_options = {headers: @header_hash, query: @query_hash }
    result = HTTParty.post(request_uri,@request_options)
    return result
end
setBody(bodyHash) click to toggle source

Set request body hash

Attributes

  • bodyHash - Hash definition of body options

Examples

#[bodyHash] - my_body: { # “Key1” => “value1”, # “key2” => “value2” # }

# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 136
def setBody(bodyHash)
    @body_hash = bodyHash
end
setHeader(headerHash) click to toggle source

Set request header hash

Attributes

  • headerHash - Hash definition of header options

Examples

#[headerHash] - my_header: { # “Key1” => “value1”, # “key2” => “value2” # }

# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 106
def setHeader(headerHash)
    @header_hash = headerHash
end
setQuery(queryHash) click to toggle source

Set request query hash

Attributes

  • queryHash - Hash definition of query options

Examples

#[queryHash] - my_query: { # “Key1” => “value1”, # “key2” => “value2” # }

# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 121
def setQuery(queryHash)
    @query_hash = queryHash
end