Skip to content

Commit

Permalink
Add support for authors and tags in Pocket::Article (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 authored Apr 3, 2021
1 parent 89f5af7 commit d767d7a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Add support for authors and tags in `Pocket::Article` ([#44](https://github.com/turadg/pocket-ruby/pull/44))

## [0.1.0] - 2021-04-03

- Add `Pocket::Article` for parsing an article response ([#39](https://github.com/turadg/pocket-ruby/pull/39))
Expand Down
1 change: 1 addition & 0 deletions lib/pocket-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require File.expand_path("../pocket/api", __FILE__)
require File.expand_path("../pocket/client", __FILE__)
require File.expand_path("../pocket/article", __FILE__)
require File.expand_path("../pocket/author", __FILE__)

module Pocket
extend Configuration
Expand Down
8 changes: 8 additions & 0 deletions lib/pocket/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,13 @@ def read?
def read_url
"https://getpocket.com/read/#{item_id}"
end

def tags
Hash(response["tags"]).values.map { |tag| tag["tag"] }
end

def authors
Hash(response["authors"]).values.map { |value| Pocket::Author.new(value) }
end
end
end
23 changes: 23 additions & 0 deletions lib/pocket/author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Pocket
class Author
def initialize(response)
@response = response
end

def id
Integer(response.fetch("author_id"))
end

def name
response.fetch("name")
end

def url
response.fetch("url")
end

private

attr_reader :response
end
end
20 changes: 19 additions & 1 deletion test/fixtures/retreive.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,23 @@
"type": "1",
"vid": "Er34PbFkVGk"
}
}
},
"tags":{
"bookmark":{
"item_id":"229279689",
"tag":"my-tag-1"
},
"gtd":{
"item_id":"229279689",
"tag":"my-tag-2"
}
},
"authors":{
"62344201":{
"item_id":"229279689",
"author_id":"62344201",
"name":"Stephen King",
"url":"https://example.com/author"
}
}
}
22 changes: 22 additions & 0 deletions test/pocket/article_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ class VersionTest < Test::Unit::TestCase
assert_equal "https://getpocket.com/read/229279689", article.read_url
end

test "tags" do
assert_equal ["my-tag-1", "my-tag-2"], article.tags
end

test "tags returns an empty array if there are no tags" do
parsed_response.delete("tags")
assert_equal [], article.tags
end

test "authors" do
result = article.authors
assert_equal 1, result.size
assert_equal "Stephen King", result.first.name
assert_equal 62344201, result.first.id
assert_equal "https://example.com/author", result.first.url
end

test "authors returns an empty array if there are no tags" do
parsed_response.delete("authors")
assert_equal [], article.authors
end

private

def article
Expand Down

0 comments on commit d767d7a

Please sign in to comment.