Skip to content

Commit 10e0639

Browse files
authored
Merge pull request #77 from twingly/to_h
Add Post#to_h for both LiveFeed and Search
2 parents 1958947 + 3cdc9ae commit 10e0639

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

lib/twingly/livefeed/post.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,32 @@ def set_values(params)
6161
@blog_rank = params.fetch('blogRank').to_i
6262
@authority = params.fetch('authority').to_i
6363
end
64+
65+
# @return [Hash] a hash containing all post attributes.
66+
def to_h
67+
{
68+
id: @id,
69+
author: @author,
70+
url: @url,
71+
title: @title,
72+
text: @text,
73+
language_code: @language_code,
74+
location_code: @location_code,
75+
coordinates: @coordinates,
76+
links: @links,
77+
tags: @tags,
78+
images: @images,
79+
indexed_at: @indexed_at,
80+
published_at: @published_at,
81+
reindexed_at: @reindexed_at,
82+
inlinks_count: @inlinks_count,
83+
blog_id: @blog_id,
84+
blog_name: @blog_name,
85+
blog_url: @blog_url,
86+
blog_rank: @blog_rank,
87+
authority: @authority,
88+
}
89+
end
6490
end
6591
end
6692
end

lib/twingly/search/post.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ def outlinks
8484
warn "[DEPRECATION] `outlinks` is deprecated. Please use `links` instead."
8585
links
8686
end
87+
88+
# @return [Hash] a hash containing all post attributes.
89+
def to_h
90+
{
91+
id: @id,
92+
author: @author,
93+
url: @url,
94+
title: @title,
95+
text: @text,
96+
language_code: @language_code,
97+
location_code: @location_code,
98+
coordinates: @coordinates,
99+
links: @links,
100+
tags: @tags,
101+
images: @images,
102+
indexed_at: @indexed_at,
103+
published_at: @published_at,
104+
reindexed_at: @reindexed_at,
105+
inlinks_count: @inlinks_count,
106+
blog_id: @blog_id,
107+
blog_name: @blog_name,
108+
blog_url: @blog_url,
109+
blog_rank: @blog_rank,
110+
authority: @authority,
111+
}
112+
end
87113
end
88114
end
89115
end

spec/livefeed/post_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,51 @@ module Twingly::LiveFeed
2323
it { should respond_to :blog_rank }
2424
it { should respond_to :authority }
2525
end
26+
27+
describe "#to_h" do
28+
let(:post) do
29+
document = Fixture.livefeed_get(:valid)
30+
result = Parser.new.parse(document)
31+
32+
result.posts.first
33+
end
34+
35+
let(:post_hash) do
36+
post.to_h
37+
end
38+
39+
attributes = %i[
40+
id
41+
author
42+
url
43+
title
44+
text
45+
location_code
46+
language_code
47+
coordinates
48+
links
49+
tags
50+
images
51+
indexed_at
52+
published_at
53+
reindexed_at
54+
inlinks_count
55+
blog_id
56+
blog_name
57+
blog_url
58+
blog_rank
59+
authority
60+
]
61+
62+
it do
63+
expect(post_hash.keys).to match_array(attributes)
64+
end
65+
66+
attributes.each do |key|
67+
describe "#to_h[:#{key}]" do
68+
subject { post_hash[key] }
69+
it { is_expected.to eq(post.public_send(key)) }
70+
end
71+
end
72+
end
2673
end

spec/post_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
it { should respond_to :blog_url }
2424
it { should respond_to :blog_rank }
2525
it { should respond_to :authority }
26+
it { should respond_to :to_h }
2627

2728
deprecated_methods = %i(summary indexed published outlinks)
2829
deprecated_methods.each do |method_name|
@@ -33,4 +34,51 @@
3334
end
3435
end
3536
end
37+
38+
describe "#to_h" do
39+
let(:post) do
40+
document = Fixture.get(:minimal_valid)
41+
result = Parser.new.parse(document)
42+
43+
result.posts.first
44+
end
45+
46+
let(:post_hash) do
47+
post.to_h
48+
end
49+
50+
attributes = %i[
51+
id
52+
author
53+
url
54+
title
55+
text
56+
location_code
57+
language_code
58+
coordinates
59+
links
60+
tags
61+
images
62+
indexed_at
63+
published_at
64+
reindexed_at
65+
inlinks_count
66+
blog_id
67+
blog_name
68+
blog_url
69+
blog_rank
70+
authority
71+
]
72+
73+
it do
74+
expect(post_hash.keys).to match_array(attributes)
75+
end
76+
77+
attributes.each do |key|
78+
describe "#to_h[:#{key}]" do
79+
subject { post_hash[key] }
80+
it { is_expected.to eq(post.public_send(key)) }
81+
end
82+
end
83+
end
3684
end

0 commit comments

Comments
 (0)