Class: PinboardApi::Post

Inherits:
Object show all
Includes:
RequestUtils
Defined in:
lib/pinboard_api/post.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestUtils

included, #yes_no

Constructor Details

#initialize(attributes = {}) ⇒ Post

Returns a new instance of Post.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pinboard_api/post.rb', line 7

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

  @description = attributes["description"]
  @extended    = attributes["extended"]
  @hash        = attributes["hash"]
  @meta        = attributes["meta"]
  @url         = attributes["url"] || attributes["href"]
  @tags        = attributes["tags"] || attributes["tag"]
  @time        = attributes["time"] || Time.now
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/pinboard_api/post.rb', line 5

def description
  @description
end

#extendedObject (readonly)

Returns the value of attribute extended.



5
6
7
# File 'lib/pinboard_api/post.rb', line 5

def extended
  @extended
end

#hashObject (readonly)

Returns the value of attribute hash.



5
6
7
# File 'lib/pinboard_api/post.rb', line 5

def hash
  @hash
end

#metaObject (readonly)

Returns the value of attribute meta.



5
6
7
# File 'lib/pinboard_api/post.rb', line 5

def meta
  @meta
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/pinboard_api/post.rb', line 5

def url
  @url
end

Class Method Details

.all(options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pinboard_api/post.rb', line 80

def self.all(options = {})
  path = "/#{PinboardApi.api_version}/posts/all"
  params = {
    tag:     tag_param_string(options[:tag]),
    start:   options[:start],
    results: options[:results],
    fromdt:  dt_param_string(options[:fromdt]),
    todt:    dt_param_string(options[:todt]),
    meta:    (options[:meta] ? 1 : 0)
  }

  response = PinboardApi.request(path, params)
  extract_posts(response.body["posts"])
end

.create(attributes) ⇒ Object



68
69
70
# File 'lib/pinboard_api/post.rb', line 68

def self.create(attributes)
  new(attributes).save
end

.dates(options = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/pinboard_api/post.rb', line 122

def self.dates(options = {})
  path = "/#{PinboardApi.api_version}/posts/dates"
  tag = tag_param_string(options[:tag])
  response = PinboardApi.request(path, tag: tag)

  dates = response.body["dates"]["date"]
  dates.map do |date|
    { "count" => date["count"].to_i, "date" =>  Date.parse(date["date"]) }
  end
end

.destroy(url) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/pinboard_api/post.rb', line 72

def self.destroy(url)
  if post = find(url: url).first
    post.destroy
  else
    raise InvalidResponseError, "unknown response"
  end
end

.extract_posts(payload) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pinboard_api/post.rb', line 134

def self.extract_posts(payload)
  unless payload.respond_to?(:keys) && payload.keys.include?("post")
    return Array.new
  end

  # response.body["posts"] - "429 Too Many Requests.  Wait 60 seconds before fetching posts/all again."

  payload.inject([]) do |collection, (key, attrs)|
    if key == "post"
      Array.wrap(attrs).each do |post|
        Array.wrap(collection) << new(post)
      end
    end
    collection
  end
end

.find(options = {}) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/pinboard_api/post.rb', line 95

def self.find(options = {})
  path = "/#{PinboardApi.api_version}/posts/get"
  response = PinboardApi.request(path) do |req|
    options.each_pair { |k,v| req.params[k.to_s] = v }
  end
  extract_posts(response.body["posts"])
end

.last_updateObject



103
104
105
106
107
# File 'lib/pinboard_api/post.rb', line 103

def self.last_update
  path = "/#{PinboardApi.api_version}/posts/update"
  body = PinboardApi.request(path).body
  Time.parse(body["update"]["time"])
end

.recent(options = {}) ⇒ Object



115
116
117
118
119
120
# File 'lib/pinboard_api/post.rb', line 115

def self.recent(options = {})
  path = "/#{PinboardApi.api_version}/posts/recent"
  params = { tag: tag_param_string(options[:tag]), count: options[:count] }
  response = PinboardApi.request(path, params)
  extract_posts(response.body["posts"])
end

.suggest(url) ⇒ Object



109
110
111
112
113
# File 'lib/pinboard_api/post.rb', line 109

def self.suggest(url)
  path = "/#{PinboardApi.api_version}/posts/suggest"
  response = PinboardApi.request(path, url: url)
  response.body["suggested"]
end

Instance Method Details

#destroyObject



45
46
47
48
49
# File 'lib/pinboard_api/post.rb', line 45

def destroy
  path = "/#{PinboardApi.api_version}/posts/delete"
  result = PinboardApi.request(path, url: @url).body["result"]
  parse_result_code(result)
end

#parse_result_code(result) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/pinboard_api/post.rb', line 60

def parse_result_code(result)
  unless result && code = result.fetch("code", false)
    raise InvalidResponseError, "unknown response"
  end

  code == "done" ? self : raise(InvalidResponseError, code.to_s)
end

#save(options = {}) ⇒ Object



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

def save(options = {})
  validate!
  path = "/#{PinboardApi.api_version}/posts/add"
  params = {
    url:         @url,
    description: @description,
    extended:    @extended,
    tags:        Post.tag_param_string(tags),
    dt:          options[:dt],
    replace:     yes_no(options[:replace]),
    shared:      yes_no(options[:shared]),
    toread:      yes_no(options[:toread])
  }

  result = PinboardApi.request(path, params).body["result"]
  parse_result_code(result)
end

#tagsObject



23
24
25
# File 'lib/pinboard_api/post.rb', line 23

def tags
  @tags.is_a?(String) ? @tags.split(/\s+/) : @tags
end

#timeObject



19
20
21
# File 'lib/pinboard_api/post.rb', line 19

def time
  @time.is_a?(String) ? Time.parse(@time) : @time.to_time
end

#validate!Object



51
52
53
54
55
56
57
58
# File 'lib/pinboard_api/post.rb', line 51

def validate!
  if @url.blank?
    raise InvalidPostError, "url cannot be blank"
  end
  if @description.blank?
    raise InvalidPostError, "description cannot be blank"
  end
end