Permalink
Browse files

added basic structure, it know how to find the type of sth

  • Loading branch information...
1 parent 355afe0 commit 78b52301fb139a9fed35f85f610a76d9e53715fc @jcla1 committed Sep 16, 2012
Showing with 147 additions and 2 deletions.
  1. +4 −0 Gemfile
  2. +11 −2 Rakefile
  3. +27 −0 hn2json.gemspec
  4. +13 −0 lib/hn2json.rb
  5. +37 −0 lib/hn2json/entity.rb
  6. +34 −0 lib/hn2json/parser.rb
  7. +21 −0 lib/hn2json/request.rb
View
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+gem "rest-client", "~> 1.6.7"
+gem "nokogiri", "~> 1.5.5"
View
@@ -4,7 +4,6 @@ def command?(command)
system("type #{command} > /dev/null 2>&1")
end
-
#
# Gems
#
@@ -19,6 +18,16 @@ task :push => [:gem] do
sh "gem push #{file}"
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"
task :tag do
sh "git tag v#{HN2JSON::VERSION}"
@@ -28,5 +37,5 @@ end
desc "tag version and push gem to server"
task :release => [:push, :tag] do
- puts "And away she goes!"
+ puts "And away it goes!"
end
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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.