hack3rnews 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +11 -0
- data/lib/hack3rnews.rb +76 -0
- data/spec/hack3rnews_spec.rb +52 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5dc1c348fab112a324ae441e4310a69484572aa3a8500fe3bbef7075e713b331
|
4
|
+
data.tar.gz: daca0d8d89d6548ee506688168f2fd60eb76e14a98abe226712e41e673e8a104
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 38fe1eafd2327457ffcd5d6637a779c9efc6c47445d478e688f4b034c0c3340046a36236b0968eafab2b4b5917dd9da2211ca685b335a2e9e8ba23f75c414087
|
7
|
+
data.tar.gz: 9684b38fd27da616932b4c7fa8777c9e37c5264d4540c80840bdd8ca4fe1335c468bb9b802f305ac7d64abcbb40d92da691d5cc01797913ee95589171911b237
|
data/README.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# Hackernews-gem
|
2
|
+
Ruby gem for consume Hackernews API
|
3
|
+
|
4
|
+
Information for the API: [HackerNewsAPI](https://github.com/HackerNews/API)
|
5
|
+
|
6
|
+
## Requirements:
|
7
|
+
* Get the last 20 new stories with all the information
|
8
|
+
* Get the last 20 top stories with all the information
|
9
|
+
* Get the last 10 stories for a specific user (you will need to know his/her username)
|
10
|
+
* Get all the comments of a specific story
|
11
|
+
* Get the last 20 hihghest-rated score job stories
|
data/lib/hack3rnews.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class HackerNew
|
5
|
+
URL = 'https://hacker-news.firebaseio.com/v0/'.freeze
|
6
|
+
JSON_FORMAT = '/.json?print=pretty'.freeze
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def new_stories(num = 5)
|
10
|
+
capture_stories('newstories', num)
|
11
|
+
end
|
12
|
+
|
13
|
+
def top_stories(num = 5)
|
14
|
+
capture_stories('topstories', num)
|
15
|
+
end
|
16
|
+
|
17
|
+
def user_stories(user, num = 5)
|
18
|
+
capture_item_kids('user', num: num, item_id: user, kind: 'submitted')
|
19
|
+
end
|
20
|
+
|
21
|
+
def story_comments(story)
|
22
|
+
capture_item_kids('item', item_id: story, kind: 'kids')
|
23
|
+
end
|
24
|
+
|
25
|
+
def top_job_stories(num = 5)
|
26
|
+
job_scores = []
|
27
|
+
request_bunch('jobstories').each do |job_id|
|
28
|
+
job_scores << request_item('item', job_id)
|
29
|
+
end
|
30
|
+
job_scores.sort_by! { |hsh| hsh["score"] }
|
31
|
+
job_scores.last(num).reverse
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def capture_item_kids(type, opt = {})
|
36
|
+
kids = []
|
37
|
+
item = request_item(type, opt[:item_id])
|
38
|
+
kid_ids = capture_ids(item, opt)
|
39
|
+
while kid_id = kid_ids.shift
|
40
|
+
kids << request_item('item', kid_id)
|
41
|
+
end
|
42
|
+
kids
|
43
|
+
end
|
44
|
+
|
45
|
+
def capture_ids(item, opt)
|
46
|
+
num = opt[:num]
|
47
|
+
return (item[opt[:kind]].first(num) || [nil]) if num
|
48
|
+
item[opt[:kind]] || [nil]
|
49
|
+
end
|
50
|
+
|
51
|
+
def capture_stories(type, num)
|
52
|
+
stories = []
|
53
|
+
storie_ids = request_bunch(type, num)
|
54
|
+
while story_id = storie_ids.shift
|
55
|
+
stories << request_item('item', story_id)
|
56
|
+
end
|
57
|
+
stories
|
58
|
+
end
|
59
|
+
|
60
|
+
def request(url)
|
61
|
+
response = RestClient.get(url)
|
62
|
+
JSON.parse(response)
|
63
|
+
end
|
64
|
+
|
65
|
+
def request_item(type, id)
|
66
|
+
url = URL + type + '/' + id.to_s + JSON_FORMAT
|
67
|
+
request(url)
|
68
|
+
end
|
69
|
+
|
70
|
+
def request_bunch(type, num = nil)
|
71
|
+
url = URL + type + JSON_FORMAT
|
72
|
+
response = request(url)
|
73
|
+
num ? response.first(num) : response
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative '../lib/hack3rnews'
|
2
|
+
|
3
|
+
RSpec.describe HackerNew do
|
4
|
+
describe '.new_stories' do
|
5
|
+
it 'returns the 5 newest sotries by default' do
|
6
|
+
expect(HackerNew.new_stories.count).to eq(5)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'returns just the 2 newest stories' do
|
10
|
+
expect(HackerNew.new_stories(2).count).to eq(2)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '.top_stories' do
|
15
|
+
it 'returns the top 5 stories of the moment' do
|
16
|
+
expect(HackerNew.top_stories.count).to eq(5)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'returns the top 3 stories of the moment' do
|
20
|
+
expect(HackerNew.top_stories(3).count).to eq(3)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '.user_stories' do
|
25
|
+
user = 'stanislavb'
|
26
|
+
it 'returns the last 5 stories of a user' do
|
27
|
+
expect(HackerNew.user_stories(user).count).to eq(5)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns the last 3 stories of a user' do
|
31
|
+
expect(HackerNew.user_stories(user, 3).count).to eq(3)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.story_comments' do
|
36
|
+
it 'returns all the comments of a given story' do
|
37
|
+
story = 26096340
|
38
|
+
|
39
|
+
expect(HackerNew.story_comments(story).count).to be > 10
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '.top_job_stories' do
|
44
|
+
it 'returns the 5 most ranked job stories' do
|
45
|
+
expect(HackerNew.top_job_stories.count).to eq(5)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'returns the 4 most ranked job stories' do
|
49
|
+
expect(HackerNew.top_job_stories(4).count).to eq(4)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hack3rnews
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alvaro Padilla
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-02-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
description: Consume heckernews content through the use of their API
|
42
|
+
email: varoboss.ap@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- README.md
|
48
|
+
- lib/hack3rnews.rb
|
49
|
+
- spec/hack3rnews_spec.rb
|
50
|
+
homepage: https://github.com/ese-varo
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.2.3
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Consume hackernews content (stories, comments, etc)
|
73
|
+
test_files: []
|