buzzsprout-cli 0.1.1

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.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +271 -0
  4. data/bin/buzzsprout +6 -0
  5. data/lib/buzzsprout/animation/wordmark.rb +41 -0
  6. data/lib/buzzsprout/animation.rb +181 -0
  7. data/lib/buzzsprout/cli.rb +215 -0
  8. data/lib/buzzsprout/client.rb +306 -0
  9. data/lib/buzzsprout/color.rb +195 -0
  10. data/lib/buzzsprout/commands/base.rb +188 -0
  11. data/lib/buzzsprout/commands/brand_affiliations.rb +98 -0
  12. data/lib/buzzsprout/commands/chapters.rb +58 -0
  13. data/lib/buzzsprout/commands/contributors.rb +119 -0
  14. data/lib/buzzsprout/commands/downloads.rb +139 -0
  15. data/lib/buzzsprout/commands/episodes.rb +116 -0
  16. data/lib/buzzsprout/commands/fan_mail.rb +216 -0
  17. data/lib/buzzsprout/commands/feed_verifications.rb +74 -0
  18. data/lib/buzzsprout/commands/listings.rb +159 -0
  19. data/lib/buzzsprout/commands/login.rb +264 -0
  20. data/lib/buzzsprout/commands/podcasts.rb +85 -0
  21. data/lib/buzzsprout/commands/podrolls.rb +74 -0
  22. data/lib/buzzsprout/commands/skills.rb +144 -0
  23. data/lib/buzzsprout/commands/status.rb +64 -0
  24. data/lib/buzzsprout/commands/team.rb +75 -0
  25. data/lib/buzzsprout/commands/transcripts.rb +100 -0
  26. data/lib/buzzsprout/commands/update.rb +36 -0
  27. data/lib/buzzsprout/commands/uploads.rb +134 -0
  28. data/lib/buzzsprout/config.rb +125 -0
  29. data/lib/buzzsprout/formatter.rb +68 -0
  30. data/lib/buzzsprout/group_help.rb +74 -0
  31. data/lib/buzzsprout/oauth.rb +203 -0
  32. data/lib/buzzsprout/options.rb +155 -0
  33. data/lib/buzzsprout/podcast_picker.rb +328 -0
  34. data/lib/buzzsprout/redact.rb +65 -0
  35. data/lib/buzzsprout/updater.rb +230 -0
  36. data/lib/buzzsprout/version.rb +5 -0
  37. data/skills/README.md +62 -0
  38. data/skills/brand_affiliations/SKILL.md +52 -0
  39. data/skills/buzzsprout/SKILL.md +111 -0
  40. data/skills/chapters/SKILL.md +49 -0
  41. data/skills/contributors/SKILL.md +58 -0
  42. data/skills/episodes/SKILL.md +61 -0
  43. data/skills/fan_mail/SKILL.md +53 -0
  44. data/skills/feed_verifications/SKILL.md +51 -0
  45. data/skills/insertion_points/SKILL.md +50 -0
  46. data/skills/listings/SKILL.md +52 -0
  47. data/skills/podcasts/SKILL.md +57 -0
  48. data/skills/podrolls/SKILL.md +49 -0
  49. data/skills/soundbites/SKILL.md +51 -0
  50. data/skills/stats/SKILL.md +65 -0
  51. data/skills/team/SKILL.md +10 -0
  52. data/skills/transcripts/SKILL.md +60 -0
  53. data/skills/uploads/SKILL.md +68 -0
  54. metadata +102 -0
