Sha256: 48b860b501ce4e9a4124b53fac2462a88738884fd24fa6c5a8e352fd629c1885

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# lib/fxdatapi/convert_all.rb

# frozen_string_literal: true

require 'net/http'
require 'json'
require_relative 'auth'

module Fxdatapi
  class ConvertAll
    BASE_URL = 'https://fxdatapi.com/v1/convert_all'

    def self.convert_all(username, password, base_currency, amount, date)
      login_result = Authentication.login(username, password)
      cookie = "user_type=#{login_result['user_type']}; username=#{username}"

      uri = URI(BASE_URL)
      request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Cookie' => cookie)

      request.body = {
        username: username,
        base_currency: base_currency,
        amount: amount,
        date: date,
      }.to_json

      response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') { |http| http.request(request) }

      case response
      when Net::HTTPSuccess
        JSON.parse(response.body)
      else
        raise "Failed to fetch conversion for all currencies: #{response.code} #{response.message}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fxdatapi-0.1.3 lib/fxdatapi/convert_all.rb
fxdatapi-0.1.2 lib/fxdatapi/convert_all.rb