Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(entry): Add new entry #66

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
GEM_NAME: "noko_cli"
strategy:
matrix:
ruby: [2.7]
ruby: [3.1]
steps:
- name: Update Release PR.
uses: google-github-actions/release-please-action@v3
Expand Down
12 changes: 11 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
noko_cli (0.2.4)
faraday (>= 2.3, < 2.12)
tty-prompt (~> 0.23)
tty-table (~> 0.12.0)
zeitwerk (~> 2.6)

Expand Down Expand Up @@ -79,13 +80,22 @@ GEM
strings-ansi (0.2.0)
strscan (3.1.0)
tty-color (0.6.0)
tty-cursor (0.7.1)
tty-prompt (0.23.1)
pastel (~> 0.8)
tty-reader (~> 0.8)
tty-reader (0.9.0)
tty-cursor (~> 0.7)
tty-screen (~> 0.8)
wisper (~> 2.0)
tty-screen (0.8.1)
tty-table (0.12.0)
pastel (~> 0.8)
strings (~> 0.2.0)
tty-screen (~> 0.8)
unicode-display_width (2.5.0)
unicode_utils (1.4.0)
wisper (2.0.1)
zeitwerk (2.6.17)

PLATFORMS
Expand All @@ -103,4 +113,4 @@ DEPENDENCIES
rubocop-rspec (~> 2.20.0)

BUNDLED WITH
2.3.26
2.4.4
13 changes: 11 additions & 2 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ require "noko_cli"

# Examples

# Entries
# NokoCli::Entries.new.list
# App
# config = NokoCli::Config.new
# run = NokoCli::Run.call(["add"])

# Get data from noko's API
# projects = NokoCli::Project.new
# projects.list
# tags = NokoCli::Tag.new
# tags.list
# entries = NokoCli::Entry.new
# entries.list

require "irb"
IRB.start(__FILE__)
2 changes: 1 addition & 1 deletion exe/noko_cli
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

require "noko_cli"

NokoCli::Entries.new.list
NokoCli::Run.call(ARGV)
31 changes: 31 additions & 0 deletions lib/noko_cli/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "faraday"

module NokoCli
class Config # :nodoc:
attr_reader :adapter, :stubs, :noko_token, :url

def initialize(
adapter: Faraday.default_adapter,
stubs: nil,
noko_token: ENV.fetch("NOKO_TOKEN", nil)
)
@adapter = adapter
@stubs = stubs
@noko_token = noko_token
@url = "https://api.nokotime.com/v2"
end

def conn
@conn ||=
Faraday.new({ url:, params: { noko_token: } }) do |f|
unless stubs
f.request :json
f.response :json, content_type: "application/json"
end
f.adapter adapter, stubs
end
end
end
end
49 changes: 0 additions & 49 deletions lib/noko_cli/entries.rb

This file was deleted.

33 changes: 33 additions & 0 deletions lib/noko_cli/entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require "tty-table"

module NokoCli
class Entry # :nodoc:
def initialize(config = Config.new)
@conn = config.conn
end

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

private

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
32 changes: 32 additions & 0 deletions lib/noko_cli/form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "tty-prompt"

module NokoCli
class Form # :nodoc:
def call
result = show_form
concat_description(result)
end

# rubocop:disable Metrics/AbcSize
def show_form
prompt = TTY::Prompt.new
date = Time.new.strftime("%Y-%m-%d")

prompt.collect do
key(:date).ask("*Date (#{date})", default: date, required: true)
key(:minutes).ask("*Time (in minutes)", convert: :int, required: true)
key(:project_name).select("*Select a project", Project.new.list, filter: true, required: true)
key(:description).ask("*Description", required: true)
key(:tags).multi_select("Select some tags?", Tag.new.list, filter: true, cycle: true)
end
end
# rubocop:enable Metrics/AbcSize

def concat_description(info)
info[:description] = [info[:description]].concat(info[:tags]).join(" ")
info
end
end
end
13 changes: 13 additions & 0 deletions lib/noko_cli/project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module NokoCli
class Project # :nodoc:
def initialize(config = Config.new)
@conn = config.conn
end

def list
@conn.get("projects").body.map { |p| p["name"] }
end
end
end
31 changes: 31 additions & 0 deletions lib/noko_cli/run.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module NokoCli
class Run # :nodoc:
def self.call(options)
new(options).call
end

def initialize(options)
@options = options
end

def call
return create_entry if add?

Entry.new.list
end

private

def create_entry
require "json"
result = Form.new.call
puts JSON.pretty_generate(result)
end

