Sha256: b6ddbaaff481c14d020e35c9bf97a41da1f5908749469dbb282ae6547d327175

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'rest_client'
module Amzwish
  module Services
    class WebsiteWrapper
      FIND_WISHLIST_URL = "http://www.amazon.co.uk/gp/registry/search.html?ie=UTF8&type=wishlist"
      DISPLAY_WISHLIST_URL_TEMPLATE = "http://www.amazon.co.uk/registry/wishlist/%s"
      
      def initialize(rest_client = RestClientWrapper.new)
        @rest_client = rest_client
      end            

      def find_for(email)
        resp = @rest_client.post(email)
        # If a user has a single public wishlist then should get a redirect to it
        if (resp[:code] == 302)
          /(?:\?|&)id=(\w*)/ =~ resp[:headers][:location]
          [{:id => $~[1]}]
        else
          []
        end
      end

      def get_page(wishlist_id, page=1)
        @rest_client.get(wishlist_id, page)
      end
    end
    
    class RestClientWrapper
      def post(email)
        r = RestClient.post( WebsiteWrapper::FIND_WISHLIST_URL, "field-name" => email ) do |resp, req, result| 
          {:code => resp.code, :headers=>resp.headers}
        end
      end

      def get(wishlist_id, page)
        url = generate_url_for_wishlist(wishlist_id)
        params = { :page => page, :_encoding => 'UTF8', :filter => '3', :sort=> 'date-added',
          :layout => 'compact', :reveal => 'unpurchased'}
        RestClient.get( url, :params => params ) do |resp, req, result|
          raise "could not find wishlist" unless resp.code == 200 
          resp.body
        end
      end

      private 
      def generate_url_for_wishlist(id)
        sprintf(WebsiteWrapper::DISPLAY_WISHLIST_URL_TEMPLATE, id)
      end    
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amzwish-0.0.0 lib/amzwish/services/website_wrapper.rb