-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
133 lines (103 loc) · 4.16 KB
/
server.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
127
128
129
130
131
132
133
require 'sinatra'
require 'http'
require 'digest'
get '/ifttt/v1/status' do
halt 401 unless request.env.fetch('HTTP_IFTTT_CHANNEL_KEY') == ENV.fetch("IFFT_SERVICE_KEY")
{ status: "OK" }.to_json
end
post '/ifttt/v1/test/setup' do
halt 401 unless request.env.fetch('HTTP_IFTTT_CHANNEL_KEY') == ENV.fetch("IFFT_SERVICE_KEY")
{
"data": {
"samples": {
"triggers": {
"search-trigger": {
"keywords": "what then"
},
"registers": {
"register": "country"
},
"companies": {
"company_number": "09426399"
},
}
}
}
}.to_json
end
# https://platform.ifttt.com/docs/api_reference
post '/ifttt/v1/triggers/search-trigger' do
halt 401, { errors: [ { message: "Wrong channel key" }] }.to_json unless request.env.fetch('HTTP_IFTTT_CHANNEL_KEY') == ENV.fetch("IFFT_SERVICE_KEY")
data = JSON.parse(request.body.read)
keywords = data.dig("triggerFields", "keywords")
halt 400, { errors: [ { message: "There weren't any keywords!" }] }.to_json unless keywords
search_params = {
count: data["limit"] || 50,
order: '-public_timestamp',
fields: %w[public_timestamp link title]
}
unless keywords == ""
search_params.merge!(q: keywords)
end
response = JSON.parse(HTTP.get("https://www.gov.uk/api/search.json", params: search_params))
entries = response["results"].map do |result|
public_timestamp = Time.parse(result["public_timestamp"])
{
title: result["title"],
url: "https://www.gov.uk#{result["link"]}",
created_at: public_timestamp.iso8601,
meta: {
id: Digest::MD5.hexdigest(result["public_timestamp"]),
timestamp: public_timestamp.to_i,
}
}
end
{ data: entries }.to_json
end
post '/ifttt/v1/triggers/registers/fields/register/options' do
halt 401, { errors: [ { message: "Wrong channel key" }] }.to_json unless request.env.fetch('HTTP_IFTTT_CHANNEL_KEY') == ENV.fetch("IFFT_SERVICE_KEY")
response = JSON.parse(HTTP.get('https://register.register.gov.uk/records.json'))
label_and_values = response.map do |register_id, info|
{ label: info["item"][0]["text"], value: register_id }
end
{ data: label_and_values }.to_json
end
post '/ifttt/v1/triggers/registers' do
halt 401, { errors: [ { message: "Wrong channel key" }] }.to_json unless request.env.fetch('HTTP_IFTTT_CHANNEL_KEY') == ENV.fetch("IFFT_SERVICE_KEY")
data = JSON.parse(request.body.read)
register_id = data.dig("triggerFields", "register")
halt 400, { errors: [ { message: "Register not specified" }] }.to_json unless register_id
register_data = JSON.parse(HTTP.get("https://#{register_id}.register.gov.uk/register"))
register_name = register_data["register-record"]["text"]
# This will stop working once there are more than 5000 entries in a register
response = JSON.parse(HTTP.get("https://#{register_id}.register.gov.uk/entries.json?limit=5000"))
entries = response.reverse.first(data["limit"] || 50).map do |entry|
{
updated_record: "https://#{register_id}.register.gov.uk/record/#{entry["key"]}",
register_name: register_name,
meta: {
id: entry["index-entry-number"],
timestamp: Time.parse(entry["entry-timestamp"]).to_i,
}
}
end
{ data: entries }.to_json
end
post '/ifttt/v1/triggers/companies' do
halt 401, { errors: [ { message: "Wrong channel key" }] }.to_json unless request.env.fetch('HTTP_IFTTT_CHANNEL_KEY') == ENV.fetch("IFFT_SERVICE_KEY")
data = JSON.parse(request.body.read)
company_number = data.dig("triggerFields", "company_number")
halt 400, { errors: [ { message: "Company number not specified" }] }.to_json unless company_number
response = JSON.parse(HTTP.auth(ENV.fetch("COMPANIES_HOUSE_API_KEY")).get("https://api.companieshouse.gov.uk/company/#{company_number}/filing-history"))
entries = response["items"].first(data["limit"] || 50).map do |entry|
{
summary: entry["description"],
company_url: "https://beta.companieshouse.gov.uk/company/#{company_number}",
meta: {
id: entry["transaction_id"],
timestamp: Time.parse(entry["date"]).to_i,
}
}
end
{ data: entries }.to_json
end