Sha256: 1d0ad7e940553fe52c2e64646358072d2fb0f41c9f6e497ec8e4fdaf93b85c73

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module NotionOrbit
  module Services
    class Notion
      attr_reader :client, :notion_api_key

      def initialize(params = {})
        @client = ::Notion::Client.new(token: params[:notion_api_key])
      end

      def notes(database_id:)
        pages = @client.database_query(id: database_id).results
        
        # only process pages that opt-in to sending the note to Orbit 
        pages = pages.filter { |page| page.properties['Send to Orbit'].checkbox }
        puts pages

        notes = []
        pages.each do |page|
          notes << {
            properties: page_properties(page),
            content: page_content(page)
          }
        end
        notes
      end

      def mark_note_as_synced(page_id, orbit_note_url)
        properties = {
          'Orbit Note URL': orbit_note_url,
          'Orbit Status': [{ text: { content: "OK" }}]
        }
        @client.update_page(id: page_id, properties: properties)
      end

      private

      def page_properties(page)
        {
          email: page[:properties]['Member Email'].email,
          page_id: page[:id]
        }
      end

      def page_content(page)
        raw_blocks = @client.block_children(id: page.id).results
        blocks = NotionOrbit::NotionObjects::Blocks.new(raw_blocks, @client.token)
        content = blocks.to_markdown
        content += "\\n\\n"
        content += "[Open in Notion](#{page_url(page[:id])})"
      end

      def page_url(page_id)
        "https://notion.so/#{ENV['NOTION_WORKSPACE_SLUG']}/#{page_id}}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
notion_orbit-0.0.3 lib/notion_orbit/services/notion.rb