Class: Notion::Service
- Inherits:
-
Object
- Object
- Notion::Service
- Defined in:
- lib/utilities/notion/service.rb
Constant Summary collapse
- BASE_URI =
"https://api.notion.com/v1"
Instance Method Summary collapse
- #get_bot_user ⇒ Object
- #get_page(page_id) ⇒ Object
- #get_page_content(page_id) ⇒ Object
- #get_user(user_id) ⇒ Object
-
#initialize ⇒ Service
constructor
A new instance of Service.
- #list_users(page_size: 100, start_cursor: nil) ⇒ Object
- #query_database(database_id, filters: {}, sorts: [], page_size: 100, start_cursor: nil) ⇒ Object
- #search_databases(query: "", page_size: 10) ⇒ Object
- #search_pages(query: "", page_size: 10) ⇒ Object
- #test_connection ⇒ Object
Constructor Details
#initialize ⇒ Service
Returns a new instance of Service.
13 14 15 |
# File 'lib/utilities/notion/service.rb', line 13 def initialize ensure_env! end |
Instance Method Details
#get_bot_user ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/utilities/notion/service.rb', line 165 def get_bot_user response = get_request("/users/me") { id: response["id"], type: response["type"], name: response["name"], bot: { owner: response.dig("bot", "owner"), workspace_name: response.dig("bot", "workspace_name") } } rescue => e log_error("get_bot_user", e) raise e end |
#get_page(page_id) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/utilities/notion/service.rb', line 51 def get_page(page_id) clean_id = clean_notion_id(page_id) response = get_request("/pages/#{clean_id}") { id: response["id"], title: extract_title(response), url: response["url"], created_time: response["created_time"], last_edited_time: response["last_edited_time"], properties: response["properties"] } rescue => e log_error("get_page", e) raise e end |
#get_page_content(page_id) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/utilities/notion/service.rb', line 68 def get_page_content(page_id) clean_id = clean_notion_id(page_id) response = get_request("/blocks/#{clean_id}/children") blocks = response["results"] || [] extract_text_content(blocks) rescue => e log_error("get_page_content", e) raise e end |
#get_user(user_id) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/utilities/notion/service.rb', line 150 def get_user(user_id) response = get_request("/users/#{user_id}") { id: response["id"], type: response["type"], name: response["name"], avatar_url: response["avatar_url"], email: response.dig("person", "email") } rescue => e log_error("get_user", e) raise e end |
#list_users(page_size: 100, start_cursor: nil) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/utilities/notion/service.rb', line 124 def list_users(page_size: 100, start_cursor: nil) params = {page_size: [page_size.to_i, 100].min} params[:start_cursor] = start_cursor if start_cursor response = get_request("/users", params) users = response["results"].map do |user| { id: user["id"], type: user["type"], name: user["name"], avatar_url: user["avatar_url"], email: user.dig("person", "email") } end { users: users, has_more: response["has_more"], next_cursor: response["next_cursor"] } rescue => e log_error("list_users", e) raise e end |
#query_database(database_id, filters: {}, sorts: [], page_size: 100, start_cursor: nil) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/utilities/notion/service.rb', line 79 def query_database(database_id, filters: {}, sorts: [], page_size: 100, start_cursor: nil) clean_id = clean_notion_id(database_id) body = { page_size: [page_size.to_i, 100].min } body[:filter] = filters unless filters.empty? body[:sorts] = sorts unless sorts.empty? body[:start_cursor] = start_cursor if start_cursor response = post_request("/databases/#{clean_id}/query", body) entries = response["results"].map do |page| { id: page["id"], title: extract_title(page), url: page["url"], created_time: page["created_time"], last_edited_time: page["last_edited_time"], properties: page["properties"] } end { entries: entries, has_more: response["has_more"], next_cursor: response["next_cursor"] } rescue => e log_error("query_database", e) raise e end |
#search_databases(query: "", page_size: 10) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/utilities/notion/service.rb', line 34 def search_databases(query: "", page_size: 10) body = { query: query.to_s.strip, page_size: [page_size.to_i, 100].min, filter: { value: "database", property: "object" } } response = post_request("/search", body) parse_search_results(response, "database") rescue => e log_error("search_databases", e) raise e end |
#search_pages(query: "", page_size: 10) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/utilities/notion/service.rb', line 17 def search_pages(query: "", page_size: 10) body = { query: query.to_s.strip, page_size: [page_size.to_i, 100].min, filter: { value: "page", property: "object" } } response = post_request("/search", body) parse_search_results(response, "page") rescue => e log_error("search_pages", e) raise e end |
#test_connection ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/utilities/notion/service.rb', line 112 def test_connection response = get_request("/users/me") { ok: true, user: response["name"] || response["id"], type: response["type"] } rescue => e log_error("test_connection", e) {ok: false, error: e.} end |