hack3rnews 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82e9fe3f6cc5f6fa373cae0c63a4058114bb417de0d91a0c55fad23fb1de445a
4
- data.tar.gz: 4e696613d05b3b9682e131d4a0b1f494549ba2a0a7c89be06301b6c3ff063ec6
3
+ metadata.gz: 70c46de84b63ecf71765ff736548f9000fef25183e42dbe0e80fa1388d0bc12b
4
+ data.tar.gz: 11c449b1017188a3a9670c2bf3013294b5c421d0c0e3499b16ba176ef9faa8f6
5
5
  SHA512:
6
- metadata.gz: f528de6bcafaaff9da60e90d2ce4392af3446ca819942d24f7b62d5a8cd1fc8f832b44f65f7f42bbb17c7fc921bff2ae880535fd92262957e187a476eb5217c9
7
- data.tar.gz: 8458a45c5e0dbf386da32bc42aa8ced08b8397a106467002cef97e6354230de05cccbcdfc9fc9031a0e2c4c912655c30c49820d0ab632ff054850ce9dc1f95e5
6
+ metadata.gz: 5eaecfdbaf0962f968cd69a8d61b1782977f8d980e78bf33ec8a3b037fe2cbc27d0f363b24740184be242609f1a72a2c4657fbb2f0cf724c0bc493d3afa8e3ba
7
+ data.tar.gz: 5cf662da4570b9bccbc5de5fcd7da324a16def2c9bb100b4371909c41df62f12b7cd58e986df124dbd3b1bd65c6166f74fdef3f0b47413643af66b2fce81a95f
data/README.md CHANGED
@@ -9,3 +9,31 @@ Information for the API: [HackerNewsAPI](https://github.com/HackerNews/API)
9
9
  * Get the last 10 stories for a specific user (you will need to know his/her username)
10
10
  * Get all the comments of a specific story
11
11
  * Get the last 20 hihghest-rated score job stories
12
+
13
+ ## How to use
14
+
15
+ ### Installation
16
+ First be sure to install the gem: `gem install hack3rnews`
17
+
18
+ Once installed you have to require the gem in order to use it.
19
+ At the top of your ruby file where you need to use the gem write: `require 'hack3rnews'`
20
+
21
+ ### Avalilable methods
22
+
23
+ - Newest stories:
24
+ - Returns the newest stories, the last 5 by default. This method accepts an argument to specify as many as you need (based on availavility):
25
+ - example: `newest_stories = HackerNew.new_stories(40)`
26
+ - Top stories:
27
+ - Returns the highest rated stories, the last 5 by default. It also accepts an argument to specify how many stories you need.
28
+ - example: `top_stories = HackerNew.top_stories(25)`
29
+ - User stories:
30
+ - Returns the stories written by an specified user, the last 5 by default. It also accepts an argument to specify the amount that you need.
31
+ - example: `user_stories = HackerNew.top_stories(17)`
32
+ - Story comments:
33
+ - Returns all the comments of a specified story.
34
+ - example: `story_comments = HackerNew.story_comments(story_id)`
35
+ - Top job stories:
36
+ - Returns the highest rated job stories, the last 5 by default. The method also accepts an argument to ask for certain amount of stories.
37
+ - example: `top_job_stories = HackerNew.top_job_stories(33)`
38
+
39
+
data/lib/hack3rnews.rb CHANGED
@@ -1,76 +1,60 @@
1
- require 'rest-client'
2
- require 'json'
1
+ require_relative 'hack3rnews/request'
3
2
 
4
3
  class HackerNew
5
4
  URL = 'https://hacker-news.firebaseio.com/v0/'.freeze
6
5
  JSON_FORMAT = '/.json?print=pretty'.freeze
7
6
 
7
+ @@request = Request.new
8
+
8
9
  class << self
9
10
  def new_stories(num = 5)
10
- capture_stories('newstories', num)
11
+ stories('newstories', num)
11
12
  end
12
13
 
13
14
  def top_stories(num = 5)
14
- capture_stories('topstories', num)
15
+ stories('topstories', num)
15
16
  end
16
17
 
17
- def user_stories(user, num = 5)
18
- capture_item_kids('user', num: num, item_id: user, kind: 'submitted')
18
+ def user_stories(user_id, num = 5)
19
+ user = item('user', user_id)
20
+ items(user['submitted'].first(num) || [])
19
21
  end
20
22
 
21
- def story_comments(story)
22
- capture_item_kids('item', item_id: story, kind: 'kids')
23
+ def story_comments(story_id)
24
+ story = item('item', story_id)
25
+ items(story['kids'] || [])
23
26
  end
24
27
 
25
28
  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
29
+ top_items(stories('jobstories'), 'score').last(num)
32
30
  end
33
31
 
34
32
  private
35
- def capture_item_kids(type, opt = {})
36
- item = request_item(type, opt[:item_id])
37
- kid_ids = capture_ids(item, opt)
38
- capture_items(kid_ids)
39
- end
40
-
41
- def capture_ids(item, opt)
42
- num = opt[:num]
43
- return (item[opt[:kind]].first(num) || [nil]) if num
44
- item[opt[:kind]] || [nil]
33
+ def stories(type, num = nil)
34
+ story_ids = request.item(url(type))
35
+ story_ids = story_ids.first(num) if num
36
+ items(story_ids)
45
37
  end
46
38
 
47
- def capture_stories(type, num)
48
- story_ids = request_bunch(type, num)
49
- capture_items(story_ids)
39
+ def top_items(items, field)
40
+ items.sort_by! { |hsh| hsh[field] }.reverse
50
41
  end
51
42
 
52
- def capture_items(item_ids)
53
- items = []
54
- while item_id = item_ids.shift
55
- items << request_item('item', item_id)
56
- end
57
- items
43
+ def items(item_ids)
44
+ item_ids.map {|item_id| item('item', item_id)}
58
45
  end
59
46
 
60
- def request(url)
61
- response = RestClient.get(url)
62
- JSON.parse(response)
47
+ def item(type, item_id)
48
+ request.item(url(type, item_id))
63
49
  end
64
50
 
65
- def request_item(type, id)
66
- url = URL + type + '/' + id.to_s + JSON_FORMAT
67
- request(url)
51
+ def url(type, id = nil)
52
+ return URL + type + '/' + id.to_s + JSON_FORMAT if id
53
+ URL + type + JSON_FORMAT
68
54
  end
69
55
 
70
- def request_bunch(type, num = nil)
71
- url = URL + type + JSON_FORMAT
72
- response = request(url)
73
- num ? response.first(num) : response
56
+ def request
57
+ @@request
74
58
  end
75
59
  end
76
60
  end
@@ -0,0 +1,9 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ class Request
5
+ def item(url)
6
+ response = RestClient.get(url)
7
+ JSON.parse(response)
8
+ end
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hack3rnews
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alvaro Padilla
@@ -46,8 +46,9 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - README.md
48
48
  - lib/hack3rnews.rb
49
+ - lib/hack3rnews/request.rb
49
50
  - spec/hack3rnews_spec.rb
50
- homepage: https://github.com/ese-varo
51
+ homepage: https://github.com/GregVaz/hackernews-gem
51
52
  licenses:
52
53
  - MIT
53
54
  metadata: {}