Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
added basic structure, it know how to find the type of sth
Browse files Browse the repository at this point in the history
  • Loading branch information
jcla1 committed Sep 16, 2012
1 parent 355afe0 commit 78b5230
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

gem "rest-client", "~> 1.6.7"
gem "nokogiri", "~> 1.5.5"
13 changes: 11 additions & 2 deletions Rakefile
Expand Up @@ -4,7 +4,6 @@ def command?(command)
system("type #{command} > /dev/null 2>&1") system("type #{command} > /dev/null 2>&1")
end end



# #
# Gems # Gems
# #
Expand All @@ -19,6 +18,16 @@ task :push => [:gem] do
sh "gem push #{file}" sh "gem push #{file}"
end end


desc "Install gem."
task :install => [:gem] do
sh "gem install hn2json-#{HN2JSON::VERSION}.gem"
end

desc "Build the gem, install it and open irb."
task :irb => [:install] do
sh "irb -r hn2json"
end

desc "tag version" desc "tag version"
task :tag do task :tag do
sh "git tag v#{HN2JSON::VERSION}" sh "git tag v#{HN2JSON::VERSION}"
Expand All @@ -28,5 +37,5 @@ end


desc "tag version and push gem to server" desc "tag version and push gem to server"
task :release => [:push, :tag] do task :release => [:push, :tag] do
puts "And away she goes!" puts "And away it goes!"
end end
27 changes: 27 additions & 0 deletions hn2json.gemspec
@@ -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
13 changes: 13 additions & 0 deletions lib/hn2json.rb
@@ -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
37 changes: 37 additions & 0 deletions lib/hn2json/entity.rb
@@ -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
34 changes: 34 additions & 0 deletions lib/hn2json/parser.rb
@@ -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
21 changes: 21 additions & 0 deletions lib/hn2json/request.rb
@@ -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

0 comments on commit 78b5230

Please sign in to comment.