forked from devbuscapecompany/buscape-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buscape.rb
61 lines (45 loc) · 1.49 KB
/
buscape.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'httparty'
require 'uri'
class BuscaPe
include HTTParty
def initialize(options = {})
raise "You need to inform your :application_id" if options[:application_id].nil?
@base_uri = "sandbox.buscape.com/service" unless options[:sandbox].nil? || !options[:sandbox]
@application_id = options[:application_id];
@uris = {
:categories => "findCategoryList",
:products => "findProductList",
:ratings => "viewUserRatings",
:oferts => "findOfferList",
:details => "viewProductDetails"
}
@params = {
:category => "categoryId",
:product => "productId",
:top_products => "topProducts",
:seller => "sellerId",
:offer => "offerId",
:keyword => "keyword"
}
@data = {}
end
private
def self.method_missing(method, *args, &block)
if @uris.map {|v, k| v }.include? method
self.fetch_api(method)
else
@data.merge!({method => args[0]})
self
end
end
def self.fetch_api(method)
raise "Method '#{method}' doesn't exist!" if @uris[method].empty?
@uris[method] = "viewSellerDetails" if method === :details && !@data[:seller].blank? && @data[:product].blank?
url = "http://#{@base_uri}/#{@uris[method]}/#{@application_id}/"
@data.each { |sym, value|
url += ((url[-1, 1] == "/") ? "?" : "&") + "#{(@params[sym].blank?) ? sym.to_s : @params[sym]}=#{value}"
}
uri_parser = URI::Parser.new
self.get(uri_parser(url))
end
end