-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordler.rb
More file actions
95 lines (81 loc) · 2.18 KB
/
wordler.rb
File metadata and controls
95 lines (81 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env ruby
require 'sinatra/base'
require './lib/wordle.rb'
require 'json'
require 'yaml'
class Wordler < Sinatra::Base
wordle = Wordle.new './data/nytwordle.json'
get '/' do
self.help
end
get '/wordle.?:format?' do
return self.help if params['letters'].nil?
letters = params['letters'].upcase
positional = validate params['positional']
if(letters.length > 5)
if params['format'] == 'json'
content_type 'json'
{ :status => 'error', :msg => "Sorry, wordle uses 5-letter words only." }.to_json
else
content_type 'text'
return "Sorry, wordle uses 5-letter words only."
end
end
words = (wordle.get_words(letters, positional)).sort
if params['format'] == 'json'
content_type 'json'
{ :positional => positional, :words => words }.to_json
elsif params['format'] == 'yaml'
content_type 'yaml'
{ :positional => positional, :words => words }.to_yaml
else
content_type 'text'
words.join("\n")
end
end
post '/wordle.?:format?' do
return self.help if params['letters'].nil?
letters = params['letters'].upcase
positional = validate params['positional']
if(letters.length > 5)
if params['format'] == 'json'
content_type 'json'
{ :status => 'error', :msg => "Sorry, wordle uses 5-letter words only." }.to_json
else
content_type 'text'
return "Sorry, wordle uses 5-letter words only."
end
end
words = (wordle.get_words(letters, positional)).sort
if params['format'] == 'json'
content_type 'json'
{ :positional => positional, :words => words }.to_json
elsif params['format'] == 'yaml'
content_type 'yaml'
{ :positional => positional, :words => words }.to_yaml
else
content_type 'text'
words.join("\n")
end
end
private
def validate positional
case positional
when nil?
return true
when /true/i
return true
when /false/i
return false
else
return true
end
end
def help
headers['Content-Type'] = 'text/plain'
<<HELP
Call the service like this:
/wordle.json?letters=LEDGE
HELP
end
end