A simple Ruby server using Sinatra that serves Bluesky custom feeds

oops, forgot to wrap the class in a module

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