This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added basic structure, it know how to find the type of sth
- Loading branch information
Showing
7 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
source "http://rubygems.org" | |||
|
|||
gem "rest-client", "~> 1.6.7" | |||
gem "nokogiri", "~> 1.5.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
require File.dirname(__FILE__) + '/lib/hn2json/version' | |||
|
|||
Gem::Specification.new do |s| | |||
s.name = "hn2json" | |||
s.version = HN2JSON::VERSION | |||
s.date = Time.now.strftime('%Y-%m-%d') | |||
s.summary = "A Ruby interface to HackerNews" | |||
s.homepage = "http://github.com/jcla1/HN2JSON" | |||
s.email = "whitegolem@gmail.com" | |||
s.authors = [ "Joseph Adams" ] | |||
s.has_rdoc = false | |||
|
|||
s.files = %w( README.md Rakefile LICENSE ) | |||
s.files += Dir.glob("lib/**/*") | |||
|
|||
s.add_dependency "rest-client", "~> 1.6.7" | |||
s.add_dependency "nokogiri", "~> 1.5.5" | |||
|
|||
#s.files += Dir.glob("bin/**/*") | |||
#s.executables = %w( hn2json ) | |||
|
|||
s.description = <<desc | |||
HN2JSON is a developer frendly interface to HackerNews. | |||
It provides the functionality to retrieve any HN content | |||
page in stringified JSON or a Ruby object. | |||
desc | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
require 'rest-client' | |||
require 'nokogiri' | |||
|
|||
module HN2JSON | |||
autoload :Request, 'hn2json/request' | |||
|
|||
autoload :Parser, 'hn2json/parser' | |||
|
|||
autoload :Entity, 'hn2json/entity' | |||
|
|||
|
|||
autoload :VERSION, 'hn2json/version' | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
module HN2JSON | |||
|
|||
class Entity | |||
|
|||
attr_accessor :type, :id, :parent, :url, :title, :comments, :votes | |||
attr_accessor :full_text, :posted_by, :date_posted, :voting_on | |||
|
|||
def initialize id | |||
@id = id | |||
|
|||
@type = nil | |||
@parent = nil | |||
@url = nil | |||
@title = nil | |||
@full_text = nil | |||
@posted_by = nil | |||
@date_posted = nil | |||
@voting_on = nil | |||
@comments = nil | |||
@votes = nil | |||
|
|||
get_page | |||
determine_type | |||
end | |||
|
|||
def get_page | |||
@html = Request.new(id) | |||
@parser = Parser.new @html | |||
end | |||
|
|||
def determine_type | |||
@type = @parser.determine_type | |||
end | |||
|
|||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,34 @@ | |||
module HN2JSON | |||
|
|||
# Public: Parse HTML to produce HackerNews entities | |||
|
|||
class Parser | |||
def initialize response | |||
@doc = Nokogiri::HTML::DocumentFragment.parse response.html | |||
end | |||
|
|||
|
|||
def determine_type | |||
title = @doc.css('.title a') | |||
|
|||
if title.length < 1 | |||
return :comment | |||
else | |||
forms = @doc.css('td form') | |||
if forms.length === 1 | |||
return :poll | |||
else | |||
forms = @doc.css('td')[10].css('form') | |||
if forms.length === 1 | |||
return :post | |||
else | |||
return :discussion | |||
end | |||
end | |||
end | |||
|
|||
end | |||
|
|||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,21 @@ | |||
module HN2JSON | |||
|
|||
class Request | |||
attr_accessor :html | |||
|
|||
def initialize id | |||
@base_url = "http://news.ycombinator.com/item?id=" | |||
@complete_url = @base_url + id.to_s | |||
|
|||
request_page | |||
end | |||
|
|||
private | |||
|
|||
def request_page | |||
@html = RestClient.get @complete_url | |||
end | |||
|
|||
end | |||
|
|||
end |