class BBSIA::Client

BBSIA API client class

Constants

BASE_URI

Attributes

access_token[R]
env[R]
expires_at[R]
expires_in[R]
issued_at[R]
password[R]
refresh_token[R]
user[R]

Public Class Methods

new(user, password, env = :prod) click to toggle source
# File lib/bbsia/client.rb, line 23
def initialize(user, password, env = :prod)
  @user = user
  @password = password
  @env = env
end

Public Instance Methods

auth() click to toggle source
# File lib/bbsia/client.rb, line 33
def auth
  uri = URI.parse("#{base_uri}/gmt-autorizador-api/autoriza")
  params = {
    "grant_type" => "password",
    "username" => @user,
    "password" => @password,
    "scope" => "sia:usuario"
  }

  res = Net::HTTP.post_form(uri, params)
  raise APIError, res if res.code != "200"

  data = JSON.parse(res.body)

  @access_token = data["access_token"]
  @refresh_token = data["refresh_token"]
  @expires_in = data["expires_in"]
  @issued_at = DateTime.now
  @expires_at = DateTime.rfc3339(data["data_expiracao"])

  data
end
auth?() click to toggle source
# File lib/bbsia/client.rb, line 56
def auth?
  !@access_token.nil?
end
base_uri() click to toggle source
# File lib/bbsia/client.rb, line 29
def base_uri
  BASE_URI[@env]
end
download(available_download_file) click to toggle source
# File lib/bbsia/client.rb, line 161
def download(available_download_file)
  file_id = available_download_file["id"]
  file_name = available_download_file["nome"]

  uri = URI.parse("#{base_uri}/gmt-sia-api/download/#{file_id}/#{file_name}")
  headers = { "Authorization" => "Bearer #{@access_token}" }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
  res = http.get(uri.path, headers)
  raise APIError, res if res.code != "200"

  res.body
end
download_to_path(available_download_file, path) click to toggle source
# File lib/bbsia/client.rb, line 175
def download_to_path(available_download_file, path)
  file_name = available_download_file["nome"]
  file_content = download(available_download_file)

  File.write("#{path}/#{file_name}", file_content)

  available_download_file["file_path"] = "#{path}/#{file_name}"
  available_download_file
end
get_download_metadata(file_id, file_name) click to toggle source
# File lib/bbsia/client.rb, line 90
def get_download_metadata(file_id, file_name)
  uri = URI.parse("#{base_uri}/gmt-sia-api/download/#{file_id}/#{file_name}")
  headers = { "Authorization" => "Bearer #{@access_token}" }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
  res = http.head(uri.path, headers)
  raise APIError, res if res.code != "200"

  {
    "id" => file_id,
    "name" => file_name,
    "bytes" => res["content-length"],
    "md5" => res["content-md5"]
  }
end
get_upload_contract(contract) click to toggle source
# File lib/bbsia/client.rb, line 72
def get_upload_contract(contract)
  ftas = list_uploads
  ftas.select { |f| f["nome"].include?(contract) }.first
end
list_downloads() click to toggle source
# File lib/bbsia/client.rb, line 77
def list_downloads
  uri = URI.parse("#{base_uri}/gmt-sia-api/listaDownloads")
  headers = { "Authorization" => "Bearer #{@access_token}" }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
  res = http.get(uri.path, headers)
  raise APIError, res if res.code != "200"

  data = JSON.parse(res.body)

  data["arquivos"]
end
list_uploads() click to toggle source
# File lib/bbsia/client.rb, line 60
def list_uploads
  uri = URI.parse("#{base_uri}/gmt-catalogo-api/listaUploads")
  headers = { "Authorization" => "Bearer #{@access_token}" }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
  res = http.get(uri.path, headers)
  raise APIError, res if res.code != "200"

  data = JSON.parse(res.body)
  data["remessa"]
end
pre_upload(upload_contract, file_name, file_size, file_md5) click to toggle source
# File lib/bbsia/client.rb, line 106
def pre_upload(upload_contract, file_name, file_size, file_md5)
  fta = upload_contract["fta"]
  event = upload_contract["evento"]

  uri = URI.parse("#{base_uri}/gmt-sia-api/upload/#{fta}/#{event}/#{file_name}")
  headers = {
    "Authorization" => "Bearer #{@access_token}",
    "Content-MD5" => file_md5,
    "x-gmt-content-length" => file_size.to_s
  }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
  res = http.head(uri.path, headers)
  raise APIError, res unless %w[204 206].include?(res.code)

  true
end
pre_upload_from_path(upload_contract, path) click to toggle source
# File lib/bbsia/client.rb, line 124
def pre_upload_from_path(upload_contract, path)
  file = File.open(path)
  file_name = File.basename(file.path)
  file_size = file.stat.size
  file_content = file.read
  file_md5 = Digest::MD5.hexdigest(file_content)

  pre_upload(upload_contract, file_name, file_size, file_md5)
end
query_protocols(params = {}) click to toggle source
# File lib/bbsia/client.rb, line 185
def query_protocols(params = {})
  uri = URI.parse("#{base_uri}/gmt-protocolo-api/listaProtocolos")
  headers = { "Authorization" => "Bearer #{@access_token}", "Content-Type" => "application/json" }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
  res = http.post(uri.path, params.to_json, headers)
  raise APIError, res if res.code != "200"

  JSON.parse(res.body)
end
upload(upload_contract, file_name, file_size, file_md5, file_content) click to toggle source
# File lib/bbsia/client.rb, line 134
def upload(upload_contract, file_name, file_size, file_md5, file_content)
  fta = upload_contract["fta"]
  event = upload_contract["evento"]

  uri = URI.parse("#{base_uri}/gmt-sia-api/upload/#{fta}/#{event}/#{file_name}")
  headers = {
    "Authorization" => "Bearer #{@access_token}", "Content-Type" => "application/octet-stream",
    "Content-MD5" => file_md5, "Content-Length" => file_size.to_s
  }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
  res = http.put(uri.path, file_content, headers)
  raise APIError, res unless %w[200 201].include?(res.code)

  JSON.parse(res.body)
end
upload_from_path(upload_contract, path) click to toggle source
# File lib/bbsia/client.rb, line 151
def upload_from_path(upload_contract, path)
  file = File.open(path)
  file_name = File.basename(file.path)
  file_size = file.stat.size
  file_content = file.read
  file_md5 = Digest::MD5.hexdigest(file_content)

  upload(upload_contract, file_name, file_size, file_md5, file_content)
end