From 6a5e03f20b18cc5387605d4c889d8240e2740c02 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Fri, 20 Jan 2023 23:45:52 -0600 Subject: [PATCH 01/19] feat(entry): Add new entry Closes #2 --- Gemfile.lock | 2 +- exe/noko_cli | 2 +- lib/noko_cli/config.rb | 16 ++++++++++++++++ lib/noko_cli/entries.rb | 13 ++++++------- lib/noko_cli/run.rb | 10 ++++++++++ spec/noko_cli/entries_spec.rb | 3 ++- 6 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 lib/noko_cli/config.rb create mode 100644 lib/noko_cli/run.rb diff --git a/Gemfile.lock b/Gemfile.lock index 4b20e74..a9c58b3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -103,4 +103,4 @@ DEPENDENCIES rubocop-rspec (~> 2.20.0) BUNDLED WITH - 2.3.26 + 2.4.4 diff --git a/exe/noko_cli b/exe/noko_cli index 1bb88c2..a8105a7 100755 --- a/exe/noko_cli +++ b/exe/noko_cli @@ -3,4 +3,4 @@ require "noko_cli" -NokoCli::Entries.new.list +NokoCli::Run.call diff --git a/lib/noko_cli/config.rb b/lib/noko_cli/config.rb new file mode 100644 index 0000000..327705e --- /dev/null +++ b/lib/noko_cli/config.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "faraday" + +module NokoCli + class Config # :nodoc: + attr_reader :token, :url, :adapter, :stubs + + def initialize(adapter: Faraday.default_adapter, stubs: nil) + @token = ENV.fetch("NOKO_TOKEN", nil) + @url = "https://api.nokotime.com/v2" + @adapter = adapter + @stubs = stubs + end + end +end diff --git a/lib/noko_cli/entries.rb b/lib/noko_cli/entries.rb index 0221019..7ebf54b 100644 --- a/lib/noko_cli/entries.rb +++ b/lib/noko_cli/entries.rb @@ -6,12 +6,11 @@ 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 + def initialize(config:) + @token = config.token + @url = config.url + @adapter = config.adapter + @stubs = config.stubs end def list @@ -21,7 +20,7 @@ def list private def conn - @conn ||= Faraday.new({ url: BASE_URL, params: { noko_token: NOKO_TOKEN } }) do |f| + @conn ||= Faraday.new({ url: @url, params: { noko_token: @token } }) do |f| unless @stubs f.request :json f.response :json, content_type: "application/json" diff --git a/lib/noko_cli/run.rb b/lib/noko_cli/run.rb new file mode 100644 index 0000000..b704880 --- /dev/null +++ b/lib/noko_cli/run.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module NokoCli + class Run # :nodoc: + def self.call + @config ||= Config.new + Entries.new(config: @config).list + end + end +end diff --git a/spec/noko_cli/entries_spec.rb b/spec/noko_cli/entries_spec.rb index cb99474..60ae94e 100644 --- a/spec/noko_cli/entries_spec.rb +++ b/spec/noko_cli/entries_spec.rb @@ -8,7 +8,8 @@ let(:stubs) do stub_request("current_user/entries", response: stub_response(fixture: "entries/list")) end - let(:entries) { described_class.new(adapter: :test, stubs: stubs) } + let(:config) { NokoCli::Config.new(adapter: :test, stubs: stubs) } + let(:entries) { described_class.new(config: config) } it "includes headers" do headers = /date |minutes |project |description/ From a47ff0bb73c94d2185072b61e79f4c7ccd386850 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 00:20:09 -0600 Subject: [PATCH 02/19] chore(config): Move the faraday connection --- lib/noko_cli/config.rb | 19 +++++++++++++++---- lib/noko_cli/entries.rb | 18 +++--------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/noko_cli/config.rb b/lib/noko_cli/config.rb index 327705e..7d8289d 100644 --- a/lib/noko_cli/config.rb +++ b/lib/noko_cli/config.rb @@ -4,13 +4,24 @@ module NokoCli class Config # :nodoc: - attr_reader :token, :url, :adapter, :stubs + attr_reader :adapter, :stubs, :token, :url - def initialize(adapter: Faraday.default_adapter, stubs: nil) - @token = ENV.fetch("NOKO_TOKEN", nil) - @url = "https://api.nokotime.com/v2" + def initialize(adapter: Faraday.default_adapter, stubs: nil, token: ENV.fetch("NOKO_TOKEN", nil)) @adapter = adapter @stubs = stubs + @token = token + @url = "https://api.nokotime.com/v2" + end + + def conn + @conn ||= + Faraday.new({ url: url, params: { noko_token: token } }) do |f| + unless stubs + f.request :json + f.response :json, content_type: "application/json" + end + f.adapter adapter, stubs + end end end end diff --git a/lib/noko_cli/entries.rb b/lib/noko_cli/entries.rb index 7ebf54b..b4c931a 100644 --- a/lib/noko_cli/entries.rb +++ b/lib/noko_cli/entries.rb @@ -1,16 +1,14 @@ # frozen_string_literal: true -require "faraday" require "tty-table" module NokoCli # This is an entry resource, which could be listed class Entries + attr_reader :conn + def initialize(config:) - @token = config.token - @url = config.url - @adapter = config.adapter - @stubs = config.stubs + @conn = config.conn end def list @@ -19,16 +17,6 @@ def list private - def conn - @conn ||= Faraday.new({ url: @url, params: { noko_token: @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 From 37d5653e75279f971cec0d6fac06e5ac1c7f183a Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 16:46:53 -0600 Subject: [PATCH 03/19] Rename Entry class --- lib/noko_cli/{entries.rb => entry.rb} | 2 +- lib/noko_cli/run.rb | 2 +- spec/noko_cli/{entries_spec.rb => entry_spec.rb} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename lib/noko_cli/{entries.rb => entry.rb} (97%) rename spec/noko_cli/{entries_spec.rb => entry_spec.rb} (96%) diff --git a/lib/noko_cli/entries.rb b/lib/noko_cli/entry.rb similarity index 97% rename from lib/noko_cli/entries.rb rename to lib/noko_cli/entry.rb index b4c931a..59f9cd9 100644 --- a/lib/noko_cli/entries.rb +++ b/lib/noko_cli/entry.rb @@ -4,7 +4,7 @@ module NokoCli # This is an entry resource, which could be listed - class Entries + class Entry attr_reader :conn def initialize(config:) diff --git a/lib/noko_cli/run.rb b/lib/noko_cli/run.rb index b704880..20917ba 100644 --- a/lib/noko_cli/run.rb +++ b/lib/noko_cli/run.rb @@ -4,7 +4,7 @@ module NokoCli class Run # :nodoc: def self.call @config ||= Config.new - Entries.new(config: @config).list + Entry.new(config: @config).list end end end diff --git a/spec/noko_cli/entries_spec.rb b/spec/noko_cli/entry_spec.rb similarity index 96% rename from spec/noko_cli/entries_spec.rb rename to spec/noko_cli/entry_spec.rb index 60ae94e..d9d5281 100644 --- a/spec/noko_cli/entries_spec.rb +++ b/spec/noko_cli/entry_spec.rb @@ -3,7 +3,7 @@ require "faraday" require "json" -RSpec.describe NokoCli::Entries do +RSpec.describe NokoCli::Entry do describe "#list" do let(:stubs) do stub_request("current_user/entries", response: stub_response(fixture: "entries/list")) From 396c70e9efd771a2e17eca10a001aca3401346b2 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 19:22:22 -0600 Subject: [PATCH 04/19] Rename Entry class --- spec/noko_cli/entry_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/noko_cli/entry_spec.rb b/spec/noko_cli/entry_spec.rb index d9d5281..4051e6c 100644 --- a/spec/noko_cli/entry_spec.rb +++ b/spec/noko_cli/entry_spec.rb @@ -9,24 +9,24 @@ stub_request("current_user/entries", response: stub_response(fixture: "entries/list")) end let(:config) { NokoCli::Config.new(adapter: :test, stubs: stubs) } - let(:entries) { described_class.new(config: config) } + let(:entry) { described_class.new(config.conn) } it "includes headers" do headers = /date |minutes |project |description/ - expect { entries.list }.to output(headers).to_stdout + expect { entry.list }.to output(headers).to_stdout end it "includes first entry info" do entry_info = /2022-07-03 |60 |noko_cli |#development implementing list functionality, installing faraday/ - expect { entries.list }.to output(entry_info).to_stdout + expect { entry.list }.to output(entry_info).to_stdout end it "includes second entry info" do entry_info = /2022-07-05 |90 |noko_cli |Rendering the entry #development #testing/ - expect { entries.list }.to output(entry_info).to_stdout + expect { entry.list }.to output(entry_info).to_stdout end end end From e93a2b46309bde55d51fdd4ac422b08867a1cd2a Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 19:23:11 -0600 Subject: [PATCH 05/19] Use connection --- lib/noko_cli/entry.rb | 4 ++-- lib/noko_cli/run.rb | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/noko_cli/entry.rb b/lib/noko_cli/entry.rb index 59f9cd9..5fcfdd1 100644 --- a/lib/noko_cli/entry.rb +++ b/lib/noko_cli/entry.rb @@ -7,8 +7,8 @@ module NokoCli class Entry attr_reader :conn - def initialize(config:) - @conn = config.conn + def initialize(conn) + @conn = conn end def list diff --git a/lib/noko_cli/run.rb b/lib/noko_cli/run.rb index 20917ba..28ded7c 100644 --- a/lib/noko_cli/run.rb +++ b/lib/noko_cli/run.rb @@ -2,9 +2,14 @@ module NokoCli class Run # :nodoc: + attr_reader :config + + def initialize + @config = Config.new + end + def self.call - @config ||= Config.new - Entry.new(config: @config).list + Entry.new(config.conn).list end end end From 09c557326167eb3046a118044e9bac3e52eb2161 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 19:44:51 -0600 Subject: [PATCH 06/19] Add Tag class --- lib/noko_cli/run.rb | 9 ++++++--- lib/noko_cli/tag.rb | 15 +++++++++++++++ spec/noko_cli/tag_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 lib/noko_cli/tag.rb create mode 100644 spec/noko_cli/tag_spec.rb diff --git a/lib/noko_cli/run.rb b/lib/noko_cli/run.rb index 28ded7c..01a0783 100644 --- a/lib/noko_cli/run.rb +++ b/lib/noko_cli/run.rb @@ -2,14 +2,17 @@ module NokoCli class Run # :nodoc: - attr_reader :config - def initialize @config = Config.new + @tags = Tag.new(@config.conn).list end def self.call - Entry.new(config.conn).list + new.call + end + + def call + Entry.new(@config.conn).list end end end diff --git a/lib/noko_cli/tag.rb b/lib/noko_cli/tag.rb new file mode 100644 index 0000000..e304759 --- /dev/null +++ b/lib/noko_cli/tag.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module NokoCli + class Tag # :nodoc: + attr_reader :conn + + def initialize(conn) + @conn = conn + end + + def list + conn.get("tags").body + end + end +end diff --git a/spec/noko_cli/tag_spec.rb b/spec/noko_cli/tag_spec.rb new file mode 100644 index 0000000..8cceb7b --- /dev/null +++ b/spec/noko_cli/tag_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +RSpec.describe NokoCli::Tag do + describe "#list" do + let(:stubs) do + stub_request("tags", response: stub_response(fixture: "tags/list")) + end + let(:config) { NokoCli::Config.new(adapter: :test, stubs: stubs) } + let(:tag) { described_class.new(config.conn) } + + it "returns an array" do + expect(tag.list).to be_an_instance_of(Array) + end + + it "returns tag's attributes" do + attributes = %w[id name] + + expect(tag.list.first.keys).to match_array attributes + end + end +end From dfb62ed6f5b102577e6a8906a7f3b1cd9305e2be Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 19:53:51 -0600 Subject: [PATCH 07/19] Remove attr_writter --- lib/noko_cli/entry.rb | 4 +--- lib/noko_cli/tag.rb | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/noko_cli/entry.rb b/lib/noko_cli/entry.rb index 5fcfdd1..75cb74c 100644 --- a/lib/noko_cli/entry.rb +++ b/lib/noko_cli/entry.rb @@ -5,8 +5,6 @@ module NokoCli # This is an entry resource, which could be listed class Entry - attr_reader :conn - def initialize(conn) @conn = conn end @@ -18,7 +16,7 @@ def list private def current_user_entries - conn.get("current_user/entries").body + @conn.get("current_user/entries").body end def headers diff --git a/lib/noko_cli/tag.rb b/lib/noko_cli/tag.rb index e304759..3c487d8 100644 --- a/lib/noko_cli/tag.rb +++ b/lib/noko_cli/tag.rb @@ -2,14 +2,12 @@ module NokoCli class Tag # :nodoc: - attr_reader :conn - def initialize(conn) @conn = conn end def list - conn.get("tags").body + @conn.get("tags").body end end end From 15bb94b49e2e3c3e42956d645cf11c2e835545ad Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 19:54:17 -0600 Subject: [PATCH 08/19] Add Project class --- lib/noko_cli/project.rb | 13 +++++++++++++ spec/fixtures/projects/list.json | 10 ++++++++++ spec/noko_cli/project_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 lib/noko_cli/project.rb create mode 100644 spec/fixtures/projects/list.json create mode 100644 spec/noko_cli/project_spec.rb diff --git a/lib/noko_cli/project.rb b/lib/noko_cli/project.rb new file mode 100644 index 0000000..8bcb7ac --- /dev/null +++ b/lib/noko_cli/project.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module NokoCli + class Project # :nodoc: + def initialize(conn) + @conn = conn + end + + def list + @conn.get("projects").body + end + end +end diff --git a/spec/fixtures/projects/list.json b/spec/fixtures/projects/list.json new file mode 100644 index 0000000..4c4c653 --- /dev/null +++ b/spec/fixtures/projects/list.json @@ -0,0 +1,10 @@ +[ + { + "id": 1, + "name": "OmbuLabs" + }, + { + "id": 2, + "name": "NokoCli" + } +] diff --git a/spec/noko_cli/project_spec.rb b/spec/noko_cli/project_spec.rb new file mode 100644 index 0000000..e6094eb --- /dev/null +++ b/spec/noko_cli/project_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +RSpec.describe NokoCli::Project do + describe "#list" do + let(:stubs) do + stub_request("projects", response: stub_response(fixture: "projects/list")) + end + let(:config) { NokoCli::Config.new(adapter: :test, stubs: stubs) } + let(:project) { described_class.new(config.conn) } + + it "returns an array" do + expect(project.list).to be_an_instance_of(Array) + end + + it "returns tag's attributes" do + attributes = %w[id name] + + expect(project.list.first.keys).to match_array attributes + end + end +end From 599b0f367cecd59f813d32913dcb62f45d98b4a7 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 22:50:03 -0600 Subject: [PATCH 09/19] Update to Ruby 3.1 --- .github/workflows/release.yml | 2 +- lib/noko_cli/config.rb | 12 ++++++++---- noko_cli.gemspec | 2 +- spec/noko_cli/entry_spec.rb | 2 +- spec/noko_cli/project_spec.rb | 2 +- spec/noko_cli/tag_spec.rb | 2 +- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5c0989e..7bc6cc2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/lib/noko_cli/config.rb b/lib/noko_cli/config.rb index 7d8289d..f126a69 100644 --- a/lib/noko_cli/config.rb +++ b/lib/noko_cli/config.rb @@ -4,18 +4,22 @@ module NokoCli class Config # :nodoc: - attr_reader :adapter, :stubs, :token, :url + attr_reader :adapter, :stubs, :noko_token, :url - def initialize(adapter: Faraday.default_adapter, stubs: nil, token: ENV.fetch("NOKO_TOKEN", nil)) + def initialize( + adapter: Faraday.default_adapter, + stubs: nil, + noko_token: ENV.fetch("NOKO_TOKEN", nil) + ) @adapter = adapter @stubs = stubs - @token = token + @noko_token = noko_token @url = "https://api.nokotime.com/v2" end def conn @conn ||= - Faraday.new({ url: url, params: { noko_token: token } }) do |f| + Faraday.new({ url:, params: { noko_token: } }) do |f| unless stubs f.request :json f.response :json, content_type: "application/json" diff --git a/noko_cli.gemspec b/noko_cli.gemspec index cd250df..f0eaeb5 100644 --- a/noko_cli.gemspec +++ b/noko_cli.gemspec @@ -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 diff --git a/spec/noko_cli/entry_spec.rb b/spec/noko_cli/entry_spec.rb index 4051e6c..9db191f 100644 --- a/spec/noko_cli/entry_spec.rb +++ b/spec/noko_cli/entry_spec.rb @@ -8,7 +8,7 @@ let(:stubs) do stub_request("current_user/entries", response: stub_response(fixture: "entries/list")) end - let(:config) { NokoCli::Config.new(adapter: :test, stubs: stubs) } + let(:config) { NokoCli::Config.new(adapter: :test, stubs:) } let(:entry) { described_class.new(config.conn) } it "includes headers" do diff --git a/spec/noko_cli/project_spec.rb b/spec/noko_cli/project_spec.rb index e6094eb..aa0a7fe 100644 --- a/spec/noko_cli/project_spec.rb +++ b/spec/noko_cli/project_spec.rb @@ -5,7 +5,7 @@ let(:stubs) do stub_request("projects", response: stub_response(fixture: "projects/list")) end - let(:config) { NokoCli::Config.new(adapter: :test, stubs: stubs) } + let(:config) { NokoCli::Config.new(adapter: :test, stubs:) } let(:project) { described_class.new(config.conn) } it "returns an array" do diff --git a/spec/noko_cli/tag_spec.rb b/spec/noko_cli/tag_spec.rb index 8cceb7b..b86ce10 100644 --- a/spec/noko_cli/tag_spec.rb +++ b/spec/noko_cli/tag_spec.rb @@ -5,7 +5,7 @@ let(:stubs) do stub_request("tags", response: stub_response(fixture: "tags/list")) end - let(:config) { NokoCli::Config.new(adapter: :test, stubs: stubs) } + let(:config) { NokoCli::Config.new(adapter: :test, stubs:) } let(:tag) { described_class.new(config.conn) } it "returns an array" do From 2f8bff9bee690f2adec1de1d97906e9b3eb33786 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 22:54:17 -0600 Subject: [PATCH 10/19] Skip Tag tests --- spec/noko_cli/tag_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/noko_cli/tag_spec.rb b/spec/noko_cli/tag_spec.rb index b86ce10..4973313 100644 --- a/spec/noko_cli/tag_spec.rb +++ b/spec/noko_cli/tag_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true RSpec.describe NokoCli::Tag do + it "is a skipped example" describe "#list" do let(:stubs) do stub_request("tags", response: stub_response(fixture: "tags/list")) From 2129cec0a95ad4bfb1d1c681d8c8dbc6ee3803b0 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 22:56:09 -0600 Subject: [PATCH 11/19] Enable tests back --- spec/noko_cli/tag_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/noko_cli/tag_spec.rb b/spec/noko_cli/tag_spec.rb index 4973313..b86ce10 100644 --- a/spec/noko_cli/tag_spec.rb +++ b/spec/noko_cli/tag_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true RSpec.describe NokoCli::Tag do - it "is a skipped example" describe "#list" do let(:stubs) do stub_request("tags", response: stub_response(fixture: "tags/list")) From 52684ce15fb638ddae8950c8fa29c5086a312850 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 23:03:18 -0600 Subject: [PATCH 12/19] Fix test --- spec/fixtures/tags/list.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 spec/fixtures/tags/list.json diff --git a/spec/fixtures/tags/list.json b/spec/fixtures/tags/list.json new file mode 100644 index 0000000..218bace --- /dev/null +++ b/spec/fixtures/tags/list.json @@ -0,0 +1,10 @@ +[ + { + "id": 1, + "name": "Foo" + }, + { + "id": 2, + "name": "Bar" + } +] From 6b938134abba98abcd752cc596dc4f20bfb41b43 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 23:27:05 -0600 Subject: [PATCH 13/19] Update tags and projects fixtures --- spec/fixtures/projects/list.json | 65 +++++++++++++++++++++++++++++++- spec/fixtures/tags/list.json | 26 +++++++++++-- spec/noko_cli/project_spec.rb | 4 +- spec/noko_cli/tag_spec.rb | 7 ++-- 4 files changed, 90 insertions(+), 12 deletions(-) diff --git a/spec/fixtures/projects/list.json b/spec/fixtures/projects/list.json index 4c4c653..540af14 100644 --- a/spec/fixtures/projects/list.json +++ b/spec/fixtures/projects/list.json @@ -1,10 +1,71 @@ [ { "id": 1, - "name": "OmbuLabs" + "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": "NokoCli" + "name": "Learning Noko", + "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" } ] diff --git a/spec/fixtures/tags/list.json b/spec/fixtures/tags/list.json index 218bace..5d070e3 100644 --- a/spec/fixtures/tags/list.json +++ b/spec/fixtures/tags/list.json @@ -1,10 +1,28 @@ [ - { + { "id": 1, - "name": "Foo" + "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": "Bar" + "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" } ] diff --git a/spec/noko_cli/project_spec.rb b/spec/noko_cli/project_spec.rb index aa0a7fe..1b1d039 100644 --- a/spec/noko_cli/project_spec.rb +++ b/spec/noko_cli/project_spec.rb @@ -13,9 +13,7 @@ end it "returns tag's attributes" do - attributes = %w[id name] - - expect(project.list.first.keys).to match_array attributes + expect(project.list.first.keys.count).to eq 28 end end end diff --git a/spec/noko_cli/tag_spec.rb b/spec/noko_cli/tag_spec.rb index b86ce10..83b2fa8 100644 --- a/spec/noko_cli/tag_spec.rb +++ b/spec/noko_cli/tag_spec.rb @@ -5,6 +5,9 @@ let(:stubs) do stub_request("tags", response: stub_response(fixture: "tags/list")) end + let(:tag_attributes) do + %w[id name billable formatted_name import entries entries_url url created_at updated_at merge_url] + end let(:config) { NokoCli::Config.new(adapter: :test, stubs:) } let(:tag) { described_class.new(config.conn) } @@ -13,9 +16,7 @@ end it "returns tag's attributes" do - attributes = %w[id name] - - expect(tag.list.first.keys).to match_array attributes + expect(tag.list.first.keys).to match_array tag_attributes end end end From a27b33eafdf1ff5b9036541cbacc0d5bea7f9e77 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Jan 2023 23:43:11 -0600 Subject: [PATCH 14/19] Get Products --- lib/noko_cli/run.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/noko_cli/run.rb b/lib/noko_cli/run.rb index 01a0783..38e8007 100644 --- a/lib/noko_cli/run.rb +++ b/lib/noko_cli/run.rb @@ -4,6 +4,7 @@ module NokoCli class Run # :nodoc: def initialize @config = Config.new + @projects = Project.new(@config.conn).list @tags = Tag.new(@config.conn).list end From ba2ad6a6a00f6e028f69e9c36a38007e51f7cbfc Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sun, 22 Jan 2023 00:06:00 -0600 Subject: [PATCH 15/19] Update console script --- bin/console | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/console b/bin/console index 7305727..1aeadc8 100755 --- a/bin/console +++ b/bin/console @@ -13,8 +13,10 @@ require "noko_cli" # Examples -# Entries -# NokoCli::Entries.new.list +# config = NokoCli::Config.new +# projects = NokoCli::Project.new(config.conn) +# tags = NokoCli::Tag.new(config.conn) +# entries = NokoCli::Entry.new(config.conn) require "irb" IRB.start(__FILE__) From 2dacfc5fda791c1b1fde21c9f4103336950ae986 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sun, 22 Jan 2023 11:42:38 -0600 Subject: [PATCH 16/19] Start on create entry --- Gemfile.lock | 10 ++++++++++ exe/noko_cli | 2 +- lib/noko_cli/project.rb | 2 +- lib/noko_cli/run.rb | 20 +++++++++++++++++--- lib/noko_cli/tag.rb | 2 +- noko_cli.gemspec | 1 + spec/fixtures/projects/list.json | 2 +- spec/noko_cli/project_spec.rb | 4 ++-- spec/noko_cli/tag_spec.rb | 7 ++----- 9 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a9c58b3..32c34d5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -79,6 +80,14 @@ 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) @@ -86,6 +95,7 @@ GEM tty-screen (~> 0.8) unicode-display_width (2.5.0) unicode_utils (1.4.0) + wisper (2.0.1) zeitwerk (2.6.17) PLATFORMS diff --git a/exe/noko_cli b/exe/noko_cli index a8105a7..a6a1643 100755 --- a/exe/noko_cli +++ b/exe/noko_cli @@ -3,4 +3,4 @@ require "noko_cli" -NokoCli::Run.call +NokoCli::Run.call(ARGV) diff --git a/lib/noko_cli/project.rb b/lib/noko_cli/project.rb index 8bcb7ac..6062c4b 100644 --- a/lib/noko_cli/project.rb +++ b/lib/noko_cli/project.rb @@ -7,7 +7,7 @@ def initialize(conn) end def list - @conn.get("projects").body + @conn.get("projects").body.map { |p| p["name"] } end end end diff --git a/lib/noko_cli/run.rb b/lib/noko_cli/run.rb index 38e8007..64002f6 100644 --- a/lib/noko_cli/run.rb +++ b/lib/noko_cli/run.rb @@ -2,18 +2,32 @@ module NokoCli class Run # :nodoc: - def initialize + def initialize(options) + @options = options @config = Config.new @projects = Project.new(@config.conn).list @tags = Tag.new(@config.conn).list end - def self.call - new.call + def self.call(argv) + new(argv).call end def call + return create_entry if @options[0] == "add" + Entry.new(@config.conn).list end + + def create_entry + require "tty-prompt" + prompt = TTY::Prompt.new + result = prompt.collect do + key(:time).ask("Time in seconds?", convert: :int) + key(:project).select("Select a project", @projects, required: true) + key(:tags).multi_select("Select some tags?", @tags) + end + puts result + end end end diff --git a/lib/noko_cli/tag.rb b/lib/noko_cli/tag.rb index 3c487d8..95db48c 100644 --- a/lib/noko_cli/tag.rb +++ b/lib/noko_cli/tag.rb @@ -7,7 +7,7 @@ def initialize(conn) end def list - @conn.get("tags").body + @conn.get("tags").body.map { |tag| tag["name"] } end end end diff --git a/noko_cli.gemspec b/noko_cli.gemspec index f0eaeb5..29bff02 100644 --- a/noko_cli.gemspec +++ b/noko_cli.gemspec @@ -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" diff --git a/spec/fixtures/projects/list.json b/spec/fixtures/projects/list.json index 540af14..8066d8f 100644 --- a/spec/fixtures/projects/list.json +++ b/spec/fixtures/projects/list.json @@ -40,7 +40,7 @@ }, { "id": 2, - "name": "Learning Noko", + "name": "Learning", "description": null, "billing_increment": 15, "enabled": true, diff --git a/spec/noko_cli/project_spec.rb b/spec/noko_cli/project_spec.rb index 1b1d039..86cc50c 100644 --- a/spec/noko_cli/project_spec.rb +++ b/spec/noko_cli/project_spec.rb @@ -12,8 +12,8 @@ expect(project.list).to be_an_instance_of(Array) end - it "returns tag's attributes" do - expect(project.list.first.keys.count).to eq 28 + it "returns tag's name" do + expect(project.list).to match_array %w[Internal Learning] end end end diff --git a/spec/noko_cli/tag_spec.rb b/spec/noko_cli/tag_spec.rb index 83b2fa8..c191102 100644 --- a/spec/noko_cli/tag_spec.rb +++ b/spec/noko_cli/tag_spec.rb @@ -5,9 +5,6 @@ let(:stubs) do stub_request("tags", response: stub_response(fixture: "tags/list")) end - let(:tag_attributes) do - %w[id name billable formatted_name import entries entries_url url created_at updated_at merge_url] - end let(:config) { NokoCli::Config.new(adapter: :test, stubs:) } let(:tag) { described_class.new(config.conn) } @@ -15,8 +12,8 @@ expect(tag.list).to be_an_instance_of(Array) end - it "returns tag's attributes" do - expect(tag.list.first.keys).to match_array tag_attributes + it "returns tag's name" do + expect(tag.list).to match_array %w[sales unbillable] end end end From f6ee18028f3426fdaee4448317385dc86d847387 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sun, 22 Jan 2023 18:02:04 -0600 Subject: [PATCH 17/19] Add scope class --- bin/console | 12 ++++++--- exe/noko_cli | 2 +- lib/noko_cli/api/entry.rb | 35 +++++++++++++++++++++++++ lib/noko_cli/api/project.rb | 15 +++++++++++ lib/noko_cli/api/tag.rb | 15 +++++++++++ lib/noko_cli/entry.rb | 34 ------------------------ lib/noko_cli/project.rb | 13 --------- lib/noko_cli/run.rb | 23 ++++++++-------- lib/noko_cli/tag.rb | 13 --------- spec/noko_cli/{ => api}/entry_spec.rb | 4 +-- spec/noko_cli/{ => api}/project_spec.rb | 4 +-- spec/noko_cli/{ => api}/tag_spec.rb | 4 +-- 12 files changed, 92 insertions(+), 82 deletions(-) create mode 100644 lib/noko_cli/api/entry.rb create mode 100644 lib/noko_cli/api/project.rb create mode 100644 lib/noko_cli/api/tag.rb delete mode 100644 lib/noko_cli/entry.rb delete mode 100644 lib/noko_cli/project.rb delete mode 100644 lib/noko_cli/tag.rb rename spec/noko_cli/{ => api}/entry_spec.rb (90%) rename spec/noko_cli/{ => api}/project_spec.rb (82%) rename spec/noko_cli/{ => api}/tag_spec.rb (83%) diff --git a/bin/console b/bin/console index 1aeadc8..c866915 100755 --- a/bin/console +++ b/bin/console @@ -13,10 +13,14 @@ require "noko_cli" # Examples -# config = NokoCli::Config.new -# projects = NokoCli::Project.new(config.conn) -# tags = NokoCli::Tag.new(config.conn) -# entries = NokoCli::Entry.new(config.conn) +# App +# config = NokoCli::Config.new +# run = NokoCli::Run.new(["add"]).call + +# Api +# projects = NokoCli::Api::Project.new +# tags = NokoCli::Api::Tag.new +# entries = NokoCli::Api::Entry.new require "irb" IRB.start(__FILE__) diff --git a/exe/noko_cli b/exe/noko_cli index a6a1643..baf7104 100755 --- a/exe/noko_cli +++ b/exe/noko_cli @@ -3,4 +3,4 @@ require "noko_cli" -NokoCli::Run.call(ARGV) +NokoCli::Run.new(ARGV).call diff --git a/lib/noko_cli/api/entry.rb b/lib/noko_cli/api/entry.rb new file mode 100644 index 0000000..cb30617 --- /dev/null +++ b/lib/noko_cli/api/entry.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require "tty-table" + +module NokoCli + module Api + 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 +end diff --git a/lib/noko_cli/api/project.rb b/lib/noko_cli/api/project.rb new file mode 100644 index 0000000..33e8f33 --- /dev/null +++ b/lib/noko_cli/api/project.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module NokoCli + module Api + 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 +end diff --git a/lib/noko_cli/api/tag.rb b/lib/noko_cli/api/tag.rb new file mode 100644 index 0000000..a383a2a --- /dev/null +++ b/lib/noko_cli/api/tag.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module NokoCli + module Api + class Tag # :nodoc: + def initialize(config = Config.new) + @conn = config.conn + end + + def list + @conn.get("tags").body.map { |tag| tag["name"] } + end + end + end +end diff --git a/lib/noko_cli/entry.rb b/lib/noko_cli/entry.rb deleted file mode 100644 index 75cb74c..0000000 --- a/lib/noko_cli/entry.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -require "tty-table" - -module NokoCli - # This is an entry resource, which could be listed - class Entry - def initialize(conn) - @conn = 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 diff --git a/lib/noko_cli/project.rb b/lib/noko_cli/project.rb deleted file mode 100644 index 6062c4b..0000000 --- a/lib/noko_cli/project.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -module NokoCli - class Project # :nodoc: - def initialize(conn) - @conn = conn - end - - def list - @conn.get("projects").body.map { |p| p["name"] } - end - end -end diff --git a/lib/noko_cli/run.rb b/lib/noko_cli/run.rb index 64002f6..dd2a5d4 100644 --- a/lib/noko_cli/run.rb +++ b/lib/noko_cli/run.rb @@ -4,30 +4,31 @@ module NokoCli class Run # :nodoc: def initialize(options) @options = options - @config = Config.new - @projects = Project.new(@config.conn).list - @tags = Tag.new(@config.conn).list - end - - def self.call(argv) - new(argv).call end def call - return create_entry if @options[0] == "add" + return create_entry if add? - Entry.new(@config.conn).list + Api::Entry.new.list end + private + def create_entry require "tty-prompt" prompt = TTY::Prompt.new result = prompt.collect do + projects = Api::Project.new.list + tags = Api::Tag.new.list key(:time).ask("Time in seconds?", convert: :int) - key(:project).select("Select a project", @projects, required: true) - key(:tags).multi_select("Select some tags?", @tags) + key(:project).select("Select a project", projects, required: true) + key(:tags).multi_select("Select some tags?", tags) end puts result end + + def add? + @options[0] == "add" + end end end diff --git a/lib/noko_cli/tag.rb b/lib/noko_cli/tag.rb deleted file mode 100644 index 95db48c..0000000 --- a/lib/noko_cli/tag.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -module NokoCli - class Tag # :nodoc: - def initialize(conn) - @conn = conn - end - - def list - @conn.get("tags").body.map { |tag| tag["name"] } - end - end -end diff --git a/spec/noko_cli/entry_spec.rb b/spec/noko_cli/api/entry_spec.rb similarity index 90% rename from spec/noko_cli/entry_spec.rb rename to spec/noko_cli/api/entry_spec.rb index 9db191f..b789136 100644 --- a/spec/noko_cli/entry_spec.rb +++ b/spec/noko_cli/api/entry_spec.rb @@ -3,13 +3,13 @@ require "faraday" require "json" -RSpec.describe NokoCli::Entry do +RSpec.describe NokoCli::Api::Entry do describe "#list" do let(:stubs) do stub_request("current_user/entries", response: stub_response(fixture: "entries/list")) end let(:config) { NokoCli::Config.new(adapter: :test, stubs:) } - let(:entry) { described_class.new(config.conn) } + let(:entry) { described_class.new(config) } it "includes headers" do headers = /date |minutes |project |description/ diff --git a/spec/noko_cli/project_spec.rb b/spec/noko_cli/api/project_spec.rb similarity index 82% rename from spec/noko_cli/project_spec.rb rename to spec/noko_cli/api/project_spec.rb index 86cc50c..73d4f54 100644 --- a/spec/noko_cli/project_spec.rb +++ b/spec/noko_cli/api/project_spec.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true -RSpec.describe NokoCli::Project do +RSpec.describe NokoCli::Api::Project do describe "#list" do let(:stubs) do stub_request("projects", response: stub_response(fixture: "projects/list")) end let(:config) { NokoCli::Config.new(adapter: :test, stubs:) } - let(:project) { described_class.new(config.conn) } + let(:project) { described_class.new(config) } it "returns an array" do expect(project.list).to be_an_instance_of(Array) diff --git a/spec/noko_cli/tag_spec.rb b/spec/noko_cli/api/tag_spec.rb similarity index 83% rename from spec/noko_cli/tag_spec.rb rename to spec/noko_cli/api/tag_spec.rb index c191102..0bcbd5d 100644 --- a/spec/noko_cli/tag_spec.rb +++ b/spec/noko_cli/api/tag_spec.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true -RSpec.describe NokoCli::Tag do +RSpec.describe NokoCli::Api::Tag do describe "#list" do let(:stubs) do stub_request("tags", response: stub_response(fixture: "tags/list")) end let(:config) { NokoCli::Config.new(adapter: :test, stubs:) } - let(:tag) { described_class.new(config.conn) } + let(:tag) { described_class.new(config) } it "returns an array" do expect(tag.list).to be_an_instance_of(Array) From db7194f225fd99b3888a4231a933b13fc919aa48 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 11 Feb 2023 22:41:48 -0600 Subject: [PATCH 18/19] Able to create entries with the right attrs --- bin/console | 13 +++++---- exe/noko_cli | 2 +- lib/noko_cli/api/entry.rb | 35 ------------------------- lib/noko_cli/api/project.rb | 15 ----------- lib/noko_cli/api/tag.rb | 15 ----------- lib/noko_cli/entry.rb | 33 +++++++++++++++++++++++ lib/noko_cli/form.rb | 26 ++++++++++++++++++ lib/noko_cli/project.rb | 13 +++++++++ lib/noko_cli/run.rb | 19 ++++++-------- lib/noko_cli/tag.rb | 13 +++++++++ spec/noko_cli/{api => }/entry_spec.rb | 2 +- spec/noko_cli/{api => }/project_spec.rb | 2 +- spec/noko_cli/{api => }/tag_spec.rb | 4 +-- 13 files changed, 106 insertions(+), 86 deletions(-) delete mode 100644 lib/noko_cli/api/entry.rb delete mode 100644 lib/noko_cli/api/project.rb delete mode 100644 lib/noko_cli/api/tag.rb create mode 100644 lib/noko_cli/entry.rb create mode 100644 lib/noko_cli/form.rb create mode 100644 lib/noko_cli/project.rb create mode 100644 lib/noko_cli/tag.rb rename spec/noko_cli/{api => }/entry_spec.rb (95%) rename spec/noko_cli/{api => }/project_spec.rb (92%) rename spec/noko_cli/{api => }/tag_spec.rb (80%) diff --git a/bin/console b/bin/console index c866915..f758b75 100755 --- a/bin/console +++ b/bin/console @@ -15,12 +15,15 @@ require "noko_cli" # App # config = NokoCli::Config.new -# run = NokoCli::Run.new(["add"]).call +# run = NokoCli::Run.call(["add"]) -# Api -# projects = NokoCli::Api::Project.new -# tags = NokoCli::Api::Tag.new -# entries = NokoCli::Api::Entry.new +# 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__) diff --git a/exe/noko_cli b/exe/noko_cli index baf7104..a6a1643 100755 --- a/exe/noko_cli +++ b/exe/noko_cli @@ -3,4 +3,4 @@ require "noko_cli" -NokoCli::Run.new(ARGV).call +NokoCli::Run.call(ARGV) diff --git a/lib/noko_cli/api/entry.rb b/lib/noko_cli/api/entry.rb deleted file mode 100644 index cb30617..0000000 --- a/lib/noko_cli/api/entry.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require "tty-table" - -module NokoCli - module Api - 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 -end diff --git a/lib/noko_cli/api/project.rb b/lib/noko_cli/api/project.rb deleted file mode 100644 index 33e8f33..0000000 --- a/lib/noko_cli/api/project.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -module NokoCli - module Api - 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 -end diff --git a/lib/noko_cli/api/tag.rb b/lib/noko_cli/api/tag.rb deleted file mode 100644 index a383a2a..0000000 --- a/lib/noko_cli/api/tag.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -module NokoCli - module Api - class Tag # :nodoc: - def initialize(config = Config.new) - @conn = config.conn - end - - def list - @conn.get("tags").body.map { |tag| tag["name"] } - end - end - end -end diff --git a/lib/noko_cli/entry.rb b/lib/noko_cli/entry.rb new file mode 100644 index 0000000..da21a6f --- /dev/null +++ b/lib/noko_cli/entry.rb @@ -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 diff --git a/lib/noko_cli/form.rb b/lib/noko_cli/form.rb new file mode 100644 index 0000000..b3c6920 --- /dev/null +++ b/lib/noko_cli/form.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "tty-prompt" + +module NokoCli + class Form # :nodoc: + # rubocop:disable Metrics/MethodLength + # rubocop:disable Metrics/AbcSize + def call + prompt = TTY::Prompt.new + date = Time.new.strftime("%Y-%m-%d") + projects = Project.new.list + tags = Tag.new.list + + prompt.collect do + key(:date).ask("*Date (yyyy-mm-dd)", default: date, required: true) + key(:minutes).ask("*Time (in minutes)", convert: :int, required: true) + key(:project_name).select("*Select a project", projects, filter: true, required: true) + key(:description).ask("*Description", required: true) + key(:tags).multi_select("Select some tags?", tags, filter: true, cycle: true) + end + end + # rubocop:enable Metrics/MethodLength + # rubocop:enable Metrics/AbcSize + end +end diff --git a/lib/noko_cli/project.rb b/lib/noko_cli/project.rb new file mode 100644 index 0000000..7847781 --- /dev/null +++ b/lib/noko_cli/project.rb @@ -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 diff --git a/lib/noko_cli/run.rb b/lib/noko_cli/run.rb index dd2a5d4..4ac9f4c 100644 --- a/lib/noko_cli/run.rb +++ b/lib/noko_cli/run.rb @@ -2,6 +2,10 @@ module NokoCli class Run # :nodoc: + def self.call(options) + new(options).call + end + def initialize(options) @options = options end @@ -9,22 +13,15 @@ def initialize(options) def call return create_entry if add? - Api::Entry.new.list + Entry.new.list end private def create_entry - require "tty-prompt" - prompt = TTY::Prompt.new - result = prompt.collect do - projects = Api::Project.new.list - tags = Api::Tag.new.list - key(:time).ask("Time in seconds?", convert: :int) - key(:project).select("Select a project", projects, required: true) - key(:tags).multi_select("Select some tags?", tags) - end - puts result + require "json" + result = Form.new.call + puts JSON.pretty_generate(result) end def add? diff --git a/lib/noko_cli/tag.rb b/lib/noko_cli/tag.rb new file mode 100644 index 0000000..55cc905 --- /dev/null +++ b/lib/noko_cli/tag.rb @@ -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 diff --git a/spec/noko_cli/api/entry_spec.rb b/spec/noko_cli/entry_spec.rb similarity index 95% rename from spec/noko_cli/api/entry_spec.rb rename to spec/noko_cli/entry_spec.rb index b789136..96fa1e6 100644 --- a/spec/noko_cli/api/entry_spec.rb +++ b/spec/noko_cli/entry_spec.rb @@ -3,7 +3,7 @@ require "faraday" require "json" -RSpec.describe NokoCli::Api::Entry do +RSpec.describe NokoCli::Entry do describe "#list" do let(:stubs) do stub_request("current_user/entries", response: stub_response(fixture: "entries/list")) diff --git a/spec/noko_cli/api/project_spec.rb b/spec/noko_cli/project_spec.rb similarity index 92% rename from spec/noko_cli/api/project_spec.rb rename to spec/noko_cli/project_spec.rb index 73d4f54..01695cf 100644 --- a/spec/noko_cli/api/project_spec.rb +++ b/spec/noko_cli/project_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe NokoCli::Api::Project do +RSpec.describe NokoCli::Project do describe "#list" do let(:stubs) do stub_request("projects", response: stub_response(fixture: "projects/list")) diff --git a/spec/noko_cli/api/tag_spec.rb b/spec/noko_cli/tag_spec.rb similarity index 80% rename from spec/noko_cli/api/tag_spec.rb rename to spec/noko_cli/tag_spec.rb index 0bcbd5d..668ec16 100644 --- a/spec/noko_cli/api/tag_spec.rb +++ b/spec/noko_cli/tag_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe NokoCli::Api::Tag do +RSpec.describe NokoCli::Tag do describe "#list" do let(:stubs) do stub_request("tags", response: stub_response(fixture: "tags/list")) @@ -13,7 +13,7 @@ end it "returns tag's name" do - expect(tag.list).to match_array %w[sales unbillable] + expect(tag.list).to match_array %w[#sales* #unbillable*] end end end From acf6a44ec179f813065b86a91c52c79450662287 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sun, 26 Feb 2023 15:03:07 -0600 Subject: [PATCH 19/19] Concat description --- lib/noko_cli/form.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/noko_cli/form.rb b/lib/noko_cli/form.rb index b3c6920..c5dd58e 100644 --- a/lib/noko_cli/form.rb +++ b/lib/noko_cli/form.rb @@ -4,23 +4,29 @@ module NokoCli class Form # :nodoc: - # rubocop:disable Metrics/MethodLength - # rubocop:disable Metrics/AbcSize 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") - projects = Project.new.list - tags = Tag.new.list prompt.collect do - key(:date).ask("*Date (yyyy-mm-dd)", default: date, required: true) + 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", projects, filter: true, 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?", tags, filter: true, cycle: true) + key(:tags).multi_select("Select some tags?", Tag.new.list, filter: true, cycle: true) end end - # rubocop:enable Metrics/MethodLength # rubocop:enable Metrics/AbcSize + + def concat_description(info) + info[:description] = [info[:description]].concat(info[:tags]).join(" ") + info + end end end