Skip to content

Instantly share code, notes, and snippets.

@swistak
Created September 17, 2012 11:21
Show Gist options
  • Save swistak/3736755 to your computer and use it in GitHub Desktop.
Save swistak/3736755 to your computer and use it in GitHub Desktop.
Hacker news API test
require 'open-uri'
require 'json'
require 'ostruct'
require 'mail' # you need to `gem install mail`
# Retrieval
print "Connecting to api.ihackernews.com ... "; $stdout.flush
home_page = open('http://api.ihackernews.com/page').read
puts "done."
home_page_json = JSON.parse(home_page)
items = home_page_json["items"]
points = items.map{|i| i["points"] }
raise ArgumenError, "We failed to get points data" if points.length == 0
# Mean
sum = points.inject(0, &:+)
mean = sum.to_f / points.length
# Median
sorted = points.sort
l = sorted.length
median = l % 2 == 0 ?
(sorted[l/2-1] + sorted[l/2]).to_f / 2 : # even
sorted[l/2] # odd
cutoff = l % 2 == 0 ? l/2 : l/2+1
top_stories = items.sort_by{|i| i["points"] }[cutoff..-1].reverse
# Modes
distribution = Hash.new{|h,k| h[k] = 0}
points.each{|e| distribution[e] += 1}
highest = distribution.sort_by{|k,v| v}.last[1]
modes = distribution.select{|k,v| v == highest}.map{|k,v| k}
content = []
content << "Mean: %0.2f" % mean
content << "Median: %0.2f" % median
content << "Modes (x%d): %s" % [ highest, modes.join(", ") ]
content << ""
content += top_stories.map{|i| s = OpenStruct.new(i)
"= #{s.title} (#{s.points} points)\n"+
"#{s.url}\n"+
"Posted by #{s.postedBy} #{s.postedAgo}\n"
}
mail = Mail.new
mail.from = 'marcin@example.com'
mail.to = 'subscriber@example.com'
mail.subject = 'Top stories from HN daily!'
mail.body = content.join("\n")
if ENV["DELIVER"]
# mail.delivery_method :sendmail
# by default local smtp server on port 25 is used.
mail.deliver!
else
puts mail.to_s
end
#!/bin/env irb
require 'open-uri'
#=> true
require 'json'
#=> true
home_page = open('http://api.ihackernews.com/page')
#=> #<StringIO:0x0000000206bd08>
home_page = home_page.read
#=> "{\"nextId\":null,\"items\":[{\"title\": ...
home_page_json = JSON.parse(home_page)
#=> {"nextId"=>nil, "items"=>[{"title"=>"...
items = home_page_json["items"]
#=> [{"title"=>"Everything's broken and nobody's upset",
points = items.map{|i| i["points"] }
#=> [110, 104, 337, 187, 19, 46, 9, 22, 40, 47, 22, 105, 27, 236, 87, 142, 5, 19, 55, 52, 97, 107, 47, 71, 29, 64, 3, 54, 62, 21]
points.sum
# NoMethodError: undefined method `sum' for #<Array:0x00000001d3bd40>
# from (irb):9
# from /home/swistak/.rvm/rubies/ruby-1.9.2-p320/bin/irb:16:in `<main>'
sum = points.inject(0, &:+)
#=> 2226
mean = sum / points.length
#=> 74
mean_f = sum.to_f / points.length
#=> 74.2
points.length
#=> 30
l = points.length
#=> 30
sorted = points.sort; median = l % 2 == 0 ? (sorted[l/2-1] + sorted[l/2]) / 2 : sorted[l/2]
#=> 53
modes = Hash.new{|h,k| h[k] = 0}; sorted.each{|e| modes[e] += 1}; modes.sort{|pair| pair[1]}
#=> [[337, 1], [55, 1], [3, 1], [62, 1], [9, 1], [64, 1], [21, 1], [71, 1], [27, 1], [87, 1], [40, 1], [97, 1], [47, 2], [104, 1], [54, 1], [105, 1], [19, 2], [107, 1], [29, 1], [110, 1], [52, 1], [142, 1], [22, 2], [187, 1], [5, 1], [236, 1], [46, 1]]
sorted
#=> [3, 5, 9, 19, 19, 21, 22, 22, 27, 29, 40, 46, 47, 47, 52, 54, 55, 62, 64, 71, 87, 97, 104, 105, 107, 110, 142, 187, 236, 337]
modes = Hash.new{|h,k| h[k] = 0}; sorted.each{|e| modes[e] += 1}; modes.sort_by{|pair| pair[1]}
#=> [[3, 1], [5, 1], [9, 1], [337, 1], [21, 1], [236, 1], [27, 1], [29, 1], [40, 1], [46, 1], [187, 1], [52, 1], [54, 1], [142, 1], [62, 1], [64, 1], [71, 1], [87, 1], [97, 1], [104, 1], [105, 1], [107, 1], [110, 1], [55, 1], [47, 2], [22, 2], [19, 2]]
distribution = Hash.new{|h,k| h[k] = 0}; points.each{|e| distribution[e] += 1}; highest = distribution.sort_by{|k,v| v}.last[1]; modes = distribution.select{|k,v| v == highest}.map{|k,v| k}
#=> [19, 22, 47]
require 'open-uri'
require 'json'
# Retrieval
print "Connecting to api.ihackernews.com ... "; $stdout.flush
home_page = open('http://api.ihackernews.com/page').read
puts "done."
home_page_json = JSON.parse(home_page)
items = home_page_json["items"]
points = items.map{|i| i["points"] }
raise ArgumenError, "We failed to get points data" if points.length == 0
# Mean
sum = points.inject(0, &:+)
mean = sum.to_f / points.length
# Median
sorted = points.sort
l = sorted.length
median = l % 2 == 0 ?
(sorted[l/2-1] + sorted[l/2]).to_f / 2 : # even
sorted[l/2] # odd
# Modes
distribution = Hash.new{|h,k| h[k] = 0}
points.each{|e| distribution[e] += 1}
highest = distribution.sort_by{|k,v| v}.last[1]
modes = distribution.select{|k,v| v == highest}.map{|k,v| k}
puts "Mean: %0.2f" % mean
puts "Median: %0.2f" % median
puts "Modes (x%d): %s" % [ highest, modes.join(", ") ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment