hn_api 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
  SHA1:
3
- metadata.gz: 5025908a7656a4cfda10a23815666e2712251751
4
- data.tar.gz: 1586072fdea51f0301be0e27dc840e725a589de4
3
+ metadata.gz: 81896995b35ec40bc6a7751c4d029560157c1c70
4
+ data.tar.gz: 73e4cbfa37c225ec582bcfd987528d810fa4248f
5
5
  SHA512:
6
- metadata.gz: 8f91f82695b28dae1492545cbfca6ed5360f306ec357f9a6ce7eaccf0121e0f8c4a9895b1faf84a43cf3137930012446cd074ea10a3248c2cd70f2feb69ceebd
7
- data.tar.gz: 2e33ab0387af1851cbfc36f6394e73758821d940366a82ebcea6e4fe4b6379c64b9bc26343228e8032a4e62190774dddaf8a3fb664002ac4419c8eea2e93e7cf
6
+ metadata.gz: d900e14c6d5996e0add6ff4fa6d5ffae3f2a92238ab63a00aee6336abd9c834b34868f833a605f91d8517d4ba8abc1ddc0ac09a771f9d469192862ec0e0d2d6e
7
+ data.tar.gz: b38ee0e581accc65dcb26de738a1217a98426c86733c1a613b93690bbe385bb4f2d36b4224da1a6743ef7dfc51f889434ac63e2f0889ed0941f4fd35bec343ee
data/README.md CHANGED
@@ -1,8 +1,90 @@
1
1
  # hn_api
