class Notionar::Client

Public Class Methods

new(token) click to toggle source
# File lib/notionar/client.rb, line 3
def initialize(token)
  endpoint = "https://api.notion.com/"
  @version  = "v1"

  url = URI.parse(endpoint)
  @http = Net::HTTP.new(url.host, url.port)
  @http.use_ssl = true
  @headers = {
    "Authorization" => "Bearer #{token}",
    "Content-Type"  => "application/json",
    "Notion-Version" =>  "2021-05-13",
  }
end

Public Instance Methods

database(database_id) click to toggle source
# File lib/notionar/client.rb, line 41
def database(database_id)
  @path = "/#{@version}/databases/#{database_id}"
  request(:get)
end
page(page_id) click to toggle source
# File lib/notionar/client.rb, line 27
def page(page_id)
  @path = "/#{@version}/pages/#{page_id}"
  request(:get)
end
query_database(database_id, property_name, query) click to toggle source
# File lib/notionar/client.rb, line 46
def query_database(database_id, property_name, query)
  @path = "/#{@version}/databases/#{database_id}/query"
  @params = {
    filter: {
      property: property_name,
      select: {
        equals: query
      }
    }
  }
  request(:post)
end
user(user_id) click to toggle source
# File lib/notionar/client.rb, line 22
def user(user_id)
  @path = "/#{@version}/users/#{user_id}"
  request(:get)
end
users() click to toggle source
# File lib/notionar/client.rb, line 17
def users
  @path = "/#{@version}/users"
  request(:get)
end

Private Instance Methods

request(method) click to toggle source
# File lib/notionar/client.rb, line 61
def request(method)
  response =
    case method
    when :get
      @http.get(@path, @headers)
    when :post
      @http.post(@path, JSON.generate(@params), @headers)
    else
      raise "Unknown method - #{method}"
    end

  { body: JSON.parse(response.body), code: response.code }
end