class Fretala
Constants
- FRETALA_PRODUCTION_URL
- FRETALA_SANDBOX_URL
Public Class Methods
new(environment, settings)
click to toggle source
# File lib/fretala.rb, line 19 def initialize(environment, settings) throw 'environment must be sandbox or production' if not ['production', 'sandbox'].include? environment @environment = environment if @environment == 'production' @url = FRETALA_PRODUCTION_URL else @url = FRETALA_SANDBOX_URL end @clientId = settings['clientId'] @clientSecret = settings['clientSecret'] @username = settings['username'] @password = settings['password'] @token = '' end
Public Instance Methods
authenticate()
click to toggle source
# File lib/fretala.rb, line 35 def authenticate() data = { 'grant_type' => 'password', 'username' => @username, 'password' => @password }; res = performRequest('POST', '/authenticate', data.to_json, true) @token = res['access_token'] return @token end
cost(route)
click to toggle source
# File lib/fretala.rb, line 66 def cost(route) performRequest('POST', '/fretes/cost', route.to_json) end
deleteCard(cardToken)
click to toggle source
# File lib/fretala.rb, line 56 def deleteCard(cardToken) authenticate() performRequest('DELETE', '/cards/'+cardToken) end
getCards()
click to toggle source
# File lib/fretala.rb, line 46 def getCards() authenticate() performRequest('GET', '/cards') end
insertCard(card)
click to toggle source
# File lib/fretala.rb, line 51 def insertCard(card) authenticate() performRequest('POST', '/cards', card.to_json) end
insertFrete(frete)
click to toggle source
# File lib/fretala.rb, line 61 def insertFrete(frete) authenticate() performRequest('POST', '/fretes', frete.to_json) end
Private Instance Methods
buildHeaders(auth=false)
click to toggle source
# File lib/fretala.rb, line 70 def buildHeaders(auth=false) headers = { 'Content-Type' => 'application/json' } if(auth) headers['Authorization'] = 'Basic ' + Base64.encode64(@clientId + ':' + @clientSecret) elsif(@token != '') headers['Authorization'] = 'Bearer ' + @token end return headers; end
performRequest(type, path, data='', auth=false)
click to toggle source
# File lib/fretala.rb, line 82 def performRequest(type, path, data='', auth=false) http = Net::HTTP.new(@url, 443) http.use_ssl = true headers = buildHeaders(auth) if type == 'POST' request = Net::HTTP::Post.new(path, headers) request.body = data elsif type == 'GET' request = Net::HTTP::Get.new(path, headers) elsif type == 'PUT' request = Net::HTTP::Put.new(path, headers) request.body = data elsif type == 'DELETE' request = Net::HTTP::Delete.new(path, headers) else throw 'Request type ' + type + ' is not valid' end response = http.request(request) json = JSON.parse(response.body) if response.code != '200' && response.code != '204' errMsg = json.has_key?('message') ? json['message'] : json['error_description'] if response.code == '422' raise ValidationError, errMsg elsif response.code == '400' raise BadRequestError, errMsg elsif response.code == '500' raise InternalError, errMsg else raise Error, errMsg end end @token = '' return json end