github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/tests/system/lib/note.rb (about)

     1  class Note
     2    attr_reader :file, :title, :dir_id, :schema
     3  
     4    include Model
     5  
     6    def self.doctype
     7      "io.cozy.notes.documents"
     8    end
     9  
    10    def self.default_schema
    11      {
    12        "nodes": [
    13          ["doc", { "content": "block+" }],
    14          ["paragraph", { "content": "inline*", "group": "block" }],
    15          ["blockquote", { "content": "block+", "group": "block" }],
    16          ["horizontal_rule", { "group": "block" }],
    17          ["heading", { "content": "inline*", "group": "block" }],
    18          ["text", { "group": "inline" }],
    19          ["hard_break", { "group": "inline", "inline": true }]
    20        ],
    21        "marks": [
    22          ["link", { "attrs": { "href": {}, "title": {} }, "inclusive": false }],
    23          ["em", {}],
    24          ["strong", {}],
    25          ["code", {}]
    26        ],
    27        "topNode": "doc"
    28      }
    29    end
    30  
    31    def self.open(inst, id)
    32      opts = {
    33        accept: 'application/vnd.api+json',
    34        authorization: "Bearer #{inst.token_for CozyFile.doctype}"
    35      }
    36      res = inst.client["/notes/#{id}/open"].get opts
    37      JSON.parse(res.body).dig "data", "attributes"
    38    end
    39  
    40    def initialize(opts = {})
    41      @title = opts[:title] || Faker::TvShows::DrWho.quote
    42      @dir_id = opts[:dir_id] || Folder::ROOT_DIR
    43      @schema = opts[:schema] || Note.default_schema
    44    end
    45  
    46    def save(inst)
    47      opts = {
    48        content_type: 'application/vnd.api+json',
    49        authorization: "Bearer #{inst.token_for CozyFile.doctype}"
    50      }
    51      res = inst.client["/notes"].post to_jsonapi, opts
    52      @file = CozyFile.parse_jsonapi(res.body)
    53    end
    54  
    55    def as_json
    56      {
    57        title: @title,
    58        dir_id: @dir_id,
    59        schema: @schema
    60      }
    61    end
    62  
    63    def to_jsonapi
    64      JSON.generate data: { type: Note.doctype, attributes: as_json }
    65    end
    66  end