module MeApi class Client < Ac::Base BASE_URL = "https://melhorenvio.com.br" Rate = Data.define(:from, :to, :price, :delivery_range, :service_name, :carrier_name, :carrier_logo_url) attr_reader :default_height, :default_width, :default_length def initialize(access_token = nil, default_height: 24, default_width: 16, default_length: 10) @default_height = default_height @default_width = default_width @default_length = default_length super access_token end def rates(from:, to:, weight_kg:, contents_value_brl:, height_cm: default_height, width_cm: default_width, length_cm: default_length) body = { "from" => { "postal_code" => from }, "to" => { "postal_code" => to }, "package" => { "weight" => weight_kg, "height" => height_cm, "width" => width_cm, "length" => length_cm }, "options" => { "insurance_value" => contents_value_brl, "receipt" => false, "own_hand" => false } } response = post "api/v2/me/shipment/calculate", body: JSON.dump(body) if response.json.is_a?(Array) rates = response.json else rates = [response.json] end rates.map do |rate| Rate.new( from: from, to: to, price: rate["price"].to_f, delivery_range: rate["delivery_range"], service_name: rate["name"], carrier_name: rate["company"]["name"], carrier_logo_url: rate["company"]["picture"] ) end end def refresh_token(client_id:, client_secret:, refresh_token:) body = { grant_type: "refresh_token", client_id: client_id, client_secret: client_secret, refresh_token: refresh_token } response = post "/oauth/token", body: JSON.dump(body), headers: { "Content-Type": "application/json" } end def authorize client_id:, client_secret:, code:, redirect_url: body = { grant_type: "authorization_code", client_id: client_id, client_secret: client_secret, code: code, redirect_uri: redirect_url } response = post "/oauth/token", body: JSON.dump(body), headers: { "Content-Type": "application/json" } end end end