Skip to content
This repository has been archived by the owner on Apr 4, 2018. It is now read-only.

Commit

Permalink
Refactored method for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Fliri committed Jan 22, 2013
1 parent 037919e commit b64c0cd
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/response/insidegov_weekly_policy_entries_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(response, config_class)

def create_messages response_as_hash
rows = (response_as_hash["rows"] or [])
condense_to_one_week(rows).map { |slug, entries|
collect_by_slug(rows).map { |slug, entries|
create_message({
start_at: extract_start_at(response_as_hash["query"]["start-date"]),
end_at: extract_end_at(response_as_hash["query"]["end-date"]),
Expand All @@ -28,13 +28,17 @@ def create_messages response_as_hash
}
end

def condense_to_one_week rows
rows.
map { |week, slug, entries| [slug, entries.to_i] }.
group_by { |slug, entries| slug }.
map { |slug, array|
[slug, array.map { |slug, entries| entries }.reduce(&:+)]
}
def collect_by_slug rows
entries_by_slug = discard_week(rows).group_by { |slug, _| slug }
sum_entries(entries_by_slug)
end

def sum_entries(entries_by_slug)
entries_by_slug.map { |slug, array| [slug, array.map { |_, entries| entries }.reduce(&:+)] }
end

def discard_week(rows)
rows.map { |_, slug, entries| [slug, entries.to_i] }
end
end
end

0 comments on commit b64c0cd

Please sign in to comment.