diff --git a/slices/micropub/request_parser.rb b/slices/micropub/request_parser.rb index a1fc9d7..310b420 100644 --- a/slices/micropub/request_parser.rb +++ b/slices/micropub/request_parser.rb @@ -65,7 +65,7 @@ module Micropub end elsif params[:properties][:photo].is_a?(Hash) params[:properties][:photo] - elsif + elsif params[:properties][:photo] {value: params[:properties][:photo], alt: ""} else [] @@ -126,16 +126,22 @@ module Micropub checkin = params.dig(:properties, :checkin).first new_params[:h] = "entry" - new_params[:syndication_sources] = params.dig(:properties, :syndication) + new_params[:syndication_sources] = params.dig(:properties, :syndication) || [] new_params[:name] = checkin.dig(:properties, :name).first new_params[:content] = params.dig(:properties, :content)&.first || "" new_params[:url] = checkin.dig(:properties, :url)&.first new_params[:slug] = SecureRandom.uuid - new_params[:category] = params.dig(:properties, :category) + new_params[:category] = params.dig(:properties, :category) || [] published = DateTime.parse(params.dig(:properties, :published)&.first).new_offset(0) new_params[:published_at] = published.to_s new_params[:post_type] = :checkin - location = params.dig(:properties, :location).first[:properties] + + location = if params.dig(:properties, :location) + params.dig(:properties, :location).first[:properties] + elsif checkin.dig(:properties, :latitude) && checkin.dig(:properties, :longitude) + {latitude: checkin.dig(:properties, :latitude).first, longitude: checkin.dig(:properties, :longitude).first} + end + new_params[:photos] = params.dig(:properties, :photo)&.map { |p| {value: p, alt: new_params[:name]} } || [] new_params[:location] = "geo:#{location.dig(:latitude).first},#{location.dig(:longitude).first};u=0" new_params diff --git a/spec/requests/create_post_spec.rb b/spec/requests/create_post_spec.rb index 079b0be..34c40d9 100644 --- a/spec/requests/create_post_spec.rb +++ b/spec/requests/create_post_spec.rb @@ -41,6 +41,123 @@ RSpec.describe "Post creation", :db, :requests do expect(post_repo.statuses_listing.count).to eq 1 expect(post_repo.statuses_listing.first.tags.map(&:label)).to eq ["ruby", "rspec"] end + + it "Create an h-entry with HTML content (JSON)" do + params = { + type: ["h-entry"], + properties: { + content: [{ + html: "
This post has bold and italic text.
" + }] + } + } + + post "/micropub", params + + expect(last_response).to be_successful + expect(post_repo.statuses_listing.count).to eq 1 + expect(post_repo.statuses_listing.first.content).to eq "This post has bold and italic text.
\n" + end + + it "Create an h-entry with a photo referenced by URL (JSON)" do + params = { + type: ["h-entry"], + properties: { + content: ["Micropub test of creating a photo referenced by URL. This post should include a photo of a sunset."], + photo: ["https://micropub.rocks/media/sunset.jpg"] + } + } + + post "/micropub", params + + expect(last_response).to be_successful + expect(post_repo.statuses_listing.count).to eq 1 + expect(post_repo.statuses_listing.first.photos.count).to eq 1 + expect(post_repo.statuses_listing.first.content).to eq "Micropub test of creating a photo referenced by URL. This post should include a photo of a sunset.
\n" + end + + it "Create an h-entry post with a nested object (JSON)" do + params = { + type: [ + "h-entry" + ], + properties: { + published: [ + "2017-05-31T12:03:36-07:00" + ], + content: [ + "Lunch meeting" + ], + checkin: [ + { + type: [ + "h-card" + ], + properties: { + name: ["Los Gorditos"], + url: ["https://foursquare.com/v/502c4bbde4b06e61e06d1ebf"], + latitude: [45.524330801154], + longitude: [-122.68068808051], + "street-address": ["922 NW Davis St"], + locality: ["Portland"], + region: ["OR"], + "country-name": ["United States"], + "postal-code": ["97209"] + } + } + ] + } + } + + post "/micropub", params + + expect(last_response).to be_successful + expect(post_repo.places_listing.count).to eq 1 + expect(post_repo.places_listing.first.post_type).to eq "checkin" + expect(post_repo.places_listing.first.content).to eq "Lunch meeting
\n" + end + + it "Create an h-entry post with a photo with alt text (JSON)" do + params = { + type: ["h-entry"], + properties: { + content: ["Micropub test of creating a photo referenced by URL with alt text. This post should include a photo of a sunset."], + photo: [ + { + value: "https://micropub.rocks/media/sunset.jpg", + alt: "Photo of a sunset" + } + ] + } + } + + post "/micropub", params + + expect(last_response).to be_successful + expect(post_repo.statuses_listing.count).to eq 1 + expect(post_repo.statuses_listing.first.photos.count).to eq 1 + expect(post_repo.statuses_listing.first.content).to eq "Micropub test of creating a photo referenced by URL with alt text. This post should include a photo of a sunset.
\n" + end + + it "Create an h-entry with multiple photos referenced by URL (JSON)" do + params = { + type: ["h-entry"], + properties: { + content: ["Micropub test of creating multiple photos referenced by URL. This post should include a photo of a city at night."], + photo: [ + "https://micropub.rocks/media/sunset.jpg", + "https://micropub.rocks/media/city-at-night.jpg" + ] + } + } + + post "/micropub", params + + expect(last_response).to be_successful + expect(post_repo.statuses_listing.count).to eq 1 + expect(post_repo.statuses_listing.first.photos.count).to eq 2 + expect(post_repo.statuses_listing.first.content).to eq "Micropub test of creating multiple photos referenced by URL. This post should include a photo of a city at night.
\n" + end end context "bookmarks" do