kodeco 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 917b9f6e09b0d724f94ff27f1596cba5c870545dc6a1199a0ebb31cd7a68d114
4
- data.tar.gz: c9e3e16f779d27b3353e42593cb37abf181f9e3d1459b60dc79cef5d1b20d48d
3
+ metadata.gz: e2c3bf5ae4351b9f93929c33d3373a6b0c197a4a555f69d63191a327d5773e57
4
+ data.tar.gz: 1c07c086b622a92abc1c8b55013a64e3f2d6f8c725f59f396487b35db09986cd
5
5
  SHA512:
6
- metadata.gz: a5812db82c6f20d0bb3346fd4dd501149a58f382235038b6b274b78e7bae1329cdad77791469d0f83743ac6ad55022037c06eb363ec7a4c8f0bb3b8bf01d3ed3
7
- data.tar.gz: 105c668ab4fc503d874401e048f6cec51e5732e7df7673f91e9cf2c6e43992486fa065310b1e842213ab45b642f8a311a60cc9f7f54be25d9c8d07a27c1dfc7d
6
+ metadata.gz: 1297bbbb47629bdf96ee0bcd6a8e658f5c2f1ac07e2ca4cda8842347ef348011cf34e4aabf22cb73c43cf9c1777debe211542324787e16918123b80dce99ad9e
7
+ data.tar.gz: 5678009ccbe2d9a3641376b20c59a9085b7140b086dcd265160727a5df605dc9a415401f32212c3c42b0f52992abfdfd11dc4f02daaa7feaa5d40cb8228ecb6d
data/README.md CHANGED
@@ -24,8 +24,8 @@ kodeco problems [--kind overdue|stuck_in_preauth|invoice_overdue|expiry_missed]
24
24
  kodeco flags --app guardpost|kerching|carolus
25
25
  kodeco shortlinks list [query] [--page N]
26
26
  kodeco shortlinks show <slug>
