Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTParty options parameter not functioning properly

Tags:

ruby

httparty

I'm setting up an application which can make LastFM API Requests. These are simple get requests and I'm using the HTTParty gem.

My function is as follows:

def get_albums
  self.class.base_uri "http://ws.audioscrobbler.com/2.0/"
  options = {
    :user => "Gerard1992",
    :method => "user.gettopalbums", 
    :api_key => Constants::LASTFM_API_KEY, 
    :format => "json"
  }
  puts options.to_query
  self.class.get "/?#{options.to_query}", {} #options don't work
end

This piece of code that's shown above works. The get request returns a set of JSON. My problem is that this /?#{options.to_query} doesn't look that neat. And neither does the actual (now empty {}) options parameter. How do I get the HTTParty options parameter to work like it should?

This is what I've tried, but both cases failed:

self.class.get "/", options
self.class.get "/", options => options

I appreciate the help.

like image 933
Biketire Avatar asked May 26 '13 13:05

Biketire


1 Answers

The correct option for query parameters in HTTParty is :query, so what you want is:

self.class.get "/", query: options

You can see all the available parameters in the docs.

like image 129
Andrew Marshall Avatar answered Sep 22 '22 16:09

Andrew Marshall