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

     1  class Album
     2    include Model
     3  
     4    attr_reader :name, :created_at
     5  
     6    def self.doctype
     7      "io.cozy.photos.albums"
     8    end
     9  
    10    def initialize(opts = {})
    11      @name = opts[:name] || Faker::TvShows::DrWho.quote
    12      @created_at = opts[:created_at] || Date.today
    13    end
    14  
    15    def add(inst, photo)
    16      opts = {
    17        accept: "application/vnd.api+json",
    18        authorization: "Bearer #{inst.token_for [doctype, photo.doctype]}",
    19        :"content-type" => "application/vnd.api+json"
    20      }
    21      body = JSON.generate data: [{ type: doctype, id: @couch_id }]
    22      inst.client["/files/#{photo.couch_id}/relationships/referenced_by"].post body, opts
    23    end
    24  
    25    def remove_photo(inst, photo)
    26      opts = {
    27        accept: "application/vnd.api+json",
    28        authorization: "Bearer #{inst.token_for [doctype, photo.doctype]}",
    29        :"content-type" => "application/vnd.api+json"
    30      }
    31      body = JSON.generate data: [{ type: doctype, id: @couch_id }]
    32      url = "#{inst.domain}/files/#{photo.couch_id}/relationships/referenced_by"
    33      RestClient::Request.execute(method: :delete, url: url, payload: body, headers: opts)
    34    end
    35  
    36    def self.find(inst, id)
    37      opts = {
    38        accept: :json,
    39        authorization: "Bearer #{inst.token_for doctype}"
    40      }
    41      res = inst.client["/data/#{doctype}/#{id}"].get opts
    42      j = JSON.parse(res.body)
    43      album = Album.new(
    44        name: j["name"],
    45        created_at: j["created_at"]
    46      )
    47      album.couch_id = j["_id"]
    48      album.couch_rev = j["_rev"]
    49      album
    50    end
    51  
    52    def as_json
    53      {
    54        name: @name,
    55        created_at: @created_at.rfc3339
    56      }
    57    end
    58  end