Skip to content

Commit

Permalink
Unescaping *after* parsing JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaranarcher committed Jan 9, 2013
1 parent 5dabeb8 commit 9abade8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/hacker_term/page_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ class PageData
attr_reader :data, :mean_score, :median_score, :mode_score, :sorted_by, :line_pos

def initialize(data)
unescaped = CGI.unescapeHTML data
@data = JSON.parse(unescaped)['items']
begin
@data = JSON.parse(data)['items']
rescue JSON::ParserError
raise "JSON appears to be malformed: #{unescaped}" # Bomb out for now...
end

add_missing_keys!
format_numbers!
format_urls!

unescape_titles!

calculate_mean_score
calculate_median_score
calculate_mode_score
Expand All @@ -28,6 +32,10 @@ def initialize(data)
@line_pos = 1
end

def unescape_titles!
@data.each { |row| row['title'] = CGI.unescapeHTML(row['title']) }
end

def sort_on!(mode)
case mode
when :score
Expand Down

0 comments on commit 9abade8

Please sign in to comment.