Sha256: c862a23f9ab6a7ec4e6d12c777d620357664f932631e0b0909bd1a1481bfe1e0

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require "faraday"
require "tty-table"

module NokoCli
  # This is an entry resource, which could be listed
  class Entries
    BASE_URL = "https://api.nokotime.com/v2"
    NOKO_TOKEN = ENV.fetch("NOKO_TOKEN", nil)

    def initialize(adapter: Faraday.default_adapter, stubs: nil)
      @adapter = adapter
      @stubs = stubs
    end

    def list
      puts TTY::Table.new(headers, rows).render(:ascii, resize: true)
    end

    private

    def conn
      @conn ||= Faraday.new({ url: BASE_URL, params: { noko_token: NOKO_TOKEN } }) do |f|
        unless @stubs
          f.request :json
          f.response :json, content_type: "application/json"
        end
        f.adapter @adapter, @stubs
      end
    end

    def current_user_entries
      conn.get("current_user/entries").body
    end

    def headers
      %w[date minutes project description]
    end

    def rows
      current_user_entries.map { |entry| row(entry) }
    end

    def row(entry)
      [entry["date"], entry["minutes"], entry["project"]["name"], entry["description"]]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
noko_cli-0.2.5 lib/noko_cli/entries.rb
noko_cli-0.2.4 lib/noko_cli/entries.rb
noko_cli-0.2.3 lib/noko_cli/entries.rb
noko_cli-0.2.1 lib/noko_cli/entries.rb
noko_cli-0.2.0 lib/noko_cli/entries.rb