27
- kodeco shortlinks create <url> [--channels a,b] [--campaign NAME]
28
- kodeco shortlinks update <slug> --active=true|false
27
+ kodeco shortlinks create <url> [--channels a,b] [--campaign NAME] [--content VALUE] [--source NAME --medium NAME]
28
+ kodeco shortlinks update <slug> [--active=true|false] [--source NAME] [--medium NAME] [--content VALUE]
29
29
  ```
30
30
 
31
31
  `user` shows guardpost's customer summary, with kerching's and carolus's
@@ -42,7 +42,17 @@ print the `kodeco login --scopes` line to run, and send nothing. `show` and
42
42
  `update` take a slug or a short URL. Team members see and change only their
43
43
  own links. Admins see every link and the fuller click counts, and can use
44
44
  `--channels` (keys from carolus's `config/short_link_channels.yml`, such as
45
- `twitter`) and `--campaign`, which needs `--channels`.
45
+ `twitter`), `--campaign`, `--content` and `--source` with `--medium`.
46
+ Each channel gets a link, and so does `--source`, for a source that isn't a
47
+ channel, such as a newsletter we sponsor (`--source iosdevweekly --medium
48
+ email`). `--campaign` and `--content` need `--channels` or `--source`.
49
+ `--content` tells apart placements with the same destination, channel and
50
+ campaign; creating one again with the same content returns the existing
51
+ link. Without `--channels` or `--source` you get one link with no UTMs.
52
+ Admins can also retag a link with `update --source`, `--medium` and
53
+ `--content` (`--content ""` removes it). Its short URL stays the same, and
54
+ people get the new UTMs from their next click. carolus stores sources,
55
+ mediums and content in lower case.
46
56
 
47
57
  `login` uses guardpost's device-code flow: it prints a code and a link to
48
58
  approve it, and opens the link in a browser when run in a terminal. The
@@ -52,8 +62,9 @@ admin. A login gets whichever of the requested scopes you hold, and `login`
52
62
  lists any that weren't granted; a grant added later needs a fresh
53
63
  `kodeco login`.
54
64
 
55
- `logout` deletes the local credentials. guardpost has no revoke endpoint, so
56
- the server session lasts up to 7 days unless an admin revokes it.
65
+ `logout` signs the session out on guardpost, then deletes the local
66
+ credentials. If guardpost can't be reached, it still deletes them and says
67
+ so; the session then ends within 7 days, or an admin can sign it out.
57
68
 
58
69
  Every command takes:
59
70
 
data/lib/periscope/cli.rb CHANGED
@@ -10,13 +10,14 @@ module Periscope
10
10
  include Commands::Kerching
11
11
  include Commands::Flags
12
12
  include Commands::Shortlinks
13
+ include Commands::Logout
13
14
 
14
15
  USAGE = <<~TEXT
15
16
  Usage: kodeco <command> [options]
16
17
 
17
18
  Commands:
18
19
  login [--scopes "a b"] [--no-browser] Log in with guardpost's device-code flow
19
- logout Delete the local credentials
20
+ logout Sign out on guardpost and delete the local credentials
20
21
  whoami [--app guardpost|kerching|carolus]
21
22
  Show who an app takes you to be
22
23
  user <email|rwuuid> Show guardpost's customer summary
@@ -27,18 +28,18 @@ module Periscope
27
28
  flags --app guardpost|kerching|carolus Show an app's feature flags
28
29
  shortlinks list [query] [--page N] List kod.eco short links
29
30
  shortlinks show <slug> Show one short link and its clicks
30
- shortlinks create <url> [--channels a,b] [--campaign NAME]
31
+ shortlinks create <url> [--channels a,b] [--campaign NAME] [--content VALUE]
32
+ [--source NAME --medium NAME]
31
33
  Make short links to a kodeco.com URL
32
- shortlinks update <slug> --active=true|false
33
- Turn a short link on or off
34
+ shortlinks update <slug> [--active=true|false] [--source NAME]
35
+ [--medium NAME] [--content VALUE]
36
+ Turn a short link on or off, or retag it
34
37
 
35
38
  Every command takes --json (one JSON document on stdout, errors as JSON
36
39
  on stderr) and --debug (logs each request's method, URL and status).
37
40
  TEXT
38
41
 
39
42
  COMMANDS = %w[login logout whoami user orders order subscription problems flags shortlinks].freeze
40
- LOGOUT_NOTE = "guardpost has no revoke endpoint, so the server session lasts up to 7 days " \
41
- "unless an admin revokes it."
42
43
 
43
44
  def initialize(argv, env: ENV, stdout: $stdout, stderr: $stderr, login_options: {})
44
45
  @argv = argv.dup
@@ -96,14 +97,6 @@ module Periscope
96
97
  "then run kodeco login again."
97
98
  end
98
99
 
99
- def cmd_logout
100
- parse_options
101
- deleted = store.with_lock { store.delete }
102
- emit({ "logged_out" => true, "had_credentials" => !deleted.nil?, "note" => LOGOUT_NOTE }) do
103
- deleted ? "Logged out locally. #{LOGOUT_NOTE}" : "Not logged in; there was nothing to delete."
104
- end
105
- end
106
-
107
100
  def cmd_whoami
108
101
  opts = parse_options { |parser, o| parser.on("--app APP", Config::APPS) { |v| o[:app] = v } }
109
102
  data = client.get(opts[:app] || "guardpost", "/api/v2/whoami")["data"] || {}
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Periscope
4
+ module Commands
5
+ # kodeco logout
6
+ module Logout
7
+ REVOKE_PATH = "/api/v2/auth/revoke"
8
+
9
+ private
10
+
11
+ def cmd_logout
12
+ parse_options
13
+ revoked = false
14
+ reason = nil
15
+ deleted = store.with_lock do
16
+ refresh_token = store.read&.dig("refresh_token")
17
+ revoked, reason = revoke(refresh_token) if refresh_token
18
+ store.delete
19
+ end
20
+ emit({ "logged_out" => true, "had_credentials" => !deleted.nil?, "revoked" => revoked }) do
21
+ logout_text(deleted, revoked, reason)
22
+ end
23
+ end
24
+
25
+ # Returns [revoked, reason]. It doesn't raise for guardpost's answer or a
26
+ # network error, so the local credentials are deleted whatever happens.
27
+ def revoke(refresh_token)
28
+ res = http.post_form("#{config.url('guardpost')}#{REVOKE_PATH}",
29
+ token: refresh_token, token_type_hint: "refresh_token")
30
+ return [true, nil] if res.status == 200
31
+
32
+ [false, "guardpost answered #{res.status}#{": #{res.error_message}" if res.error_message}"]
33
+ rescue Error => e
34
+ [false, e.message]
35
+ end
36
+
37
+ def logout_text(deleted, revoked, reason)
38
+ return "Not logged in; there was nothing to delete." unless deleted
39
+ return "Logged out, and signed out on guardpost." if revoked
40
+ return "Logged out locally. There was no refresh token to sign out on guardpost with." unless reason
41
+
42
+ "Logged out locally, but couldn't reach guardpost to end the session (#{reason}).\n" \
43
+ "The session ends within 7 days, or an admin can sign it out."
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Periscope
4
+ module Commands
5
+ # WEB-8388. The --source, --medium and --content options of kodeco
6
+ # shortlinks create and update. carolus stores their values in lower case.
7
+ module ShortlinkTags
8
+ NEEDS_TAGGED_LINK = {
9
+ campaign: "--campaign needs --channels or --source: carolus uses it only for tagged links",
10
+ content: "--content needs --channels or --source"
11
+ }.freeze
12
+
13
+ private
14
+
15
+ def tag_options(parser, opts)
16
+ parser.on("--source NAME") { |v| opts[:source] = v }
17
+ parser.on("--medium NAME") { |v| opts[:medium] = v }
18
+ parser.on("--content VALUE") { |v| opts[:content] = v }
19
+ end
20
+
21
+ def tag_payload(opts)
22
+ { utm_source: opts[:source], utm_medium: opts[:medium], utm_content: opts[:content] }
23
+ end
24
+
25
+ # carolus would refuse these too; checking first sends nothing.
26
+ def check_create_options(opts)
27
+ tagged = opts[:channels] || opts[:source]
28
+ NEEDS_TAGGED_LINK.each { |key, message| raise UsageError, message if opts[key] && !tagged }
29
+ return if opts[:source].nil? == opts[:medium].nil?
30
+
31
+ raise UsageError, opts[:source] ? "--source needs --medium, such as email" : "--medium needs --source"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -4,13 +4,15 @@ module Periscope
4
4
  module Commands
5
5
  # kodeco shortlinks list, show, create and update: carolus's kod.eco short links.
6
6
  module Shortlinks
7
+ include ShortlinkTags
8
+
7
9
  SUBCOMMANDS = %w[list show create update].freeze
8
10
  PATH = "/api/v2/short_links"
9
11
  SLUG = /\A[a-z2-9]{5}\z/
10
- UTM_HEADERS = %w[SOURCE MEDIUM CAMPAIGN].freeze
12
+ UTM_HEADERS = %w[SOURCE MEDIUM CAMPAIGN CONTENT].freeze
11
13
  LIST_HEADERS = ["SHORT URL", "ACTIVE", "CLICKS", "7D", "BOTS", *UTM_HEADERS, "DESTINATION"].freeze
12
- TEAM_NOTE = "Short links are for the Kodeco team, and only admins can see or change other people's links " \
13
- "or use --channels and --campaign."
14
+ TEAM_NOTE = "Short links are for the Kodeco team, and only admins can see or change other people's links, " \
15
+ "use --channels, --campaign, --content, --source and --medium, or change a link's tags."
14
16
  RETRY_NOTE = "It may have been applied; running the same command again is safe."
15
17
 
16
18
  private
@@ -43,24 +45,29 @@ module Periscope
43
45
  opts = parse_options(positional: 1) do |parser, o|
44
46
  parser.on("--channels LIST", Array) { |v| o[:channels] = channels(v) }
45
47
  parser.on("--campaign NAME") { |v| o[:campaign] = v }
48
+ tag_options(parser, o)
46
49
  end
47
- raise UsageError, "--campaign needs --channels: carolus uses it only for channel links" if
48
- opts[:campaign] && !opts[:channels]
50
+ check_create_options(opts)
49
51
 
50
- payload = { destination_url: opts[:args].first, channels: opts[:channels], utm_campaign: opts[:campaign] }
52
+ payload = { destination_url: opts[:args].first, channels: opts[:channels], utm_campaign: opts[:campaign],
53
+ **tag_payload(opts) }
51
54
  links = carolus("shortlinks:write", write: true) { client.post_json("carolus", PATH, payload.compact) }["data"]
52
55
  emit(links) { create_text(links) }
53
56
  end
54
57
 
55
58
  def shortlinks_update
56
- opts = parse_options(positional: 1) { |parser, o| parser.on("--active BOOL", TrueClass) { |v| o[:active] = v } }
59
+ opts = parse_options(positional: 1) do |parser, o|
60
+ parser.on("--active BOOL", TrueClass) { |v| o[:active] = v }
61
+ tag_options(parser, o)
62
+ end
57
63
  slug = slug_arg(opts)
58
- raise UsageError, "update needs --active=true or --active=false" if opts[:active].nil?
64
+ payload = { active: opts[:active], **tag_payload(opts) }.compact
65
+ raise UsageError, "update needs --active=true|false, --source, --medium or --content" if payload.empty?
59
66
 
60
67
  link = carolus("shortlinks:write", slug: slug, write: true) do
61
- client.patch_json("carolus", "#{PATH}/#{slug}", { active: opts[:active] })
68
+ client.patch_json("carolus", "#{PATH}/#{slug}", payload)
62
69
  end["data"]
63
- emit(link) { ShortLinkText.new(link).updated }
70
+ emit(link) { ShortLinkText.new(link).updated(active_changed: payload.key?(:active)) }
64
71
  end
65
72
 
66
73
  def create_text(links)
@@ -6,9 +6,9 @@ module Periscope
6
6
  class ShortLinkText
7
7
  INACTIVE = "is now inactive. Visitors go to the kodeco.com home page."
8
8
 
9
- # The UTM source, medium and campaign, with empty values as nil so tables print "-".
9
+ # The UTM source, medium, campaign and content, with empty values as nil so tables print "-".
10
10
  def self.utm(link)
11
- %w[utm_source utm_medium utm_campaign].map { |key| link[key].to_s.empty? ? nil : link[key] }
11
+ %w[utm_source utm_medium utm_campaign utm_content].map { |key| link[key].to_s.empty? ? nil : link[key] }
12
12
  end
13
13
 
14
14
  def initialize(link)
@@ -20,8 +20,13 @@ module Periscope
20
20
  end
21
21
 
22
22
  # After a PATCH, which sends neither daily clicks nor bots.
23
- def updated
24
- ["#{@link['short_url']} #{@link['active'] ? 'is now active.' : INACTIVE}", *fields].join("\n")
23
+ def updated(active_changed: true)
24
+ status = if active_changed
25
+ @link["active"] ? "is now active." : INACTIVE
26
+ else
27
+ "is updated. From its next click it sends the UTMs below."
28
+ end
29
+ ["#{@link['short_url']} #{status}", *fields].join("\n")
25
30
  end
26
31
 
27
32
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Periscope
4
- VERSION = "0.2.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/periscope.rb CHANGED
@@ -15,5 +15,7 @@ require_relative "periscope/short_link_text"
15
15
  require_relative "periscope/commands/user"
16
16
  require_relative "periscope/commands/kerching"
17
17
  require_relative "periscope/commands/flags"
18
+ require_relative "periscope/commands/shortlink_tags"
18
19
  require_relative "periscope/commands/shortlinks"
20
+ require_relative "periscope/commands/logout"
19
21
  require_relative "periscope/cli"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kodeco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kodeco
@@ -22,6 +22,8 @@ files:
22
22
  - lib/periscope/client.rb
23
23
  - lib/periscope/commands/flags.rb
24
24
  - lib/periscope/commands/kerching.rb
25
+ - lib/periscope/commands/logout.rb
26
+ - lib/periscope/commands/shortlink_tags.rb
25
27
  - lib/periscope/commands/shortlinks.rb
26
28
  - lib/periscope/commands/user.rb
27
29
  - lib/periscope/config.rb