2
+ [![Build Status](https://travis-ci.org/O-I/guardian_api.svg?branch=master)](https://travis-ci.org/O-I/guardian_api)
3
+ [![Coverage Status](https://img.shields.io/coveralls/O-I/hn_api.svg)](https://coveralls.io/r/O-I/hn_api?branch=master)
2
4
 
3
- A Ruby wrapper for [the Hacker News API](https://github.com/HackerNews/API). Currently very alpha.
5
+ A Ruby wrapper for the [Hacker News API](https://github.com/HackerNews/API).
4
6
 
5
- # Contributing to hn_api
7
+ ## Installation
8
+
9
+ `gem install hn_api` or add `gem 'hn_api'` to your `Gemfile` and `bundle`.
10
+
11
+ ## Configuration
12
+
13
+ Currently, the Hacker News API is read-only and does not require an API key. The API is based at https://hacker-news.firebaseio.com, and is currently on version `v0`.
14
+
15
+ ```ruby
16
+ # For now, this one line is all you need to configure
17
+ @client = HN::Client.new
18
+
19
+ # In the event of a version change, you can set the `api_url` like so
20
+ @client.configure do |config|
21
+ config.api_url = 'https://hacker-news.firebaseio.com/v1/'
22
+ end
23
+
24
+ # And you can always reset to the defaults
25
+ @client.reset
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ Descriptions and examples of the supported actions are below. For a more detailed explanation of available endpoints and an exhaustive list of the properties each response returns, check out the official [Hacker News API documentation](https://github.com/HackerNews/API).
31
+
32
+ ### Items [GET /v0/item/#{id}.json](https://github.com/HackerNews/API#items)
33
+
34
+ Fetches an item (story, comment, poll, etc.) by id. Returns a `Hashie::Mash`.
35
+
36
+ ```ruby
37
+ story = @client.item(8422599)
38
+ story.title # Hacker News API
39
+ story.url # http://blog.ycombinator.com/hacker-news-api
40
+
41
+ comment_ids = story.kids
42
+ first_comment = @client.item(comment_ids.first)
43
+ first_comment.text # Oh man you guys, patio11 has generated...
44
+ ```
45
+
46
+ ### Users [GET /v0/user/#{id}.json](https://github.com/HackerNews/API#users)
47
+
48
+ Fetches a user by unique case-sensitive username. Returns a `Hashie::Mash`.
49
+
50
+ ```ruby
51
+ me = @client.user('co_pl_te')
52
+ me.karma # 4186
53
+ me.about.split(?\n).first # Everything to everyone.
54
+ me.submitted # an array of my submitted items' ids (stories, comments, etc.)
55
+ ```
56
+
57
+ ### Live Data
58
+
59
+ The following endpoints are updated in real-time and will allow you to observe changes in front page ranking, new items, and new profiles.
60
+
61
+ #### Top Stories [GET /v0/topstories.json](https://github.com/HackerNews/API#top-stories)
62
+
63
+ Fetches the current top 100 story ids. Returns an `Array`.
64
+
65
+ ```ruby
66
+ @client.top_stories
67
+ ```
68
+
69
+ #### Max Item ID [GET /v0/maxitem.json](https://github.com/HackerNews/API#max-item-id)
70
+
71
+ Fetches the current largest item id. Returns a `String`.
72
+
73
+ ```ruby
74
+ @client.max_item
75
+ ```
76
+
77
+ #### Changed Items and Profiles [GET /v0/updates.json](https://github.com/HackerNews/API#changed-items-and-profiles)
78
+
79
+ Fetches item and profile changes. Returns a `Hashie::Mash`.
80
+
81
+ ```ruby
82
+ updates = @client.updates
83
+ updates.items # an array of updated item ids
84
+ updates.profiles # an array of updated profile ids
85
+ ```
86
+
87
+ ## Contributing to hn_api
6
88
 
7
89
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
90
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
@@ -12,7 +94,7 @@ A Ruby wrapper for [the Hacker News API](https://github.com/HackerNews/API). Cur
12
94
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
95
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
96
 
15
- # Copyright
97
+ ## Copyright
16
98
 
17
99
  Copyright (c) 2014 Rahul Horé. See LICENSE.txt for
18
100
  further details.
@@ -23,5 +23,13 @@ module HN
23
23
  def top_stories(options = {})
24
24
  get('topstories.json', options)
25
25
  end
26
+
27
+ def max_item(options = {})
28
+ get('maxitem.json', options)
29
+ end
30
+
31
+ def updates(options = {})
32
+ get('updates.json', options)
33
+ end
26
34
  end
27
35
  end
@@ -3,13 +3,11 @@ require_relative 'version'
3
3
  module HN
4
4
  module Configuration
5
5
 
6
- VALID_CONFIGURATION_KEYS = [:api_version, :base_url, :api_url, :headers]
6
+ VALID_CONFIGURATION_KEYS = [:api_url, :headers]
7
7
 
8
8
  attr_accessor *VALID_CONFIGURATION_KEYS
9
9
 
10
- DEFAULT_VERSION = 'v0'
11
- DEFAULT_BASE_URL = 'https://hacker-news.firebaseio.com'
12
- DEFAULT_API_URL = "#{DEFAULT_BASE_URL}/#{DEFAULT_VERSION}/"
10
+ DEFAULT_API_URL = 'https://hacker-news.firebaseio.com/v0/'
13
11
  DEFAULT_HEADERS = { accept: 'application/json',
14
12
  user_agent: "hn_api gem #{HN::Version}" }
15
13
 
@@ -18,8 +16,6 @@ module HN
18
16
  end
19
17
 
20
18
  def reset
21
- self.api_version = DEFAULT_VERSION
22
- self.base_url = DEFAULT_BASE_URL
23
19
  self.api_url = DEFAULT_API_URL
24
20
  self.headers = DEFAULT_HEADERS
25
21
  self
@@ -15,8 +15,6 @@ module HN
15
15
  Faraday.new(options) do |connection|
16
16
  connection.use Faraday::Request::UrlEncoded
17
17
  connection.use Faraday::Response::RaiseError
18
- connection.use Faraday::Response::Mashify
19
- connection.use Faraday::Response::ParseJson
20
18
  connection.adapter(Faraday.default_adapter)
21
19
  end
22
20
  end
@@ -1,3 +1,6 @@
1
+ require 'json'
2
+ require 'hashie'
3
+
1
4
  module HN
2
5
  module Request
3
6
  def get(path, options = {})
@@ -10,7 +13,12 @@ module HN
10
13
  response = connection.send(method) do |request|
11
14
  request.url(path, options)
12
15
  end
13
- response.body
16
+ prepare(response)
17
+ end
18
+
19
+ def prepare(response)
20
+ result = JSON.parse(response.body) rescue response.body
21
+ result.is_a?(Hash) ? Hashie::Mash.new(result) : result
14
22
  end
15
23
  end
16
24
  end
@@ -2,7 +2,7 @@ module HN
2
2
  class Version
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- PATCH = 1
5
+ PATCH = 2
6
6
  PRE = nil
7
7
 
8
8
  class << self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hn_api
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
  - Rahul Horé
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-07 00:00:00.000000000 Z
11
+ date: 2014-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -128,20 +128,14 @@ extra_rdoc_files:
128
128
  - LICENSE.txt
129
129
  - README.md
130
130
  files:
131
- - ".document"
132
- - ".rspec"
133
- - Gemfile
134
131
  - LICENSE.txt
135
132
  - README.md
136
- - Rakefile
137
133
  - lib/hn/client.rb
138
134
  - lib/hn/configuration.rb
139
135
  - lib/hn/connection.rb
140
136
  - lib/hn/request.rb
141
137
  - lib/hn/version.rb
142
138
  - lib/hn_api.rb
143
- - spec/hn_api_spec.rb
144
- - spec/spec_helper.rb
145
139
  homepage: http://github.com/O-I/hn_api
146
140
  licenses:
147
141
  - MIT
@@ -154,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
154
148
  requirements:
155
149
  - - ">="
156
150
  - !ruby/object:Gem::Version
157
- version: '0'
151
+ version: 1.9.3
158
152
  required_rubygems_version: !ruby/object:Gem::Requirement
159
153
  requirements:
160
154
  - - ">="
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
data/Gemfile DELETED
@@ -1,19 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'faraday', '>= 0.7', '< 0.10'
4
- gem 'faraday_middleware', '>= 0.8', '< 0.10'
5
- gem 'hashie', '>= 0.4.0'
6
-
7
- group :development do
8
- gem 'rspec', '~> 3.0.0'
9
- gem 'rdoc', '~> 3.12'
10
- gem 'bundler', '~> 1.0'
11
- gem 'jeweler', '~> 1.8.7'
12
- end
13
-
14
- group :test do
15
- gem 'webmock', '~> 1.19.0'
16
- gem 'coveralls', require: false
17
- gem 'simplecov', require: false
18
- gem 'guard-rspec', require: false
19
- end
data/Rakefile DELETED
@@ -1,46 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- require 'jeweler'
15
- require './lib/hn/version'
16
- Jeweler::Tasks.new do |gem|
17
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
- gem.name = "hn_api"
19
- gem.version = HN::Version
20
- gem.homepage = "http://github.com/O-I/hn_api"
21
- gem.license = "MIT"
22
- gem.summary = %Q{Ruby wrapper for the Hacker News API}
23
- gem.description = %Q{Ruby client for the Hacker News API}
24
- gem.email = "hore.rahul@gmail.com"
25
- gem.authors = ["Rahul Horé"]
26
- # dependencies defined in Gemfile
27
- end
28
- Jeweler::RubygemsDotOrgTasks.new
29
-
30
- require 'rspec/core'
31
- require 'rspec/core/rake_task'
32
- RSpec::Core::RakeTask.new(:spec) do |spec|
33
- spec.pattern = FileList['spec/**/*_spec.rb']
34
- end
35
-
36
- task :default => :spec
37
-
38
- require 'rdoc/task'
39
- Rake::RDocTask.new do |rdoc|
40
- version = HN::Version
41
-
42
- rdoc.rdoc_dir = 'rdoc'
43
- rdoc.title = "hn_api #{version}"
44
- rdoc.rdoc_files.include('README*')
45
- rdoc.rdoc_files.include('lib/**/*.rb')
46
- end
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "HnApi" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end
@@ -1,12 +0,0 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
- require 'rspec'
4
- require 'hn_api'
5
-
6
- # Requires supporting files with custom matchers and macros, etc,
7
- # in ./support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
-
10
- RSpec.configure do |config|
11
-
12
- end