Skip to content

Commit f30e2b8

Browse files
committed
Params as JSON::Any and complete channel options.
1 parent f071af6 commit f30e2b8

File tree

6 files changed

+67
-22
lines changed

6 files changed

+67
-22
lines changed

spec/cases/context_spec.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe Teletube::Context do
77
context.errors = ["Something went wrong"]
88
context.resource = "channels"
99
context.command = "create"
10-
context.params = { "token" => "secret" }
10+
context.params = { "token" => JSON::Any.new("secret") }
1111
context.run = false
1212
context.verbose = true
1313
context.command.should eq("create")

src/teletube/cli.cr

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ module Teletube
2222
def run_command
2323
case context.resource
2424
when "config"
25-
@config.attributes = context.params
25+
@config.attributes = {
26+
"endpoint" => context.params["endpoint"].as_s,
27+
"token" => context.params["token"].as_s
28+
}
2629
@config.save
2730
when "categories"
2831
puts @client.get_categories

src/teletube/client.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module Teletube
5151
response = perform_file_upload(instructions)
5252
case response.status_code
5353
when 200..399
54-
@context.params["secret"] = instructions["secret"].as_s
54+
@context.params["secret"] = instructions["secret"]
5555
create_video
5656
else
5757
"Upload failed with status code #{response.status_code}"

src/teletube/context.cr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
require "json"
2+
13
module Teletube
24
# Keeps the details of how the CLI tool was called.
35
class Context
46
property errors : Array(String)
57
property resource : String?
68
property command : String?
7-
property params : Hash(String, String)
9+
property params : Hash(String, JSON::Any)
810
property filename : String?
911
property? run
1012
property? verbose
1113

1214
def initialize
1315
@errors = [] of String
14-
@params = {} of String => String
16+
@params = {} of String => JSON::Any
1517
@run = true
1618
@verbose = false
1719
end

src/teletube/http.cr

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ module Teletube
1616
@http.get(path: path, headers: @headers)
1717
end
1818

19-
def get(path : String, params : Hash(String, String))
19+
def get(path : String, params : Hash(String, JSON::Any))
20+
params = params.each_with_object({} of String => String) do |(name, value), h|
21+
h[name] = value.as_s
22+
end
2023
@http.get(path: "#{path}?#{HTTP::Params.encode(params)}", headers: @headers)
2124
end
2225

2326
def post(path : String)
2427
@http.post(path: path, headers: @headers)
2528
end
2629

27-
def post(path : String, params : Hash(String, String))
30+
def post(path : String, params : Hash(String, JSON::Any))
2831
@http.post(path: path, headers: @headers, body: params.to_json)
2932
end
3033

31-
def patch(path : String, params : Hash(String, String))
34+
def patch(path : String, params : Hash(String, JSON::Any))
3235
@http.patch(path: path, headers: @headers, body: params.to_json)
3336
end
3437

src/teletube/option_parser.cr

