Class: RaindropIo::Collection

Inherits:
Base
  • Object
show all
Defined in:
lib/raindrop_io/collection.rb

Overview

System collections

Every user have several system non-removable collections. They are not contained in any API responses.

_id Description
-1 "Unsorted" collection
-99 "Trash" collection

Instance Attribute Summary

Attributes inherited from Base

#attributes

Class Method Summary collapse

Methods inherited from Base

delete, get, #initialize, #initialize_attributes, post, put, #to_h, #to_hash, #to_json

Constructor Details

This class inherits a constructor from RaindropIo::Base

Class Method Details

.allObject

Get root collections + system collections GET https://api.raindrop.io/rest/v1/collections



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/raindrop_io/collection.rb', line 14

def all
  response = get("/collections")
  if response.status.success? && response.parse["items"]
    bag1 = response.parse["items"].map { |attributes| Collection.new(attributes) }
    bag2 = get_system_collections
    bag3 = childrens

    bag1 + bag2 + bag3 # combine them all
  else
    RaindropIo::ApiError.new response
  end
end

.childrensObject



50
51
52
53
54
55
56
57
# File 'lib/raindrop_io/collection.rb', line 50

def childrens
  response = get("/collections/childrens")
  if response.status.success? && response.parse["items"]
    response.parse["items"].map { |attributes| Collection.new(attributes) }
  else
    RaindropIo::ApiError.new response
  end
end

.create!(attributes) ⇒ Object

TODO:

Create collection

POST https://api.raindrop.io/rest/v1/collection Create a new collection



86
87
88
89
90
91
92
93
# File 'lib/raindrop_io/collection.rb', line 86

def create!(attributes)
  response = post("/collection", json: attributes)
  if response.status.success? && response.parse["item"]
    Collection.new response.parse["item"]
  else
    RaindropIo::ApiError.new response
  end
end

.find(collection_id) ⇒ Object

Get collection



62
63
64
65
66
67
68
69
# File 'lib/raindrop_io/collection.rb', line 62

def find(collection_id)
  response = get("/collection/#{collection_id}")
  if response.status.success? && response.parse["item"]
    Collection.new response.parse["item"]
  else
    RaindropIo::ApiError.new response
  end
end

.get_system_collectionsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/raindrop_io/collection.rb', line 27

def get_system_collections
  rv = stats
  if rv["result"] == true
    rv["items"].map do |item|
      col = find(item["_id"])
      case item["_id"]
      when -1
        col.title = "Unsorted"
      when -99
        col.title = "Trash"
      else
        col.title = "system collection #{item["_id"]}" if col.title.nil?
      end
      col
    end
  else
    RaindropIo::ApiError.new response
  end
end

.remove(collection_id) ⇒ Object

DELETE https://api.raindrop.io/rest/v1/collection/id Remove an existing collection and all its descendants.



97
98
99
100
101
102
103
104
# File 'lib/raindrop_io/collection.rb', line 97

def remove(collection_id)
  response = delete("/collection/#{collection_id}")
  if response.status.success?
    response.parse
  else
    RaindropIo::ApiError.new response
  end
end

.statsObject

Get system collections count

GET https://api.raindrop.io/rest/v1/user/stats



74
75
76
77
78
79
80
81
# File 'lib/raindrop_io/collection.rb', line 74

def stats
  response = get("/user/stats")
  if response.status.success?
    response.parse
  else
    RaindropIo::ApiError.new response
  end
end