Class: NotionAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/api.rb

Overview

Notion API class

Author:

  • Dave Williams

Since:

  • 0.0.2

Instance Method Summary collapse

Constructor Details

#initializeNotionApi

Initialize the Notion API class

Since:

  • 0.0.2



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/api.rb', line 16

def initialize
  if ENV.fetch("NOTION_TASK_API_KEY", false) && ENV.fetch("NOTION_TASK_DATABASE", false)
    @key = ENV.fetch("NOTION_TASK_API_KEY")
    @database = ENV.fetch("NOTION_TASK_DATABASE")
    @headers = {
      "Authorization" => "Bearer #{@key}",
      "Content-Type" => "application/json",
      "Notion-Version" => "2022-02-22"
    }
  else
    raise "NOTION_TASK_API_KEY and NOTION_TASK_DATABASE must be set"
  end
end

Instance Method Details

#GET(path) ⇒ Object

Since:

  • 0.0.2



30
31
32
33
# File 'lib/api.rb', line 30

def GET(path)
  response = HTTParty.get("https://api.notion.com/v1/#{path}", headers: @headers)
  response.body
end

#get_tasksObject

Since:

  • 0.0.2



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/api.rb', line 40

def get_tasks
  query = {
    sorts: [
      {
        property: "Title",
        direction: "ascending"
      }
    ]
  }
  resp = POST("/databases/#{@database}/query", query.to_json)
  puts resp
end

#new_task(title) ⇒ Object

Since:

  • 0.0.2



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/api.rb', line 53

def new_task(title)
  query = {
    parent: {
      database_id: @database
    },
    properties: {
      Title: {
        title: [
          {
            text: {
              content: title
            }
          }
        ]
      }
    }
  }
  resp = POST("/pages", query.to_json)
  puts resp
end

#POST(path, body) ⇒ Object

Since:

  • 0.0.2



35
36
37
38
# File 'lib/api.rb', line 35

def POST(path, body)
  response = HTTParty.post("https://api.notion.com/v1#{path}", headers: @headers, body: body)
  response.body
end