Class: NotionAPI
- Inherits:
-
Object
- Object
- NotionAPI
- Defined in:
- lib/api.rb
Overview
Notion API class
Instance Method Summary collapse
- #GET(path) ⇒ Object
- #get_tasks ⇒ Object
-
#initialize ⇒ NotionApi
constructor
Initialize the Notion API class.
- #new_task(title) ⇒ Object
- #POST(path, body) ⇒ Object
Constructor Details
#initialize ⇒ NotionApi
Initialize the Notion API class
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
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_tasks ⇒ Object
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
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
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 |