+51-14
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ module Teletube
1414
if token.empty?
1515
context.errors << "Please specify an access token."
1616
else
17-
context.params["token"] = token
17+
context.params["token"] = JSON::Any.new(token)
1818
end
1919
end
2020
parser.on("--endpoint ENDPOINT", "Base endpoint to use to contact the web service") do |endpoint|
2121
if endpoint.empty?
2222
context.errors << "Please specify and endpoint."
2323
else
24-
context.params["endpoint"] = endpoint
24+
context.params["endpoint"] = JSON::Any.new(endpoint)
2525
end
2626
end
2727
end
@@ -40,37 +40,74 @@ module Teletube
4040
context.command = "list"
4141
parser.on("--role ROLE", "The role of the authenticated profile.") do |role|
4242
if %w[owner contributor].includes?(role)
43-
context.params["role"] = role
43+
context.params["role"] = JSON::Any.new(role)
4444
else
4545
context.errors << "Please specify either owner or contributor for the role."
4646
end
4747
end
4848
end
49+
channel_params = -> {
50+
parser.on("--name NAME", "Channel name.") do |name|
51+
context.params["name"] = JSON::Any.new(name)
52+
end
53+
parser.on("--description DESCRIPTION", "Channel description.") do |description|
54+
context.params["description"] = JSON::Any.new(description)
55+
end
56+
parser.on("--category CATEGORY", "Channel category.") do |category|
57+
context.params["category"] = JSON::Any.new(category)
58+
end
59+
parser.on("--language LANGUAGE", "Channel language.") do |language|
60+
context.params["language"] = JSON::Any.new(language)
61+
end
62+
parser.on("--viewable-by VIEWABLE_BY", "Choose: all, all-hidden, authenticated, organization, or collaborators.") do |viewable_by|
63+
context.params["viewable_by"] = JSON::Any.new(viewable_by)
64+
end
65+
parser.on("--open-channel", "Mark channel as Open Channel.") do
66+
context.params["open_channel"] = JSON::Any.new(true)
67+
end
68+
parser.on("--commentable", "Turn on comments.") do
69+
context.params["commentable"] = JSON::Any.new(true)
70+
end
71+
parser.on("--downloadable", "Allow video downloads.") do
72+
context.params["downloadable"] = JSON::Any.new(true)
73+
end
74+
parser.on("--archive", "The uploaded video files will be archived.") do
75+
context.params["archive_original_video_files"] = JSON::Any.new(true)
76+
end
77+
parser.on("--external-playback", "Allow external playback.") do
78+
context.params["external_playback"] = JSON::Any.new(true)
79+
end
80+
parser.on("--hide-owner", "Name of the channel owner will not be shown.") do
81+
context.params["hide_owner"] = JSON::Any.new(true)
82+
end
83+
parser.on("--podcast", "All uploaded files will be processed as a podcast and viewers will be able to subscribe to a feed.") do
84+
context.params["podcast"] = JSON::Any.new(true)
85+
end
86+
parser.on("--explicit", "Contains media only suited for mature audiences.") do
87+
context.params["explicit"] = JSON::Any.new(true)
88+
end
89+
}
4990
parser.on("create", "Create a new channel.") do
5091
context.command = "create"
51-
parser.on("--name NAME", "The name of the channel.") do |name|
52-
context.params["name"] = name
53-
end
92+
channel_params.call
5493
end
5594
parser.on("show", "Show details about a channel.") do
5695
context.command = "show"
5796
parser.on("--id ID", "The identifier of the channel to show.") do |id|
58-
context.params["id"] = id
97+
context.params["id"] = JSON::Any.new(id)
5998
end
6099
end
61100
parser.on("update", "Update details for a channel.") do
62101
context.command = "update"
63102
parser.on("--id ID", "The identifier of the channel to update.") do |id|
64-
context.params["id"] = id
65-
end
66-
parser.on("--name NAME", "The name of the channel.") do |name|
67-
context.params["name"] = name
103+
context.params["id"] = JSON::Any.new(id)
68104
end
105+
channel_params.call
69106
end
70107
parser.on("destroy", "Destroy a channel.") do
71108
context.command = "destroy"
72109
parser.on("--id ID", "The identifier of the channel to destroy.") do |id|
73-
context.params["id"] = id
110+
context.params["id"] = JSON::Any.new(id)
74111
end
75112
end
76113
end
@@ -80,13 +117,13 @@ module Teletube
80117
parser.on("create", "Create a new upload.") do
81118
context.command = "create"
82119
parser.on("--channel-id ID", "The id of the channel that will contain the video.") do |channel_id|
83-
context.params["channel_id"] = channel_id
120+
context.params["channel_id"] = JSON::Any.new(channel_id)
84121
end
85122
end
86123
parser.on("perform", "Create a new upload, perform the upload, and create a video.") do
87124
context.command = "perform"
88125
parser.on("--channel-id ID", "The id of the channel that will contain the video.") do |channel_id|
89-
context.params["channel_id"] = channel_id
126+
context.params["channel_id"] = JSON::Any.new(channel_id)
90127
end
91128
end
92129
end

0 commit comments

Comments
 (0)