@@ -0,0 +1,195 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buzzsprout
4
+ module Color
5
+ RESET = "\e[0m"
6
+ BOLD = "\e[1m"
7
+ DIM = "\e[2m"
8
+ RED = "\e[31m"
9
+ GREEN = "\e[32m"
10
+ YELLOW = "\e[33m"
11
+ BLUE = "\e[34m"
12
+ MAGENTA = "\e[35m"
13
+ CYAN = "\e[36m"
14
+
15
+ module_function
16
+
17
+ def enabled?(io = $stdout)
18
+ return false if ENV['NO_COLOR']
19
+ return false if ENV['BUZZSPROUT_NO_COLOR']
20
+
21
+ io.respond_to?(:tty?) && io.tty?
22
+ end
23
+
24
+ def paint(text, *codes, io: $stdout)
25
+ return text.to_s unless enabled?(io) && codes.any?
26
+
27
+ "#{codes.join}#{text}#{RESET}"
28
+ end
29
+
30
+ def bold(text, io: $stdout)
31
+ paint(text, BOLD, io: io)
32
+ end
33
+
34
+ def dim(text, io: $stdout)
35
+ paint(text, DIM, io: io)
36
+ end
37
+
38
+ def red(text, io: $stdout)
39
+ paint(text, RED, io: io)
40
+ end
41
+
42
+ def green(text, io: $stdout)
43
+ paint(text, GREEN, io: io)
44
+ end
45
+
46
+ def yellow(text, io: $stdout)
47
+ paint(text, YELLOW, io: io)
48
+ end
49
+
50
+ def cyan(text, io: $stdout)
51
+ paint(text, CYAN, io: io)
52
+ end
53
+
54
+ def magenta(text, io: $stdout)
55
+ paint(text, MAGENTA, io: io)
56
+ end
57
+
58
+ def rgb(text, hex, io: $stdout)
59
+ value = hex.to_s.delete('#')
60
+ r = value[0, 2].to_i(16)
61
+ g = value[2, 2].to_i(16)
62
+ b = value[4, 2].to_i(16)
63
+ paint(text, "\e[38;2;#{r};#{g};#{b}m", io: io)
64
+ end
65
+
66
+ # :dark or :light. Override with BUZZSPROUT_THEME=dark|light.
67
+ # Otherwise COLORFGBG, then a best-effort OSC 11 query (raw TTY only),
68
+ # then dark. OSC is skipped in CI and whenever it cannot be done without
69
+ # leaking the reply as visible text.
70
+ def theme(io = $stdout)
71
+ @theme_cache ||= {}
72
+ key = "#{ENV['BUZZSPROUT_THEME']}|#{ENV['COLORFGBG']}|#{io.object_id}"
73
+ @theme_cache[key] ||= detect_theme(io)
74
+ end
75
+
76
+ def reset_theme!
77
+ @theme_cache = {}
78
+ end
79
+
80
+ def detect_theme(io = $stdout)
81
+ override = ENV['BUZZSPROUT_THEME'].to_s.strip.downcase
82
+ return :dark if override == 'dark'
83
+ return :light if override == 'light'
84
+
85
+ from_colorfgbg || probe_osc11(io) || :dark
86
+ end
87
+
88
+ def from_colorfgbg
89
+ raw = ENV['COLORFGBG'].to_s
90
+ return nil if raw.empty?
91
+
92
+ bg = raw.split(';').reverse.find { |part| part.match?(/\A\d+\z/) }
93
+ return nil unless bg
94
+
95
+ n = bg.to_i
96
+ n == 7 || n >= 9 ? :light : :dark
97
+ end
98
+
99
+ # OSC 11 → rgb:RR/GG/BB. Luma > 0.5 is a light background.
100
+ def parse_osc11(response)
101
+ match = response.to_s.match(/rgb:([0-9a-fA-F]+)\/([0-9a-fA-F]+)\/([0-9a-fA-F]+)/)
102
+ return nil unless match
103
+
104
+ channels = match.captures.map do |hex|
105
+ hex.to_i(16).to_f / ((16**hex.length) - 1)
106
+ end
107
+ luma = (0.2126 * channels[0]) + (0.7152 * channels[1]) + (0.0722 * channels[2])
108
+ luma > 0.5 ? :light : :dark
109
+ end
110
+
111
+ # Query the terminal background via OSC 11. Writes/reads only on +tty+
112
+ # (never the UI stream). Requires raw/no-echo so the reply is not echoed.
113
+ # Pass +tty:+ in tests. Returns nil if the probe cannot be done safely.
114
+ def probe_osc11(ui_io = $stdout, tty: nil)
115
+ injected = !tty.nil?
116
+ return nil if ENV['CI'] && !injected
117
+
118
+ tty ||= osc_console
119
+ return nil unless osc_tty?(tty)
120
+ return nil if tty.equal?(ui_io)
121
+
122
+ reply = +''
123
+ tty.raw do
124
+ tty.write "\e]11;?\a"
125
+ tty.flush if tty.respond_to?(:flush)
126
+ reply = read_osc_reply(tty, timeout: 0.05)
127
+ drain_tty(tty)
128
+ end
129
+ parse_osc11(reply)
130
+ rescue Errno::ENXIO, Errno::ENOTTY, Errno::ENOENT, Errno::EAGAIN, Errno::EWOULDBLOCK, EOFError, IOError, ArgumentError
131
+ nil
132
+ end
133
+
134
+ def osc_console
135
+ require 'io/console'
136
+ console = IO.console
137
+ return nil unless console
138
+ return nil unless console.respond_to?(:raw)
139
+ return nil unless $stdin.respond_to?(:tty?) && $stdin.tty?
140
+
141
+ console
142
+ rescue LoadError
143
+ nil
144
+ end
145
+
146
+ def osc_tty?(tty)
147
+ tty &&
148
+ tty.respond_to?(:tty?) && tty.tty? &&
149
+ tty.respond_to?(:raw) &&
150
+ tty.respond_to?(:write) &&
151
+ tty.respond_to?(:read_nonblock)
152
+ end
153
+
154
+ def read_osc_reply(tty, timeout:)
155
+ buf = +''
156
+ deadline = monotonic + timeout
157
+ loop do
158
+ left = deadline - monotonic
159
+ break if left <= 0
160
+
161
+ chunk = read_tty_chunk(tty, left)
162
+ break if chunk.nil?
163
+
164
+ buf << chunk
165
+ break if osc_reply_complete?(buf)
166
+ end
167
+ buf
168
+ end
169
+
170
+ def osc_reply_complete?(buf)
171
+ buf.include?("\a") || buf.include?("\e\\")
172
+ end
173
+
174
+ def read_tty_chunk(tty, wait)
175
+ tty.read_nonblock(256)
176
+ rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable
177
+ return nil unless tty.respond_to?(:to_io) && tty.to_io
178
+
179
+ ready, = IO.select([tty], nil, nil, wait)
180
+ return nil unless ready
181
+
182
+ retry
183
+ end
184
+
185
+ def drain_tty(tty)
186
+ loop { tty.read_nonblock(4096) }
187
+ rescue Errno::EAGAIN, Errno::EWOULDBLOCK, EOFError, IO::WaitReadable
188
+ nil
189
+ end
190
+
191
+ def monotonic
192
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,188 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+ require_relative '../podcast_picker'
5
+ require_relative '../group_help'
6
+
7
+ module Buzzsprout
8
+ module Commands
9
+ module GroupHelpHook
10
+ def run
11
+ return group_help if take_help_flag! || show_bare_help?
12
+
13
+ super
14
+ end
15
+ end
16
+
17
+ class Base
18
+ def self.inherited(klass)
19
+ super
20
+ klass.prepend(GroupHelpHook)
21
+ end
22
+
23
+ def initialize(args, options, io: $stdout, err: $stderr, stdin: $stdin)
24
+ @args = args
25
+ @options = options
26
+ @io = io
27
+ @err = err
28
+ @stdin = stdin
29
+ @formatter = Formatter.new(io: io)
30
+ end
31
+
32
+ private
33
+
34
+ def take_help_flag!
35
+ removed = false
36
+ @args.delete_if do |arg|
37
+ next false unless %w[-h --help].include?(arg)
38
+
39
+ removed = true
40
+ true
41
+ end
42
+ removed
43
+ end
44
+
45
+ def show_bare_help?
46
+ return false unless self.class.const_defined?(:HELP_WHEN_BARE) && self.class::HELP_WHEN_BARE
47
+
48
+ incomplete?
49
+ end
50
+
51
+ def incomplete?
52
+ @args.empty?
53
+ end
54
+
55
+ def group_help
56
+ GroupHelp.print(help_page, io: @io)
57
+ 0
58
+ end
59
+
60
+ def help_page
61
+ page = self.class.const_defined?(:HELP_PAGE) ? self.class::HELP_PAGE.dup : {}
62
+ page[:usage] ||= usage_line
63
+ page[:name] ||= usage_line.to_s.split[1]
64
+ page
65
+ end
66
+
67
+ def client
68
+ @client ||= Client.new(
69
+ token: @options.dry_run? ? Config.token : Config.require_token!,
70
+ auth_scheme: Config.oauth? ? 'Bearer' : 'Token',
71
+ token_provider: Config.oauth? ? -> { OAuth.access_token } : nil,
72
+ dry_run: @options.dry_run?,
73
+ io: @err
74
+ )
75
+ end
76
+
77
+ def output(data)
78
+ return if @options.dry_run? && data.nil?
79
+
80
+ if @options.json?
81
+ @formatter.json(data)
82
+ elsif block_given?
83
+ yield data
84
+ else
85
+ @formatter.json(data)
86
+ end
87
+ end
88
+
89
+ def shift_id!(label)
90
+ value = @args.shift
91
+ raise "Missing #{label}. #{usage_line}" if value.nil? || value.start_with?('-')
92
+
93
+ value
94
+ end
95
+
96
+ def podcast_id_from_args!
97
+ resolve_podcast_id!
98
+ end
99
+
100
+ # 1. Explicit positional / --podcast / --in / BUZZSPROUT_PODCAST_ID
101
+ # 2. Saved default (skip picker) unless persist:true (`buzzsprout podcast use`)
102
+ # 3. Interactive picker on a TTY, then save as default
103
+ # 4. Noninteractive: saved default, else a clear error (never blocks)
104
+ def resolve_podcast_id!(persist: false)
105
+ explicit = take_explicit_podcast_id
106
+ if explicit
107
+ Config.save(default_podcast_id: explicit) if persist
108
+ return explicit
109
+ end
110
+
111
+ unless persist
112
+ default = Config.saved_podcast_id
113
+ return default if default
114
+ end
115
+
116
+ if interactive_podcast_picker?
117
+ chosen = pick_podcast!(announce: !persist)
118
+ Config.save(default_podcast_id: chosen['id'])
119
+ return chosen['id']
120
+ end
121
+
122
+ default = Config.saved_podcast_id
123
+ return default if default
124
+
125
+ raise PodcastPicker::MISSING_ID
126
+ end
127
+
128
+ def interactive_podcast_picker?
129
+ PodcastPicker.interactive?(stdin: @stdin, json: @options.json?)
130
+ end
131
+
132
+ def pick_podcast!(announce: true)
133
+ podcasts = fetch_podcasts
134
+ if podcasts.size == 1
135
+ chosen = podcasts.first
136
+ announce_podcast(chosen) if announce
137
+ return chosen
138
+ end
139
+
140
+ chosen = PodcastPicker.choose!(
141
+ podcasts,
142
+ io: @io,
143
+ stdin: @stdin,
144
+ default_id: Config.saved_podcast_id,
145
+ auto_single: false
146
+ )
147
+ announce_podcast(chosen) if announce
148
+ chosen
149
+ end
150
+
151
+ def take_explicit_podcast_id
152
+ from_args = @args.first
153
+ if from_args && !from_args.start_with?('-')
154
+ return @args.shift
155
+ end
156
+
157
+ value = @options.podcast_id || ENV['BUZZSPROUT_PODCAST_ID']
158
+ return if value.nil? || value.to_s.empty?
159
+
160
+ value
161
+ end
162
+
163
+ def fetch_podcasts
164
+ PodcastPicker.wrap_list(client.get('/podcasts'))
165
+ end
166
+
167
+ def announce_podcast(podcast)
168
+ return if @options.json?
169
+
170
+ title = podcast['title']
171
+ label = title.to_s.empty? ? podcast['id'].to_s : "#{podcast['id']} #{title}"
172
+ @io.puts Color.dim("Using podcast #{label}", io: @io)
173
+ end
174
+
175
+ def usage_line
176
+ self.class.const_defined?(:USAGE) ? self.class::USAGE : ''
177
+ end
178
+
179
+ def encode_id(value)
180
+ URI.encode_www_form_component(value.to_s)
181
+ end
182
+
183
+ def confirm_empty_delete
184
+ true
185
+ end
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buzzsprout
4
+ module Commands
5
+ class BrandAffiliations < Base
6
+ USAGE = 'buzzsprout brand-affiliations [command] [flags]'
7
+ HELP_PAGE = {
8
+ summary: 'Manage brand affiliations for a podcast.',
9
+ usage: 'buzzsprout brand-affiliations <command> [id]',
10
+ aliases: %w[brand-affiliations brand_affiliations brands],
11
+ commands: [
12
+ ['list', 'List affiliations (default)'],
13
+ ['show', 'Show one affiliation'],
14
+ ['create', 'Create an affiliation (--headline)'],
15
+ ['update', 'Update an affiliation'],
16
+ ['delete', 'Delete an affiliation'],
17
+ ['assign', 'Assign an affiliation to an episode'],
18
+ ['unassign', 'Unassign an affiliation from an episode']
19
+ ],
20
+ examples: [
21
+ 'buzzsprout brand-affiliations',
22
+ 'buzzsprout brand-affiliations create --headline "Sponsor"'
23
+ ]
24
+ }.freeze
25
+
26
+ def run
27
+ verb = @args.shift || 'list'
28
+ return group_help unless %w[list show create update delete assign unassign].include?(verb)
29
+
30
+ send(verb)
31
+ end
32
+
33
+ private
34
+
35
+ def list
36
+ podcast_id = podcast_id_from_args!
37
+ data = client.get("/#{podcast_id}/brand_affiliations")
38
+ output(data) do
39
+ @formatter.heading("Brand affiliations for podcast #{podcast_id}")
40
+ Array(data).each { |row| @io.puts "#{row['id']} #{row['headline']} #{row['url']}" }
41
+ end
42
+ 0
43
+ end
44
+
45
+ def show
46
+ podcast_id = podcast_id_from_args!
47
+ id = shift_id!('id')
48
+ data = client.get("/#{podcast_id}/brand_affiliations/#{id}")
49
+ output(data) { @formatter.show_record("Brand affiliation #{id}", data, %w[id headline description url artwork_url]) }
50
+ 0
51
+ end
52
+
53
+ def create
54
+ podcast_id = podcast_id_from_args!
55
+ body = @options.merge_body
56
+ raise 'headline is required' if body['headline'].to_s.empty?
57
+
58
+ data = client.post("/#{podcast_id}/brand_affiliations", body: body)
59
+ output(data) { @io.puts "Created brand affiliation #{data && data['id']}" }
60
+ 0
61
+ end
62
+
63
+ def update
64
+ podcast_id = podcast_id_from_args!
65
+ id = shift_id!('id')
66
+ data = client.patch("/#{podcast_id}/brand_affiliations/#{id}", body: @options.merge_body)
67
+ output(data) { @io.puts "Updated brand affiliation #{id}" }
68
+ 0
69
+ end
70
+
71
+ def delete
72
+ podcast_id = podcast_id_from_args!
73
+ id = shift_id!('id')
74
+ data = client.delete("/#{podcast_id}/brand_affiliations/#{id}")
75
+ output(data) { @io.puts "Deleted brand affiliation #{id}" }
76
+ 0
77
+ end
78
+
79
+ def assign
80
+ podcast_id = podcast_id_from_args!
81
+ episode_id = shift_id!('episode_id')
82
+ brand_id = @options.fields['brand_affiliation_id'] || shift_id!('brand_affiliation_id')
83
+ data = client.post("/#{podcast_id}/episodes/#{episode_id}/brand_affiliations", body: { brand_affiliation_id: brand_id })
84
+ output(data) { @io.puts "Assigned brand affiliation #{brand_id} to episode #{episode_id}" }
85
+ 0
86
+ end
87
+
88
+ def unassign
89
+ podcast_id = podcast_id_from_args!
90
+ episode_id = shift_id!('episode_id')
91
+ id = shift_id!('id')
92
+ data = client.delete("/#{podcast_id}/episodes/#{episode_id}/brand_affiliations/#{id}")
93
+ output(data) { @io.puts "Unassigned brand affiliation #{id} from episode #{episode_id}" }
94
+ 0
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buzzsprout
4
+ module Commands
5
+ class Chapters < Base
6
+ USAGE = 'buzzsprout chapters <command> [flags]'
7
+ HELP_WHEN_BARE = true
8
+ HELP_PAGE = {
9
+ summary: 'List or replace episode chapters.',
10
+ usage: 'buzzsprout chapters <command> <episode_id>',
11
+ aliases: %w[chapters],
12
+ commands: [
13
+ ['list', 'List chapters'],
14
+ ['replace', 'Replace chapters (--body JSON)']
15
+ ],
16
+ examples: [
17
+ 'buzzsprout chapters list 788881',
18
+ 'buzzsprout chapters replace 788881 --body \'[{"title":"Intro","start_time":"00:00:00"}]\''
19
+ ]
20
+ }.freeze
21
+
22
+ def run
23
+ verb = @args.shift
24
+ case verb
25
+ when 'list' then list
26
+ when 'replace', 'update' then replace
27
+ else
28
+ group_help
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def list
35
+ podcast_id = podcast_id_from_args!
36
+ episode_id = shift_id!('episode_id')
37
+ data = client.get("/#{podcast_id}/episodes/#{episode_id}/chapters")
38
+ output(data) do
39
+ @formatter.heading("Chapters for episode #{episode_id}")
40
+ Array(data).each { |row| @io.puts "#{row['start_time']} #{row['title']} #{row['url']}" }
41
+ end
42
+ 0
43
+ end
44
+
45
+ def replace
46
+ podcast_id = podcast_id_from_args!
47
+ episode_id = shift_id!('episode_id')
48
+ body = @options.body
49
+ raise 'Provide --body with a chapters array or {\"chapters\": [...]}' if body.nil?
50
+
51
+ payload = body.is_a?(Array) ? { chapters: body } : body
52
+ data = client.put("/#{podcast_id}/episodes/#{episode_id}/chapters", body: payload)
53
+ output(data) { @io.puts "Replaced chapters on episode #{episode_id}" }
54
+ 0
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buzzsprout
4
+ module Commands
5
+ class Contributors < Base
6
+ USAGE = 'buzzsprout contributors [command] [flags]'
7
+ HELP_PAGE = {
8
+ summary: 'Manage podcast contributors.',
9
+ usage: 'buzzsprout contributors <command> [id]',
10
+ aliases: %w[contributors],
11
+ commands: [
12
+ ['list', 'List contributors (default)'],
13
+ ['show', 'Show one contributor'],
14
+ ['create', 'Create a contributor (--name)'],
15
+ ['update', 'Update a contributor'],
16
+ ['delete', 'Delete a contributor'],
17
+ ['assign', 'Assign a contributor to an episode'],
18
+ ['unassign', 'Unassign a contributor from an episode']
19
+ ],
20
+ examples: [
21
+ 'buzzsprout contributors',
22
+ 'buzzsprout contributors create --name "Jane Doe" --role host --file ./headshot.jpg',
23
+ 'buzzsprout contributors update 123 --file ./headshot.png'
24
+ ]
25
+ }.freeze
26
+
27
+ def run
28
+ dispatch(%w[list show create update delete assign unassign])
29
+ end
30
+
31
+ private
32
+
33
+ def dispatch(verbs)
34
+ verb = @args.shift || 'list'
35
+ return group_help unless verbs.include?(verb)
36
+
37
+ send(verb)
38
+ end
39
+
40
+ def list
41
+ podcast_id = podcast_id_from_args!
42
+ data = client.get("/#{podcast_id}/contributors")
43
+ output(data) do
44
+ @formatter.heading("Contributors for podcast #{podcast_id}")
45
+ Array(data).each { |row| @io.puts "#{row['id']} #{row['name']} (#{row['role']})" }
46
+ end
47
+ 0
48
+ end
49
+
50
+ def show
51
+ podcast_id = podcast_id_from_args!
52
+ id = shift_id!('contributor_id')
53
+ data = client.get("/#{podcast_id}/contributors/#{id}")
54
+ output(data) { @formatter.show_record("Contributor #{id}", data, %w[id name role bio url headshot_url]) }
55
+ 0
56
+ end
57
+
58
+ def create
59
+ podcast_id = podcast_id_from_args!
60
+ body = @options.merge_body
61
+ raise 'name is required' if body['name'].to_s.empty?
62
+
63
+ data = write_contributor(:post, "/#{podcast_id}/contributors", body)
64
+ output(data) { @io.puts "Created contributor #{data && data['id']}: #{data && data['name']}" }
65
+ 0
66
+ end
67
+
68
+ def update
69
+ podcast_id = podcast_id_from_args!
70
+ id = shift_id!('contributor_id')
71
+ data = write_contributor(:patch, "/#{podcast_id}/contributors/#{id}", @options.merge_body)
72
+ output(data) { @io.puts "Updated contributor #{id}" }
73
+ 0
74
+ end
75
+
76
+ def write_contributor(method, path, body)
77
+ file = @options.file
78
+ return client.public_send(method, path, body: body) if file.nil?
79
+
80
+ raise 'Provide --file PATH' if file.empty?
81
+ raise "File not found: #{file}" unless File.file?(file)
82
+
83
+ content_type = { '.png' => 'image/png', '.jpg' => 'image/jpeg', '.jpeg' => 'image/jpeg', '.webp' => 'image/webp' }[File.extname(file).downcase]
84
+ raise 'Headshot must be a PNG, JPEG, or WebP file' unless content_type
85
+
86
+ parts = body.merge('headshot_file' => {
87
+ filename: File.basename(file), content_type: content_type, data: File.binread(file)
88
+ })
89
+ client.public_send(method, path, multipart: parts)
90
+ end
91
+
92
+ def delete
93
+ podcast_id = podcast_id_from_args!
94
+ id = shift_id!('contributor_id')
95
+ data = client.delete("/#{podcast_id}/contributors/#{id}")
96
+ output(data) { @io.puts "Deleted contributor #{id}" }
97
+ 0
98
+ end
99
+
100
+ def assign
101
+ podcast_id = podcast_id_from_args!
102
+ episode_id = shift_id!('episode_id')
103
+ contributor_id = @options.fields['contributor_id'] || shift_id!('contributor_id')
104
+ data = client.post("/#{podcast_id}/episodes/#{episode_id}/contributors", body: { contributor_id: contributor_id })
105
+ output(data) { @io.puts "Assigned contributor #{contributor_id} to episode #{episode_id}" }
106
+ 0
107
+ end
108
+
109
+ def unassign
110
+ podcast_id = podcast_id_from_args!
111
+ episode_id = shift_id!('episode_id')
112
+ id = shift_id!('contributor_id')
113
+ data = client.delete("/#{podcast_id}/episodes/#{episode_id}/contributors/#{id}")
114
+ output(data) { @io.puts "Unassigned contributor #{id} from episode #{episode_id}" }
115
+ 0
116
+ end
117
+ end
118
+ end
119
+ end