We can get the stories using this example. I add this on LocalNews class.
class LocalNews < ActiveRecord::Base
@key = 'f889ntmabe5mjqr9dzd2a7ce'
@secret = 'yZM6nU4zGR'
require 'open-uri'
require 'openssl'
require 'md5'
def self.generate_key
"dev_key=#{@key}&sig=#{MD5.new(@key + @secret + Time.now.to_i.to_s).hexdigest}"
end
def self.generate_url_from_zipcode(zipcode, oi_key, limit)
'http://hyperlocal-api.outside.in/v1.1/zipcodes/'+zipcode.to_s+'/stories?'+oi_key+(limit.nil? ? '' : '&limit='+limit.to_s)
end
def self.get_content(zipcode, limit=20)
oi_key = generate_key
url = generate_url_from_zipcode(zipcode, oi_key, limit)
open_url = open(url) rescue nil
return '' if open_url.nil?
json = open_url.read
contents = JSON.parse(json)
end
end
We can get stories using command like this
LocalNews.get_content(12345)
We can limit our stories to some number using command like this
It'll get 5 stories.
LocalNews.get_content(12345, 5)
I use Json to parse the stories.
No comments:
Post a Comment