mirror of
https://github.com/danbee/micropub.git
synced 2025-03-04 08:59:13 +00:00
Create post JSON parser
This commit is contained in:
parent
5f25e4b4c0
commit
97d7c2df4e
38
lib/micropub/models/post_json_parser.rb
Normal file
38
lib/micropub/models/post_json_parser.rb
Normal file
@ -0,0 +1,38 @@
|
||||
class PostJSONParser
|
||||
def initialize(json)
|
||||
@data = JSON.parse(json)
|
||||
end
|
||||
|
||||
def params
|
||||
{
|
||||
"title" => title,
|
||||
"content" => content,
|
||||
"category" => category,
|
||||
}.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_accessor :data
|
||||
|
||||
def title
|
||||
data.dig("properties", "title", 0)
|
||||
end
|
||||
|
||||
def content
|
||||
data.dig("properties", "content", 0) || nested_content
|
||||
end
|
||||
|
||||
def nested_content
|
||||
text = data.dig("properties", "content", "text", 0)
|
||||
html = data.dig("properties", "content", "html", 0)
|
||||
|
||||
return nil if text.nil? && html.nil?
|
||||
|
||||
{ "text" => text, "html" => html }.compact
|
||||
end
|
||||
|
||||
def category
|
||||
data.dig("properties", "category")
|
||||
end
|
||||
end
|
||||
@ -41,13 +41,17 @@ module Micropub
|
||||
|
||||
def post_params
|
||||
if request.env["CONTENT_TYPE"] == "application/json"
|
||||
request.body.rewind
|
||||
JSON.parse(request.body.read)
|
||||
json_params
|
||||
else
|
||||
params
|
||||
end
|
||||
end
|
||||
|
||||
def json_params
|
||||
request.body.rewind
|
||||
PostJSONParser.new(request.body.read).params
|
||||
end
|
||||
|
||||
def valid_token?
|
||||
token = Indieauth::Token.new(endpoints.token_endpoint)
|
||||
|
||||
|
||||
79
test/lib/micropub/models/post_json_parser_test.rb
Normal file
79
test/lib/micropub/models/post_json_parser_test.rb
Normal file
@ -0,0 +1,79 @@
|
||||
require "test_helper"
|
||||
|
||||
require "micropub/models/post_json_parser"
|
||||
|
||||
describe PostJSONParser do
|
||||
describe "#params" do
|
||||
it "parses basic attributes" do
|
||||
json = <<~JS
|
||||
{
|
||||
"type": ["h-entry"],
|
||||
"properties": {
|
||||
"content": ["Hello, World!"]
|
||||
}
|
||||
}
|
||||
JS
|
||||
|
||||
parser = PostJSONParser.new(json)
|
||||
|
||||
_(parser.params).must_equal(
|
||||
{ "content" => "Hello, World!" }
|
||||
)
|
||||
end
|
||||
|
||||
it "parses content with nested text" do
|
||||
json = <<~JS
|
||||
{
|
||||
"type": ["h-entry"],
|
||||
"properties": {
|
||||
"content": {
|
||||
"text": ["Hello, World!"]
|
||||
}
|
||||
}
|
||||
}
|
||||
JS
|
||||
|
||||
parser = PostJSONParser.new(json)
|
||||
|
||||
_(parser.params).must_equal(
|
||||
{ "content" => { "text" => "Hello, World!" } }
|
||||
)
|
||||
end
|
||||
|
||||
it "parses content with nested html" do
|
||||
json = <<~JS
|
||||
{
|
||||
"type": ["h-entry"],
|
||||
"properties": {
|
||||
"content": {
|
||||
"html": ["<p>Hello, World!</p>"]
|
||||
}
|
||||
}
|
||||
}
|
||||
JS
|
||||
|
||||
parser = PostJSONParser.new(json)
|
||||
|
||||
_(parser.params).must_equal(
|
||||
{ "content" => { "html" => "<p>Hello, World!</p>" } }
|
||||
)
|
||||
end
|
||||
|
||||
it "parses a single category" do
|
||||
json = <<~JS
|
||||
{
|
||||
"type": ["h-entry"],
|
||||
"properties": {
|
||||
"category": ["test"]
|
||||
}
|
||||
}
|
||||
JS
|
||||
|
||||
parser = PostJSONParser.new(json)
|
||||
|
||||
_(parser.params).must_equal(
|
||||
{ "category" => ["test"] }
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -62,8 +62,11 @@ describe "create post" do
|
||||
|
||||
it "creates a post with JSON" do
|
||||
post_json = {
|
||||
content: "Hello, World!",
|
||||
category: ["one", "two", "three"],
|
||||
type: "h-entry",
|
||||
properties: {
|
||||
content: ["Hello, World!"],
|
||||
category: ["one", "two", "three"],
|
||||
},
|
||||
}.to_json
|
||||
|
||||
post "/micropub/main", post_json, { "CONTENT_TYPE" => "application/json" }
|
||||
@ -76,8 +79,11 @@ describe "create post" do
|
||||
|
||||
it "creates a post with JSON and HTML content" do
|
||||
post_json = {
|
||||
content: { html: "<p>Hello, World!</p>" },
|
||||
category: ["one", "two", "three"],
|
||||
type: "h-entry",
|
||||
properties: {
|
||||
content: { html: ["<p>Hello, World!</p>"] },
|
||||
category: ["one", "two", "three"],
|
||||
},
|
||||
}.to_json
|
||||
|
||||
post "/micropub/main", post_json, { "CONTENT_TYPE" => "application/json" }
|
||||
|
||||
Loading…
Reference in New Issue
Block a user