FEATURE: Onebox for news.ycombinator.com (#15781)
This commit is contained in:
committed by
GitHub
parent
ea37b30ab2
commit
5b5cbbfe5c
@@ -210,3 +210,4 @@ require_relative "engine/kaltura_onebox"
|
||||
require_relative "engine/reddit_media_onebox"
|
||||
require_relative "engine/google_drive_onebox"
|
||||
require_relative "engine/facebook_media_onebox"
|
||||
require_relative "engine/hackernews_onebox"
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Onebox
|
||||
module Engine
|
||||
class HackernewsOnebox
|
||||
include Engine
|
||||
include LayoutSupport
|
||||
include JSON
|
||||
|
||||
REGEX = /^https?:\/\/news\.ycombinator\.com\/item\?id=(?<item_id>\d+)/
|
||||
|
||||
matches_regexp(REGEX)
|
||||
|
||||
# This is their official API: https://blog.ycombinator.com/hacker-news-api/
|
||||
def url
|
||||
"https://hacker-news.firebaseio.com/v0/item/#{match[:item_id]}.json"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def match
|
||||
@match ||= @url.match(REGEX)
|
||||
end
|
||||
|
||||
def data
|
||||
return nil unless %w{story comment}.include?(raw['type'])
|
||||
|
||||
html_entities = HTMLEntities.new
|
||||
data = {
|
||||
link: @url,
|
||||
title: Onebox::Helpers.truncate(raw['title'], 80),
|
||||
favicon: 'https://news.ycombinator.com/y18.gif',
|
||||
timestamp: Time.at(raw['time']).strftime("%-l:%M %p - %-d %b %Y"),
|
||||
author: raw['by']
|
||||
}
|
||||
|
||||
data['description'] = html_entities.decode(Onebox::Helpers.truncate(raw['text'], 400)) if raw['text']
|
||||
|
||||
if raw['type'] == 'story'
|
||||
data['data_1'] = raw['score']
|
||||
data['data_2'] = raw['descendants']
|
||||
end
|
||||
|
||||
data
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,18 @@
|
||||
<h3><a href="{{link}}" target="_blank" rel="noopener">{{title}}</a></h3>
|
||||
|
||||
{{#description}}
|
||||
<p>{{description}}</p>
|
||||
{{/description}}
|
||||
|
||||
|
||||
<p>
|
||||
{{#data_1}}
|
||||
<span class="label1">{{data_1}} points</span> —
|
||||
{{/data_1}}
|
||||
{{#data_2}}
|
||||
<span class="label2">{{data_2}} comments</span> —
|
||||
{{/data_2}}
|
||||
<a href="https://news.ycombinator.com/user?id={{author}}" class="author" target="_blank" rel="noopener">{{author}}</a> —
|
||||
<a href="{{link}}" class="timestamp" target="_blank" rel="noopener">{{timestamp}}</a>
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user