Sha256: 8792a9c3017e3d28a9c6870f8971aee12c64ab25309a15d4dfe0de6a8fe7df69

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require_relative 'api_routes'
require_relative 'http_client'

module ApiClient
  include ApiRoutes
  def create_session(hostname, params = {})
    uri = session_uri(hostname)
    response = Uffizzi::HttpClient.make_request(uri, :post, false, params)

    build_response(response)
  end

  def destroy_session(hostname)
    uri = session_uri(hostname)
    response = Uffizzi::HttpClient.make_request(uri, :delete, true)

    build_response(response)
  end

  def fetch_projects(hostname)
    uri = projects_uri(hostname)
    response = Uffizzi::HttpClient.make_request(uri, :get, true)

    build_response(response)
  end

  def print_errors(errors)
    puts errors.keys.reduce([]) { |acc, key| acc.push(errors[key]) }
  end

  private

  def build_response(response)
    {
      body: response_body(response),
      headers: response_cookie(response),
      code: response.class,
    }
  end

  def response_body(response)
    return nil if response.body.nil?

    JSON.parse(response.body, symbolize_names: true)
  end

  def response_cookie(response)
    cookies = response.to_hash['set-cookie']
    return nil if cookies.nil?

    cookie_content = cookies.first
    cookie = cookie_content.split(';').first
    Uffizzi::ConfigFile.rewrite_cookie(cookie) if Uffizzi::ConfigFile.exists?

    cookie
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
uffizzi-cli-0.1.4.3 lib/uffizzi/clients/api/api_client.rb
uffizzi-cli-0.1.4.2 lib/uffizzi/clients/api/api_client.rb
uffizzi-cli-0.1.3 lib/uffizzi/clients/api/api_client.rb