Class: Base
Instance Method Summary collapse
- #base_url ⇒ Object
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #method_missing(method, *args) ⇒ Object
- #parameterize(options) ⇒ Object
- #select_country ⇒ Object
- #select_service(method) ⇒ Object
- #set_app_id ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/buscape_api/base.rb', line 6 def initialize( = {}) raise "No :app_id set" unless .include? :app_id [:format] = 'json' # JSON is more lightweight than xml @options = @services = [ { :method => :categories, :service => :findCategoryList }, { :method => :products, :service => :findProductList }, { :method => :offers, :service => :findOffersList, :item => :offer }, { :method => :top_products, :service => :topProducts }, { :method => :user_ratings, :service => :viewUserRatings }, { :method => :product_details, :service => :viewProductDetailst }, { :method => :seller_details, :service => :viewSellerDetails } ] @methods = @services.map { |service| service[:method] } @url = base_url end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/buscape_api/base.rb', line 50 def method_missing(method, *args) if @methods.include? method select_service method set_app_id select_country if @options.include? :country_code self elsif method == :where parameterize args.first response = self.class.get(@url)['Result'] else raise NoMethodError end end |
Instance Method Details
#base_url ⇒ Object
23 24 25 |
# File 'lib/buscape_api/base.rb', line 23 def base_url @options[:sandbox] ? "http://sandbox.buscape.com" : "http://bws.buscape.com" end |
#parameterize(options) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/buscape_api/base.rb', line 42 def parameterize() parameters = String.new .keys.each_with_index do |parameter,index| index == 0 ? @url << "/?#{parameter}=#{[parameter]}" : @url << "&#{parameter}=#{[parameter]}" end end |
#select_country ⇒ Object
38 39 40 |
# File 'lib/buscape_api/base.rb', line 38 def select_country @url << '/' << @options[:country_code] end |
#select_service(method) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/buscape_api/base.rb', line 27 def select_service(method) @services.each do |s| @service = s[:service] if s.values.include? method end @url << '/service/' << @service.to_s end |
#set_app_id ⇒ Object
34 35 36 |
# File 'lib/buscape_api/base.rb', line 34 def set_app_id @url << '/' << @options[:app_id] end |