원본:
towardsdatascience.com/introducing-the-notion-api-ruby-gem-d47d4a6ef0ca
시작하기 위해, 토큰 _v2 자격 증명을 검색합니다.
(브라우저에서 notion session을 열고 cookies를 탐색한 다음 토큰 _v2 키를 찾을 수 있다.)
이후, 다음 코드를 사용하여 세션을 시작할 수 있다.
require "notion_api"
@client = NotionAPI::Client.new(ENV['token_v2'])
토큰 _v2 자격 증명을 token_v2라는 환경 변수로 세팅해 준다.
(export token_v2= ‘<token_v2_here>’를 사용하면 된다.)
이것이 끝난 후, .get_page method를 통해 노션 페이지를 검색할 수 있다.
노션 페이지를 검색한 후, 블록들과 새로운 데이터를 쓰거나 읽는 몇 가지 방법을 사용할 수 있다.
1. .children: 페이지의 class instance 같은 children을 찾을 수 있다.
2. .children_ids: 페이지에서 children의 block의 ID를 찾을 수 있다.
3. .create(type_to_create, title, target=nil, position='after'): 페이지에 새로운 child를 만들어 준다.
title 전달인자는 블록에 할당되는 제목이다.
target 전달인자는 before, after에 위치될 새로운 block의 block ID이다.
position 전달인자는 대상에 대해 새로운 block의 상대적인 위치를 나타낸다.
4. .get_block(block_id): 특정 block을 검색한다. block_idsms 검색할 block의 id이다.
Working with Blocks
노션 페이지의 블록과 상호작용할 때 다음 방법에 엑세스할 수 있다.
1. .move(target_block, position): 새로운 위치에 block을 옮기는 것 (before or after the target block)
2. .duplicate(target_block=nil): block을 복제하는 것
3. .create(type_to_create, title, target=nil, position'after'): 해당 블록에 새로운 child를 만들어 주는 것이다.
4. .convert(type_to_convert_to): 블록을 다른 노션의 블록 타입으로 바꿔 준다.
TodoBlock, ToggleBlock, PageBlock, DividerBlock, QuoteBlock, CalloutBlock 등등의 Block으로 변환할 수 있다!!
5. .title=(new_title): new_title로 노션 블록의 title을 바꿔 준다.
몇몇 가지 블록들은 추가적인 메소드들이 존재한다.
예를 들자면, To-do Block은 .checked=(checked_property) 메소드로 "yes", "no"를 선택할 수 있다.
Working with 'Collection Views'
마지막으로, Collection View 블록과 상호작용할 수 있다.
Tables, Lists, Calendars, Galleries, Timelines 들과 같은 블록들이 그 예이다.
csv나 json 데이터가 있으면, .create_collection 메소드를 이용해 새로운 collection view를 만들 수 있다.
.create_collection 메소드는 몇 가지 전달인자를 가진다.
1. collection_type: 만들 collection의 종류이다. table, list, calendar, gallery, and/or timeline
2. collection_title: collection의 title이다.
3. data: JSONfied 데이터, CSV로 작업하고 있으면, 당신은 다음 코드 스니펫을 이용하여 csv 데이터를 적절한 json 포맷으로 변환할 수 있다.
body = File.open("my.csv")
csv = CSV.new(body, :headers => true, :header_converters => :symbol, :converters => :all)
rows = []
# map each row in the CSV to a hash and append it to rows
csv.to_a.each {|row| rows.push(row.to_hash) }
결론과 추천 사용 경우
이 젬을 사용하면 몇 초만에 블록, 컬렉션을 만들고 업데이트할 수 있습니다. 다양한 기능이 제공되므로 일상적인 개념 사용 사례(시스템 검사, 작업 관리 건너가기, 메타 데이터 저장 등)를 자동화하는 데 사용할 수 있습니다.
'Ruby' 카테고리의 다른 글
save the bytes to a file (0) | 2021.07.16 |
---|