-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux.script.notion.rb
executable file
·126 lines (106 loc) · 2.85 KB
/
tmux.script.notion.rb
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env ruby
require 'rest-client'
require 'json'
require 'yaml'
module NotionTask
STATUS_CONFIG = {
"Not started" => {emoji: " ", color: :gray},
"Ready to start" => {emoji: "\uf9fd", color: :gray},
"Scheduled" => {emoji: " ", color: :yellow},
"In progress" => {emoji: " ", color: :blue}
}
class Client
NOTION_API_URL = "https://api.notion.com/v1/databases"
def initialize(config)
@config = config
end
def fetch_tasks
body = construct_request_body
response = make_api_request(body)
JSON.parse(response.body)["results"]
end
private
def construct_request_body
{
"filter": {
"and": [
{
"property": "Assignee",
"people": {
"contains": @config['user_id']
}
},
{
"or": STATUS_CONFIG.keys.map do |status|
{
"property": "Status",
"status": {
"equals": status
}
}
end
}
]
}
}
end
def make_api_request(body)
url = "#{NOTION_API_URL}/#{@config['database_id']}/query"
headers = {
:Authorization => 'Bearer ' + @config['integration_secret'],
:content_type => :json,
:accept => :json,
'Notion-Version' => '2022-06-28'
}
begin
RestClient.post(url, body.to_json, headers)
rescue RestClient::ExceptionWithResponse => e
puts 'Failed to fetch data from Notion API.'
Kernel.abort
end
end
end
class Processor
def initialize
@status_counts = Hash.new(0)
STATUS_CONFIG.keys.each { |status| @status_counts[status] = 0 }
end
def process(tasks)
tasks.each do |task|
status_name = task["properties"]["Status"]["status"]["name"]
@status_counts[status_name] += 1 if @status_counts.key?(status_name)
end
@status_counts
end
end
class Reporter
def initialize(status_counts)
@status_counts = status_counts
end
def display
output = @status_counts.map do |status, count|
emoji = STATUS_CONFIG[status][:emoji]
color = STATUS_CONFIG[status][:color]
colorize(emoji + count.to_s, color)
end.join(" ")
puts output
end
private
def colorize(text, color)
colors = {
gray: "#[fg=darkgray]",
yellow: "#[fg=yellow]",
blue: "#[fg=blue]"
}
"#{colors[color]}#{text}#[fg=default]"
end
end
end
# Main program
config = YAML.load_file(__dir__ + '/config/notion.yml')
client = NotionTask::Client.new(config)
tasks = client.fetch_tasks
processor = NotionTask::Processor.new
status_counts = processor.process(tasks)
reporter = NotionTask::Reporter.new(status_counts)
reporter.display