Last active
August 24, 2017 13:47
-
-
Save dataday/bc591847875d8b9b0e5bc8df809fa082 to your computer and use it in GitHub Desktop.
Gets the response data from external JSONP data source
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
# Author: dataday | |
# http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html | |
require 'json' | |
# https://github.com/sporkmonger/addressable | |
require 'addressable/uri' | |
# http://www.rubydoc.info/gems/activesupport/5.0.0 | |
require 'active_support' | |
# https://github.com/rest-client/rest-client | |
require 'rest-client' | |
path = 'data.co.uk.json' | |
url = 'https://domain.co.uk/search?callback=_' | |
# if local file exists | |
if File.file?(path) | |
# get local data | |
file = File.read(path) | |
else | |
# get remote data | |
file = RestClient.get(url, headers={}) | |
end | |
# remove JSONP function wrapper | |
file.gsub! /^\_\(|\);$/, '' | |
# parse file data as JSON | |
data = JSON.parse(file, symbolize_names: true) | |
# create deafult result set | |
results = Array.new | |
location_keys = { :lat => :latitude, :lng => :longitude } | |
data.each_with_index do |obj, index| | |
result = Hash.new | |
# assign require data | |
dataset = obj[:dataset_information] | |
centres = obj[:dataset_centre].length || 0 if obj[:dataset_centre] | |
location = dataset[:geolocation] | |
# rename location keys | |
location.keys.each { |k| location[ location_keys[k] ] = location.delete(k) if location_keys[k] } | |
# delete location obj | |
dataset.delete(:geolocation) | |
# merge geolocation into main Hash | |
dataset.merge!(location) | |
# add centres count | |
dataset[:centres] = centres | |
# add centres count | |
dataset[:index] = index+1 | |
# extract resulting key value pairs | |
result = dataset.extract!(:index, :name, :permalink, :website, :address, :longitude, :latitude, :centres) | |
# add result to results list | |
results.push result | |
end | |
# write results to file | |
File.open('dataset-snapshot-2016-2017.json', 'w') do |fh| | |
fh.write(results.to_json) | |
end | |
# write results to stnout | |
puts results | |
#/^_^... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment