require 'active_support/all' require 'fedexvichuge/version' require 'Faraday' module Fedex module Rates class Error < StandardError; end @@quote_params = { address_from: { zip: "64000", country: "MX" }, address_to: { zip: "64000", country: "MX" }, parcel: { length: 25.0, width: 28.0, height: 46.0, distance_unit: "cm", weight: 6.5, mass_unit: "kg" } } def self.get(credentials, quote_params=@@quote_params) params = " #{credentials[:key]} #{credentials[:password]} #{credentials[:account_number]} #{credentials[:meter_number]} es mx crs 13 0 0 true REGULAR_PICKUP YOUR_PACKAGING
XX #{quote_params[:address_from][:zip]} #{quote_params[:address_from][:country]}
XX #{quote_params[:address_to][:zip]} #{quote_params[:address_to][:country]} false
SENDER ACCOUNT 1 1 #{quote_params[:parcel][:mass_unit].upcase} #{quote_params[:parcel][:weight]} #{quote_params[:parcel][:length].ceil} #{quote_params[:parcel][:width].ceil} #{quote_params[:parcel][:height].ceil} #{quote_params[:parcel][:distance_unit].upcase}
" res = request(params) format(res) end def self.request(params) response = client.post do |req| req.body = params end response.body end def self.client Faraday.new( url: 'https://wsbeta.fedex.com:443/xml', headers: { 'Content-Type' => 'application/xml' } ) end def self.format(res) # xml = Nokogiri::XML(res).to_xml h = Hash.from_xml(res) r = [] h['RateReply']['RateReplyDetails'].each do |i| token = i['ServiceType'] r.push({ "price": i['RatedShipmentDetails'][0]['ShipmentRateDetail']['TotalNetChargeWithDutiesAndTaxes']['Amount'].to_f, "currency": i['RatedShipmentDetails'][0]['ShipmentRateDetail']['TotalNetChargeWithDutiesAndTaxes']['Currency'], "service_level": { "name": token.downcase.gsub('_', ' '), "token": token, } }) end r end end end