Class: PinboardApi::Post
- Includes:
- RequestUtils
- Defined in:
- lib/pinboard_api/post.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#extended ⇒ Object
readonly
Returns the value of attribute extended.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .all(options = {}) ⇒ Object
- .create(attributes) ⇒ Object
- .dates(options = {}) ⇒ Object
- .destroy(url) ⇒ Object
- .extract_posts(payload) ⇒ Object
- .find(options = {}) ⇒ Object
- .last_update ⇒ Object
- .recent(options = {}) ⇒ Object
- .suggest(url) ⇒ Object
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(attributes = {}) ⇒ Post
constructor
A new instance of Post.
- #parse_result_code(result) ⇒ Object
- #save(options = {}) ⇒ Object
- #tags ⇒ Object
- #time ⇒ Object
- #validate! ⇒ Object
Methods included from RequestUtils
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
#description ⇒ Object (readonly)
Returns the value of attribute description.
5 6 7 |
# File 'lib/pinboard_api/post.rb', line 5 def description @description end |
#extended ⇒ Object (readonly)
Returns the value of attribute extended.
5 6 7 |
# File 'lib/pinboard_api/post.rb', line 5 def extended @extended end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
5 6 7 |
# File 'lib/pinboard_api/post.rb', line 5 def hash @hash end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
5 6 7 |
# File 'lib/pinboard_api/post.rb', line 5 def @meta end |
#url ⇒ Object (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( = {}) path = "/#{PinboardApi.api_version}/posts/all" params = { tag: tag_param_string([:tag]), start: [:start], results: [:results], fromdt: dt_param_string([:fromdt]), todt: dt_param_string([:todt]), meta: ([: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( = {}) path = "/#{PinboardApi.api_version}/posts/dates" tag = tag_param_string([: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( = {}) path = "/#{PinboardApi.api_version}/posts/get" response = PinboardApi.request(path) do |req| .each_pair { |k,v| req.params[k.to_s] = v } end extract_posts(response.body["posts"]) end |
.last_update ⇒ Object
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( = {}) path = "/#{PinboardApi.api_version}/posts/recent" params = { tag: tag_param_string([:tag]), count: [: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
#destroy ⇒ Object
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( = {}) validate! path = "/#{PinboardApi.api_version}/posts/add" params = { url: @url, description: @description, extended: @extended, tags: Post.tag_param_string(), dt: [:dt], replace: yes_no([:replace]), shared: yes_no([:shared]), toread: yes_no([:toread]) } result = PinboardApi.request(path, params).body["result"] parse_result_code(result) end |
#tags ⇒ Object
23 24 25 |
# File 'lib/pinboard_api/post.rb', line 23 def @tags.is_a?(String) ? @tags.split(/\s+/) : @tags end |
#time ⇒ Object
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 |