Pushrod

Old dogs, new tricks

ebay_shopping is now a gem (and now at github)

with 16 comments

OK. I’ve jumped on the Git bandwagon, and to celebrate have made the ebay_shopping plugin (a ruby on rails library for eBay’s shopping API) into a gem, hosted at Github. It’ll take a few days before the rubyforge project is approved and loaded up, but you can still download it from github, or with a github clone.

Why turn it into a gem? Well, it was originally a plugin as that was the easiest and quickest way to do it at the time — it was generated from a Rails application, after all. It also made things like getting the initial config (from a YAML file in the Rails config directory) a no-brainer, and meant I could use some of the ActiveSupport methods without thinking.

But over the past few weeks, I’ve been playing around with Merb, and decided these benefits are more than outweighed by the greater portability a gem brings. There’s also the benefit of versioning and dependencies. Finally, with Rails edging away from plugins with 2.1, and the ease of gem generation using Dr Nic’s newgem gem, there’s really no reason to stay with the plugin approach. Enter, stage left, the ebay_shopping gem.

If you’re already using the plugin, there’s no hurry to change. If not, give the gem a try and let me know how it goes. Use is almost identical to the plugin. The only difference is with the initial configuration. You can still use the same YAML config file in your Rails config folder (if you’re using Rails or Merb), you just need to set it up explictly in your environment.rb

EbayShopping::Request.config_params("#{RAILS_ROOT}/config/ebay.yml", RAILS_ENV)

Passing the RAILS_ENV as the second param just ensures it will use the correct environment settings from the config file, if you’ve got different ones for development, production, test, etc. If not, or if you don’t tell it, it will just default to the production settings.

You can also (from the console, for example), set the inital configuration with a Hash

EbayShopping::Request.config_params({:app_id => "my_app_id", :default_site_id =>"3"})

The hash must provide the app_id you’re given by ebay, and can optionally provide the ebay affiliate info and your preferred default country (e.g. the UK in the above one). This can be overridden in individual requests, or if you just leave it out it will default to ebay.com.

Enough waffle. Explore the code over at github. The documentation still needs tweaking, but the test suite and code comments should explain it all fairly well. Plus there are some use examples on the post about the original plugin, which still stand. Patches and forks welcome — this is git after all we’re talking about.

Update:

Finally gotten around to adding the gemspec file which allows github to build the gem automatically. So now all you need to doΒ  is the usual:

gem sources -a http://gems.github.com

sudo gem install ctagg-ebay_shopping

Written by ctagg

May 13, 2008 at 4:54 pm

Posted in ebay, rails, ruby

Tagged with , , , ,

16 Responses

