Skip to content

Commit

Permalink
Add Pocket::Article for parsing an article response
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Apr 3, 2021
1 parent 3da737a commit 79cb460
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [Unreleased]

- Add `Pocket::Article` for parsing an article response ([#39](https://github.com/turadg/pocket-ruby/pull/39))
- Fix Pocket::Client::Retrieve#retrieve params default ([#21](https://github.com/turadg/pocket-ruby/pull/21))

## [0.0.9] - 2021-04-01
Expand Down
1 change: 1 addition & 0 deletions lib/pocket-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require File.expand_path("../pocket/configuration", __FILE__)
require File.expand_path("../pocket/api", __FILE__)
require File.expand_path("../pocket/client", __FILE__)
require File.expand_path("../pocket/article", __FILE__)

module Pocket
extend Configuration
Expand Down
111 changes: 111 additions & 0 deletions lib/pocket/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
require "json"

module Pocket
class Article
attr_reader :response

def initialize(response)
@response = response
end

def self.from_json_string(json_string)
new(JSON.parse(json_string))
end

def item_id
Integer(response.fetch("item_id"))
end

def given_url
response.fetch("given_url")
end

def resolved_url
response.fetch("resolved_url")
end

def given_title
response.fetch("given_title")
end

def resolved_title
response.fetch("resolved_title")
end

def favorite?
Integer(response.fetch("favorite")) == 1
end

def status
Integer(response.fetch("status"))
end

def excerpt
response.fetch("excerpt")
end

def article?
Integer(response.fetch("is_article")) == 1
end

def has_image?
Integer(response.fetch("has_image")) == 1
end

def image?
Integer(response.fetch("has_image")) == 2
end

def has_video?
Integer(response.fetch("has_video")) == 1
end

def video?
Integer(response.fetch("has_video")) == 2
end

def word_count
Integer(response.fetch("word_count"))
end

def resolved_id
Integer(response.fetch("resolved_id"))
end

def thumbnail
response.fetch("top_image_url", "")
end

def time_added
return nil unless response["time_added"]
Time.at(Integer(response["time_added"]))
end

def time_updated
return nil unless response["time_updated"]
Time.at(Integer(response["time_updated"]))
end

def time_read
return nil unless response["time_read"]
Time.at(Integer(response["time_read"]))
end

def favorited?
Integer(response["time_favorited"]) > 0
end

def time_favorited
return nil unless response["time_favorited"]
Time.at(Integer(response["time_favorited"]))
end

def read?
Integer(response["time_read"]) > 0
end

def read_url
"https://getpocket.com/read/#{item_id}"
end
end
end
42 changes: 42 additions & 0 deletions test/fixtures/retreive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"item_id": "229279689",
"resolved_id": "229279689",
"given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview?given",
"given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
"favorite": "0",
"status": "0",
"resolved_title": "The Massive Ryder Cup Preview",
"resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview?resolved",
"excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
"is_article": "1",
"has_video": "1",
"has_image": "1",
"word_count": "3197",
"top_image_url": "https://example.com/image.png",
"time_added": "1617412992",
"time_updated": "1617412993",
"time_read": "1617412994",
"time_favorited": "1617412995",
"images": {
"1": {
"item_id": "229279689",
"image_id": "1",
"src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
"width": "0",
"height": "0",
"credit": "Jamie Squire/Getty Images",
"caption": ""
}
},
"videos": {
"1": {
"item_id": "229279689",
"video_id": "1",
"src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
"width": "420",
"height": "315",
"type": "1",
"vid": "Er34PbFkVGk"
}
}
}
133 changes: 133 additions & 0 deletions test/pocket/article_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
require "test_helper"

module Pocket
class VersionTest < Test::Unit::TestCase
test "from_json_string" do
json_string = File.read("test/fixtures/retreive.json")
article = Pocket::Article.from_json_string(json_string)
assert_equal 229279689, article.item_id
end

test "item_id" do
assert_equal 229279689, article.item_id
end

test "given_url" do
assert_equal "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview?given", article.given_url
end

test "resolved_url" do
assert_equal "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview?resolved", article.resolved_url
end

test "given_title" do
assert_equal "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", article.given_title
end

test "resolved_title" do
assert_equal "The Massive Ryder Cup Preview", article.resolved_title
end

test "favorite? is false is field is '0'" do
assert_equal false, article.favorite?
end

test "status" do
assert_equal 0, article.status
end

test "excerpt" do
assert_include article.excerpt, "list of things"
end

test "article?" do
assert article.article?
end

test "has_image?" do
assert article.has_image?
end

test "image?" do
refute article.image?
end

test "has_video?" do
assert article.has_video?
end

test "video?" do
refute article.video?
end

test "word_count" do
assert_equal 3197, article.word_count
end

test "resolved_id" do
assert_equal 229279689, article.resolved_id
end

test "thumbnail" do
assert_equal "https://example.com/image.png", article.thumbnail
end

test "time_added" do
assert_equal Time.utc(2021, 4, 2, 21, 23, 12), article.time_added
end

test "time_added is nil if field not present" do
parsed_response.delete("time_added")
assert_nil article.time_added
end

test "time_updated" do
assert_equal Time.utc(2021, 4, 2, 21, 23, 13), article.time_updated
end

test "time_updated is nil if field not present" do
parsed_response.delete("time_updated")
assert_nil article.time_updated
end

test "time_read" do
assert_equal Time.utc(2021, 4, 2, 21, 23, 14), article.time_read
end

test "time_read is nil if field is not present" do
parsed_response.delete("time_read")
assert_nil article.time_read
end

test "favorited?" do
assert article.favorited?
end

test "time_favorited" do
assert_equal Time.utc(2021, 4, 2, 21, 23, 15), article.time_favorited
end

test "time_favorited is nil if field not present" do
parsed_response.delete("time_favorited")
assert_nil article.time_favorited
end

test "read?" do
assert article.read?
end

test "read_url" do
assert_equal "https://getpocket.com/read/229279689", article.read_url
end

private

def article
@article ||= Pocket::Article.new(parsed_response)
end

def parsed_response
@parsed_response ||= JSON.parse(File.read("test/fixtures/retreive.json"))
end
end
end

0 comments on commit 79cb460

Please sign in to comment.