tangled
alpha
login
or
join now
mackuba.eu
/
blue_factory
1
fork
atom
A simple Ruby server using Sinatra that serves Bluesky custom feeds
1
fork
atom
overview
issues
pulls
pipelines
oops, forgot to wrap the class in a module
mackuba.eu
5 months ago
fcc8510c
5897194c
+61
-59
1 changed file
expand all
collapse all
unified
split
lib
blue_factory
output_generator.rb
+61
-59
lib/blue_factory/output_generator.rb
···
1
1
require_relative 'errors'
2
2
3
3
-
class OutputGenerator
4
4
-
AT_URI_REGEXP = %r(^at://did:plc:[a-z0-9]+/app\.bsky\.feed\.post/[a-z0-9]+$)
3
3
+
module BlueFactory
4
4
+
class OutputGenerator
5
5
+
AT_URI_REGEXP = %r(^at://did:plc:[a-z0-9]+/app\.bsky\.feed\.post/[a-z0-9]+$)
5
6
6
6
-
def generate(response)
7
7
-
output = {}
7
7
+
def generate(response)
8
8
+
output = {}
8
9
9
9
-
raise InvalidResponseError, ":posts key is missing" unless response.has_key?(:posts)
10
10
-
raise InvalidResponseError, ":posts should be an array" unless response[:posts].is_a?(Array)
10
10
+
raise InvalidResponseError, ":posts key is missing" unless response.has_key?(:posts)
11
11
+
raise InvalidResponseError, ":posts should be an array" unless response[:posts].is_a?(Array)
11
12
12
12
-
output[:feed] = response[:posts].map { |x| process_post_element(x) }
13
13
+
output[:feed] = response[:posts].map { |x| process_post_element(x) }
13
14
14
14
-
if cursor = response[:cursor]
15
15
-
output[:cursor] = cursor.to_s
16
16
-
end
15
15
+
if cursor = response[:cursor]
16
16
+
output[:cursor] = cursor.to_s
17
17
+
end
17
18
18
18
-
if req_id = response[:req_id]
19
19
-
output[:reqId] = req_id.to_s
20
20
-
end
19
19
+
if req_id = response[:req_id]
20
20
+
output[:reqId] = req_id.to_s
21
21
+
end
21
22
22
22
-
output
23
23
-
end
23
23
+
output
24
24
+
end
24
25
25
25
-
def process_post_element(object)
26
26
-
if object.is_a?(String)
27
27
-
validate_uri(object)
28
28
-
{ post: object }
29
29
-
elsif object.is_a?(Hash)
30
30
-
process_post_hash(object)
31
31
-
else
32
32
-
raise InvalidResponseError, "Invalid post entry, expected string or hash: #{object.inspect}"
26
26
+
def process_post_element(object)
27
27
+
if object.is_a?(String)
28
28
+
validate_uri(object)
29
29
+
{ post: object }
30
30
+
elsif object.is_a?(Hash)
31
31
+
process_post_hash(object)
32
32
+
else
33
33
+
raise InvalidResponseError, "Invalid post entry, expected string or hash: #{object.inspect}"
34
34
+
end
33
35
end
34
34
-
end
35
36
36
36
-
def process_post_hash(object)
37
37
-
post = {}
37
37
+
def process_post_hash(object)
38
38
+
post = {}
38
39
39
39
-
if object[:post]
40
40
-
validate_uri(object[:post])
41
41
-
post[:post] = object[:post]
42
42
-
else
43
43
-
raise InvalidResponseError, "Post hash is missing a :post key"
44
44
-
end
40
40
+
if object[:post]
41
41
+
validate_uri(object[:post])
42
42
+
post[:post] = object[:post]
43
43
+
else
44
44
+
raise InvalidResponseError, "Post hash is missing a :post key"
45
45
+
end
45
46
46
46
-
if object[:reason]
47
47
-
post[:reason] = process_post_reason(object[:reason])
48
48
-
end
47
47
+
if object[:reason]
48
48
+
post[:reason] = process_post_reason(object[:reason])
49
49
+
end
49
50
50
50
-
if object[:context]
51
51
-
post[:feedContext] = object[:context].to_s
52
52
-
end
51
51
+
if object[:context]
52
52
+
post[:feedContext] = object[:context].to_s
53
53
+
end
53
54
54
54
-
post
55
55
-
end
55
55
+
post
56
56
+
end
56
57
57
57
-
def process_post_reason(reason)
58
58
-
raise InvalidResponseError, "Invalid post reason: #{reason.inspect}" unless reason.is_a?(Hash)
58
58
+
def process_post_reason(reason)
59
59
+
raise InvalidResponseError, "Invalid post reason: #{reason.inspect}" unless reason.is_a?(Hash)
59
60
60
60
-
if reason[:repost]
61
61
-
{
62
62
-
"$type" => "app.bsky.feed.defs#skeletonReasonRepost",
63
63
-
"repost" => reason[:repost]
64
64
-
}
65
65
-
elsif reason[:pin]
66
66
-
{
67
67
-
"$type" => "app.bsky.feed.defs#skeletonReasonPin"
68
68
-
}
69
69
-
else
70
70
-
raise InvalidResponseError, "Invalid post reason: #{reason.inspect}"
61
61
+
if reason[:repost]
62
62
+
{
63
63
+
"$type" => "app.bsky.feed.defs#skeletonReasonRepost",
64
64
+
"repost" => reason[:repost]
65
65
+
}
66
66
+
elsif reason[:pin]
67
67
+
{
68
68
+
"$type" => "app.bsky.feed.defs#skeletonReasonPin"
69
69
+
}
70
70
+
else
71
71
+
raise InvalidResponseError, "Invalid post reason: #{reason.inspect}"
72
72
+
end
71
73
end
72
72
-
end
73
74
74
74
-
def validate_uri(uri)
75
75
-
if !uri.is_a?(String)
76
76
-
raise InvalidResponseError, "Post URI should be a string: #{uri.inspect}"
77
77
-
elsif uri !~ AT_URI_REGEXP
78
78
-
raise InvalidResponseError, "Invalid post URI: #{uri.inspect}"
75
75
+
def validate_uri(uri)
76
76
+
if !uri.is_a?(String)
77
77
+
raise InvalidResponseError, "Post URI should be a string: #{uri.inspect}"
78
78
+
elsif uri !~ AT_URI_REGEXP
79
79
+
raise InvalidResponseError, "Invalid post URI: #{uri.inspect}"
80
80
+
end
79
81
end
80
82
end
81
83
end