fix filters for orders method, add customers method, rewrite orders_statuses method

This commit is contained in:
Alex Lushpai 2015-04-14 17:31:43 +03:00
parent 9a16a5068f
commit 9329ce984b

View File

@ -13,10 +13,8 @@ class Retailcrm
@rr_url = "#{url}/api/v#{@rr_version}/" @rr_url = "#{url}/api/v#{@rr_version}/"
@rr_key = key @rr_key = key
@rr_params = { :apiKey => @rr_key } @rr_params = { :apiKey => @rr_key }
end @filter = nil
@ids = nil
def self.up(params)
@rr_params = params
end end
## ##
@ -24,18 +22,34 @@ class Retailcrm
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3 # http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
# #
# Example: # Example:
# >> Retailcrm.orders(50, 2, {:email => 'test@example.com', :status => 'new'}) # >> Retailcrm.orders({:email => 'test@example.com', :status => 'new'}, 50, 2)
# => {...} # => {...}
# #
# Arguments: # Arguments:
# filter (Hash)
# limit (Integer) (20|50|100) # limit (Integer) (20|50|100)
# page (Integer) # page (Integer)
# filter (Array)
def orders(filter = nil, limit = 20, page = 1) def orders(filter = nil, limit = 20, page = 1)
url = "#{@rr_url}orders" url = "#{@rr_url}orders"
@rr_params[:limit] = limit @rr_params[:limit] = limit
@rr_params[:page] = page @rr_params[:page] = page
@rr_params[:filter] = filter @filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join("&")
make_request(url)
end
##
# === Get orders statuses
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.orders_statuses([26120, 19282])
# => {...}
#
# Arguments:
# ids (Array)
def orders_statuses(ids = [])
@ids = ids.map { |x| "ids[]=#{x}" }.join("&")
url = "#{@rr_url}orders/statuses"
make_request(url) make_request(url)
end end
@ -148,17 +162,22 @@ class Retailcrm
end end
## ##
# === Get orders statuses # === Get customers by filter
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3 # http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
# #
# Example: # Example:
# >> Retailcrm.orders_statuses([231,244,356,564]) # >> Retailcrm.customers({:email => 'test@example.com'}, 50, 2)
# => {...} # => {...}
# #
# Arguments: # Arguments:
# ids (Array) # filter (Hash)
def orders_statuses(ids) # limit (Integer) (20|50|100)
url = "#{@rr_url}orders/statuses/#{ids}" # page (Integer)
def customers(filter = nil, limit = 20, page = 1)
url = "#{@rr_url}customers"
@rr_params[:limit] = limit
@rr_params[:page] = page
@filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join("&")
make_request(url) make_request(url)
end end
@ -355,22 +374,30 @@ class Retailcrm
def make_request(url, method='get') def make_request(url, method='get')
raise ArgumentError, 'url must be not empty' unless !url.empty? raise ArgumentError, 'url must be not empty' unless !url.empty?
filter = nil
uri = URI.parse(url) uri = URI.parse(url)
https = Net::HTTP.new(uri.host, uri.port) https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true https.use_ssl = true
if method == 'post' if method == 'post'
request = Net::HTTP::Post.new(uri) request = Net::HTTP::Post.new(uri)
request.set_form_data(@rr_params) request.set_form_data(@rr_params)
elsif method == 'get' elsif method == 'get'
unless @rr_params[:filter].nil?
filter = @rr_params[:filter].to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join("&")
end
request = Net::HTTP::Get.new(uri.path) request = Net::HTTP::Get.new(uri.path)
request.set_form_data(@rr_params) request.set_form_data(@rr_params)
data = filter.nil? ? "#{request.body}" : "#{request.body}&#{filter}"
request = Net::HTTP::Get.new("#{uri.path}?#{data}")
if @filter.nil?
data = "#{request.body}"
else
data = "#{request.body}&#{@filter}"
end
if @ids.nil?
data = "#{request.body}"
else
data = "#{request.body}&#{@ids}"
end
request = Net::HTTP::Get.new("#{uri.path}?#{data}")
end end
response = https.request(request) response = https.request(request)
Retailcrm::Response.new(response.code, response.body) Retailcrm::Response.new(response.code, response.body)