Class: PinboardApi::Tag

Inherits:
Object show all
Defined in:
lib/pinboard_api/tag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Tag

Returns a new instance of Tag.



5
6
7
8
9
10
# File 'lib/pinboard_api/tag.rb', line 5

def initialize(attributes = {})
  attributes.stringify_keys!

  @name  = attributes["name"] || attributes["tag"]
  @count = attributes["count"].to_i
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



3
4
5
# File 'lib/pinboard_api/tag.rb', line 3

def count
  @count
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/pinboard_api/tag.rb', line 3

def name
  @name
end

Class Method Details

.allObject



38
39
40
41
42
43
44
# File 'lib/pinboard_api/tag.rb', line 38

def self.all
  path = "/#{PinboardApi.api_version}/tags/get"
  body = PinboardApi.request(path).body
  body["tags"]["tag"].map { |tag| new(tag) }
rescue
  raise InvalidResponseError, "unknown response"
end

.destroy(tag) ⇒ Object



50
51
52
# File 'lib/pinboard_api/tag.rb', line 50

def self.destroy(tag)
  find(tag).destroy
end

.find(name) ⇒ Object



46
47
48
# File 'lib/pinboard_api/tag.rb', line 46

def self.find(name)
  all.detect { |t| t.name == name }
end

Instance Method Details

#destroyObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/pinboard_api/tag.rb', line 27

def destroy
  path = "/#{PinboardApi.api_version}/tags/delete"
  result = PinboardApi.request(path, tag: @name).body["result"]

  if result == "done"
    self
  else
    raise InvalidResponseError, result.to_s
  end
end

#rename(new_name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pinboard_api/tag.rb', line 12

def rename(new_name)
  path = "/#{PinboardApi.api_version}/tags/rename"
  response = PinboardApi.request(path) do |req|
    req.params["old"] = @name
    req.params["new"] = new_name.to_s
  end
  result = response.body["result"]

  if result == "done"
    Tag.new(name: new_name, count: @count)
  else
    raise InvalidResponseError, result.to_s
  end
end