-1

Rails 4.5 Ruby 2.3.1

I am getting json from an API and trying to store the following into a model CLrates 1. timestamp as unix time (date) 2. Currency_code (string) 3. quote (decimal monetary value)

I can use the following in irb after parsing the json and know how to get the elements individually using: response["quotes"]. How can I generate params to be saved in the model above when the body is as follows:

irb(main):036:0> puts response.body
{
 "success":true,
 "terms":"https:\/\/xxxxx.com\/terms",
 "privacy":"https:\/\/xxxxx.com\/privacy",
 "timestamp":1504817289,
 "source":"USD",
 "quotes":{
   "USDAED":3.672703,
   "USDAFN":68.360001,
   "USDCUC":1,
   "USDCUP":26.5,
   "USDCVE":91.699997,
   "USDCZK":21.718701,
    ............ many more lines removed for brevity
   "USDZWL":322.355011
 }

I can do this using a separate associated model but have very little idea how to create the params to save to a single table.

The following links got me to this point and well worth a read if you need info on httparty GET (client): 1. http://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ 2. http://eric-price.net/blog/rails-api-wrapper/ 3. https://www.driftingruby.com/episodes/playing-with-json

The class and method in lib/clayer.rb:

 class clayer
   include HTTParty
   format :json
   read_timeout 10

  def self.get_quotes
    response = HTTParty.get('http://www.nnnnnnn.net/api/live?
    access_key=nnnnnnnnnn&format=1')
  end
end

I used irb as I am still learning how to run this through rails c. This will be called in the controller and saved however need to work out how to get the params from the json

Thanks for the help

OK: after digging I think I am on the right track I get the response["QUOTES"], loop through them and build the params required saving each at the end of the loop

rates = response["QUOTES"]
rates.each do |k,v|
  clrate = Realtimerates.new
  clrate.date = response["timestamp"] 
  clrate.countrycode = "#{k}"
  clrate.price = "#{v}"
  clrate.save
end

Going to give this a whirl

In model

class Realtimerate < ActiveRecord::Base

 include HTTParty
 format :json
 read_timeout 5


def self.get_cl_rates
    response = HTTParty.get('http://www.mmmmm.net/api/live?access_key="key"&format=1')
    rates = response["quotes"]
    rates.each do |k,v|
        create!(
        date: Time.at(response["timestamp"]),
        country_code: "#{k}",
        price: "#{v}")

    end

 end

end

In the controller:

def index
  Realtimerate.get_cl_rates
  @realtimerates = Realtimerate.all
end

This is working and shows latest GET.

1 Answers1

0

You already have a hash in your response.body. All you need to do now is to assign the relevant key-value to your model's attributes. e.g.

clrate = ClRate.new
res = response.body
clate.time = res["timestamp"] 
...
clate.save
EJAg
  • 3,210
  • 2
  • 14
  • 22
  • Thanks for the response (punny isnt it). So I need a model of the keys: USDAED, USDAFN, USDCUC, USDCUP...Then loop through each element and build the params hash. Is there a way to do it without knowing the key and adding an extra model? I understand how to access them as follows and will use your method posted to construct: response["quotes"]["USDAED"]. This does give me the price: is there a way to reference the key symbolically? – giarcnappel Sep 08 '17 at 13:03
  • Hi your latest attempt is on the right track but will only capture the last currency code, value pair. One solution is to save each pair in one object, otherwise you need one field for each currency code. – EJAg Sep 08 '17 at 14:37
  • thanks a stack for the help. I have got it working via the index action so it will show the latest GET: quite funky: lovn it. Would like to hear your comments on the solution. Especially the object approach! Probably a lot better than this. Also: moved the method into the model and called it from the controller Realtimerates. Please see edits – giarcnappel Sep 08 '17 at 22:09
  • Glad you got it to work. But I don't think you need to include HTTParty if you only need to call its class method. In fact it's probably better to not mix it in here, because you'll end up with a messy interface. – EJAg Sep 09 '17 at 01:13