9

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.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Biketire
  • 2,019
  • 1
  • 23
  • 41

2 Answers2

22

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.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
  • I find this rather weird. I also switched from the HTTParty gem to the Rest-client gem, because HTTParty uses the 'self.class." which is not that neat. Thanks anyway for the answer :) – Biketire May 28 '13 at 09:51
0

Send :verify => false in options hash

Omer Temel
  • 804
  • 1
  • 7
  • 18