Subscribe to comments with RSS.

  1. Hi,
    Great and thanks, but I’m slightly a noob at Ruby and could not figure out how to build your new(er) version from github as a Gem? Could you update the doco on how this works?
    Thanks

    Mark

    September 17, 2008 at 11:09 pm

  2. Thank you for this.
    I have been looking for something that will help me get started with the eBay api and Rails.

    Fingers crossed that this will do the job.

    πŸ™‚

    James

    James

    March 12, 2009 at 5:45 am

  3. Hi,
    Got it all plugged in and set up.
    I even got a simple search function working
    BUT…
    response.items only ever gives me 3 items despite a total count showing in the hundreds
    AND…
    I only ever get results from the US site despite setting the site_id to “3” for U.K.
    Implying that eBay.yml is not being read.

    Awesome utility. Just wish I new how to use it properly
    lol πŸ™‚
    Any ideas would be really cool
    Thank you

    James

    James

    March 13, 2009 at 6:02 pm

    • What happens if you open the Rails console and load it from there
      EbayShopping::Request.config_params("#{RAILS_ROOT}/config/ebay.yml", RAILS_ENV)
      You should see the Request echoed back at you with the correct default site_id in place.
      Similarly, have a go with making some requests in the console. You should be able to see the url that’s being call and you can check that all yur required params are being submitted.

      Hope this helps

      ctagg

      March 13, 2009 at 6:51 pm

  4. Wow,
    That was a quick response
    Thankyou

    EbayShopping::Request.config_params(“#{RAILS_ROOT}/config/ebay.yml”, RAILS_ENV)
    EbayShopping::Request.config_params(“#{RAILS_ROOT}/config/ebay.yml”, RAILS_ENV)
    ArgumentError: wrong number of arguments (2 for 0)
    from (irb):1:in `config_params’
    from (irb):1

    Not sure what is happening here

    equest = EbayShopping::Request.new(:find_items, {:query_keywords => “chevrolet camaro”}) Works fine from the console.

    James

    March 13, 2009 at 7:26 pm

  5. Your response lead me to investigate further and I have not added anything into the environment.rb
    This explains a lot but I get the same error as above if I do.

    I have a model class

    class EbayTest
    def self.search_ebay(item_to_search_for)
    @request = EbayShopping::Request.new(:FindItems, {:query_keywords => item_to_search_for})
    end
    end

    I’m calling it from my controller in response to an AJAX request

    def search_result
    # logger.debug("### Query - #{params[:search_query]}")
    @result = EbayTest.search_ebay(params[:search_query])
    @ebay_response = @result.response
    respond_to do |format|
    format.js
    end
    end
    </code
    The partial looks like this

    Item: <a href=>


    This only ever gets 3 results
    Should be 50 as this is the maximum for the shopping api and the total count is around 200 for the search I'm doing.

    Thanks again for such a quick response.

    James

    March 13, 2009 at 7:37 pm

  6. Sorry. Don’t know why the partial code cut off like that in my last post
    Here it is again

    Item: <a href=>

    James

    March 13, 2009 at 7:39 pm

    • Also, if you want more than the default 3 items returned, you have to specify it in the request parameters (see api docs). So your code should look something like:
      EbayShopping::Request.new(:find_items, {:query_keywords => item_to_search_for, :max_entries=>50}) # you can use FindItems, but find_items is a bit more ruby-ish

      ctagg

      March 13, 2009 at 9:26 pm

  7. Just to check. You’re definitely using the gem (from github) rather than the plugin, right?

    ctagg

    March 13, 2009 at 9:15 pm

  8. Yup,
    I’m using the Gem from GitHub
    I did get confused over FindItems and find_items.
    I’ll switch to using the find_items. Thank you.

    I just found your other Blog on the original plugin and afeter a good read of that I found the instructions for the environment.rb settings.

    It’s now totally obvious to me that I have the location problems because I don’t have this set up.

    I do not actually want to use the yml in my final app as I want to be able to change the location on the fly but it would be good to find out what is causing the argument errors.
    If there is anyhting you think I should be doing to get this error sorted or to track it down then let me know.

    I’ll keep pligging away at it and see what I can come up with.

    Thanks for the info on the 3 items thing. I just found that on your other blog too. πŸ™‚

    James

    James

    March 13, 2009 at 10:06 pm

    • You can still override the country/ebay site on the fly — the default country just sets the country/site which is used if no country is passed. On Autopendium, the classic car website I run, the ebay results returned are based on the country of the visitor (which is autosensed via javascript and Google Ajax library API).

      Just pass :site_id => [desired site_id] along with the other params to override the default.

      I’m struggling to understand the problem re the config file — the error implies that the config_params method doesn’t take any arguments (which was how it was configured in the plugin), but in the gem you specify the location of the config file (since it’s not necessarily being called from a rails app), as you can see from the code here

      ctagg

      March 13, 2009 at 11:08 pm

  9. I am certain that this is my bad somewhere along the line.

    The installation instructions I followed were
    gem sources -a http://gems.github.com # if you haven’t done this in the past for other gems hosted at github
    sudo gem install ctagg-ebay_shopping

    simply run the usual: script/plugin install http://ebay-shopping.googlecode.com/svn/trunk/ ebay_shopping

    Then from the root of your rails app run ruby vendor/plugins/ebay_shopping/install.rb.

    I’ll check that I haven’t got the plugin installed somehow and re-install everything.

    Thanks for your help.
    I’ll let you know how I get on.
    James

    James

    March 14, 2009 at 9:18 am

    • script/plugin install http://ebay-shopping.googlecode.com/svn/trunk/ ebay_shopping
      That’ll be your problem. You’ve installed the plugin AND the gem, and rails give the plugin precedence as it lives in the vendor directory. Remove that and you shoiuld be good to go.

      ctagg

      March 14, 2009 at 5:59 pm

  10. lol πŸ™‚
    simply run the usual: script/plugin install http://ebay-shopping.googlecode.com/svn/trunk/ ebay_shopping
    Reckon that may be the problem πŸ™‚

    James

    March 14, 2009 at 9:20 am

  11. Just about tried everything I can think of now.
    Whatever I try I get the same argument error when I run
    EbayShopping::Request.config_params(“#{RAILS_ROOT}/config/ebay.yml”, RAILS_ENV)

    Now I know that I’m doing something wrong but I just can’t figure out what it is.

    Nevermind. I’m going to figure out a different way of using this for the minuite and when I get more time I’ll look at finding out how to install the gem properly.

    Thanks for all the help

    James

    James

    March 14, 2009 at 11:06 am


Leave a comment