def add?
@options[0] == "add"
end
end
end
13 changes: 13 additions & 0 deletions lib/noko_cli/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module NokoCli
class Tag # :nodoc:
def initialize(config = Config.new)
@conn = config.conn
end

def list
@conn.get("tags").body.map { |tag| tag["formatted_name"] }
end
end
end
3 changes: 2 additions & 1 deletion noko_cli.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
DESC
spec.homepage = "https://github.com/juanvqz/noko_cli"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"
spec.required_ruby_version = ">= 3.1"
spec.metadata["allowed_push_host"] = "https://rubygems.org"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
Expand All @@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
spec.metadata = { "rubygems_mfa_required" => "true" }

spec.add_dependency "faraday", ">= 2.3", "< 2.12"
spec.add_dependency "tty-prompt", "~> 0.23"
spec.add_dependency "tty-table", "~> 0.12.0"
spec.add_dependency "zeitwerk", "~> 2.6"

Expand Down
71 changes: 71 additions & 0 deletions spec/fixtures/projects/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[
{
"id": 1,
"name": "Internal",
"description": null,
"billing_increment": 15,
"enabled": true,
"billable": false,
"color": "#86ac2a",
"created_at": "2022-06-14T03:17:13Z",
"updated_at": "2022-08-23T20:21:06Z",
"group": null,
"minutes": 120,
"billable_minutes": 0,
"unbillable_minutes": 120,
"invoiced_minutes": 0,
"uninvoiced_minutes": 0,
"remaining_minutes": null,
"budgeted_minutes": null,
"invoices": [],
"import": null,
"participants": [
{
"id": 1,
"email": "me@juanvasquez.com",
"first_name": "Juan",
"last_name": "Vasquez",
"profile_image_url": "https://usercontent.nokoti.me/public_uploads/avatar/b4262edf3e3a5ff4aa80ccf58d455c8666f47df0/88f6c1eb3e4f94255deac05154922dfe.jpg",
"url": "https://api.nokotime.com/v2/users/1"
}
],
"entries": 2,
"entries_url": "https://api.nokotime.com/v2/projects/1/entries",
"expenses": 0,
"expenses_url": "https://api.nokotime.com/v2/projects/1/expenses",
"url": "https://api.nokotime.com/v2/projects/1",
"merge_url": "https://api.nokotime.com/v2/projects/1/merge",
"archive_url": "https://api.nokotime.com/v2/projects/1/archive",
"unarchive_url": "https://api.nokotime.com/v2/projects/1/unarchive"
},
{
"id": 2,
"name": "Learning",
"description": null,
"billing_increment": 15,
"enabled": true,
"billable": true,
"color": "#13a480",
"created_at": "2022-06-14T03:17:13Z",
"updated_at": "2022-06-14T03:17:13Z",
"group": null,
"minutes": 0,
"billable_minutes": 0,
"unbillable_minutes": 0,
"invoiced_minutes": 0,
"uninvoiced_minutes": 0,
"remaining_minutes": null,
"budgeted_minutes": null,
"invoices": [],
"import": null,
"participants": [],
"entries": 0,
"entries_url": "https://api.nokotime.com/v2/projects/2/entries",
"expenses": 0,
"expenses_url": "https://api.nokotime.com/v2/projects/2/expenses",
"url": "https://api.nokotime.com/v2/projects/2",
"merge_url": "https://api.nokotime.com/v2/projects/2/merge",
"archive_url": "https://api.nokotime.com/v2/projects/2/archive",
"unarchive_url": "https://api.nokotime.com/v2/projects/2/unarchive"
}
]
28 changes: 28 additions & 0 deletions spec/fixtures/tags/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"id": 1,
"name": "unbillable",
"billable": false,
"formatted_name": "#unbillable*",
"import": null,
"entries": 0,
"entries_url": "https://api.nokotime.com/v2/tags/1/entries",
"url": "https://api.nokotime.com/v2/tags/1",
"created_at": "2022-06-14T03:17:16Z",
"updated_at": "2023-01-21T19:59:13Z",
"merge_url": "https://api.nokotime.com/v2/tags/1/merge"
},
{
"id": 2,
"name": "sales",
"billable": false,
"formatted_name": "#sales*",
"import": null,
"entries": 0,
"entries_url": "https://api.nokotime.com/v2/tags/2/entries",
"url": "https://api.nokotime.com/v2/tags/2",
"created_at": "2022-06-14T03:17:16Z",
"updated_at": "2023-01-21T19:59:13Z",
"merge_url": "https://api.nokotime.com/v2/tags/2/merge"
}
]
Loading
Loading