Created
June 10, 2015 16:28
-
-
Save allolex/0e0a9f791439321979bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'httparty' | |
| require 'json' | |
| # Use this in Rails | |
| # rails g model report date:datetime count:integer | |
| # rails db:migrate | |
| # Because this isn't Rails | |
| class Report | |
| attr_accessor :date, :count | |
| def initialize(params) | |
| @date = params[:time] | |
| @count = params[:count] | |
| end | |
| end | |
| def symbolize_keys(hash) | |
| # Rails has this already! | |
| # daily_report.symbolize_keys! | |
| results = {} | |
| hash.each_pair do |k, v| | |
| results[k.gsub(/\s+/, "_").to_sym] = v | |
| end | |
| results | |
| end | |
| def sum_all(reports) | |
| all_counts = reports.map(&:count) | |
| all_counts.inject { |sum, n| sum + n } | |
| end | |
| puts "Are we good at pharmaceuticals?" | |
| # Controller or Rake task code, possibly a service object, background job | |
| response = HTTParty.get("https://api.fda.gov/drug/event.json?search=receivedate:[20040101+TO+20150101]&count=receivedate") | |
| results = JSON.parse(response.body) | |
| reports = [] | |
| results['results'].each do |daily_report| | |
| params = symbolize_keys(daily_report) | |
| # Rails: Report.create(params) | |
| reports << Report.new(params) | |
| end | |
| puts sum_all(reports).to_s + " total incidents" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment