Search code examples
ruby-on-railsrubyruby-on-rails-3.1rubygemslinkedin-api

Ruby Linkedin Gem people search with nested field selectors


I am using this very nice LinkedIn ruby gem (https://github.com/pengwynn/linkedin) for a project of mine, and have got stuck at a very painful point for the past 1 day.

I'm doing a people search and using nested field selector for the "people" resource. My query looks something like this :

@client.search(:fields => [ { :people => %w(id first-name api-standard-profile-request) } ], :keywords => 'some keywords')

and am getting a bad URI exception

bad URI(is not URI?): /people-search:({:people=>["id", "first-name", "api-standard-profile-request"]})

Of course this is not what the URL should be like. It should be like

/people-search:(people:(id,first-name,api-standard-profile-request))

After spending some time on github and going through the code, I could understand how the path is been generated by the gem. In the search.rb file :

def field_selector(fields)
    result = ":("
    fields = fields.to_a.map do |field|
      if field.is_a?(Hash)
        innerFields = []
        field.each do |key, value|
          innerFields << key.to_s.gsub("_","-") + field_selector(value)
        end
        innerFields.join(',')
      else
        field.to_s.gsub("_","-")
      end
    end
    result += fields.join(',')
    result += ")"
    result
  end

where fields is the array that I'm passing to the search method.

The strange part is, that when I run this piece of code (field_selector method) in the console, with my fields array as input, I get the desired output !

I'm totally lost as to why I'm getting the error, and have no idea how to go about fixing it. Any pointers would be awesome !

Thanks !


Solution

  • Your code worked for me. Try upgrading your gem to the 2-0 branch. Add this to your gem file: gem 'linkedin', :git => 'http://github.com/pengwynn/linkedin.git', :branch => '2-0-stable'