api-client-ruby/lib/retailcrm.rb

605 lines
16 KiB
Ruby
Raw Normal View History

2015-04-13 03:14:25 +03:00
# encoding: utf-8
# noinspection RubyResolve
2015-04-13 03:14:25 +03:00
require 'net/http'
# noinspection RubyResolve
2015-04-13 03:14:25 +03:00
require 'net/https'
# noinspection RubyResolve
2015-04-13 03:14:25 +03:00
require 'uri'
# noinspection RubyResolve
2015-04-13 03:14:25 +03:00
require 'json'
# RetailCRM API Client
# noinspection ALL
2015-04-13 03:14:25 +03:00
class Retailcrm
def initialize(url, key)
@version = 3
@url = "#{url}/api/v#{@version}/"
@key = key
@params = { :apiKey => @key }
@filter = nil
@ids = nil
2015-04-13 18:07:40 +03:00
end
2015-04-13 03:14:25 +03:00
##
# === Get orders by filter
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.orders({:email => 'test@example.com', :status => 'new'}, 50, 2)
2015-04-13 03:14:25 +03:00
# => {...}
#
# Arguments:
# filter (Hash)
2015-04-13 03:14:25 +03:00
# limit (Integer) (20|50|100)
# page (Integer)
2015-04-13 18:07:40 +03:00
def orders(filter = nil, limit = 20, page = 1)
url = "#{@url}orders"
@params[:limit] = limit
@params[:page] = page
@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 = "#{@url}orders/statuses"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
##
# === Get orders by id (or externalId)
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.orders_get(345, 'id')
# => {...}
#
# Arguments:
# id (Integer)
# by (String)
2015-04-15 15:06:05 +03:00
# site (String)
def orders_get(id, by = 'externalId', site = nil)
url = "#{@url}orders/#{id}"
2015-04-13 18:07:40 +03:00
if by != 'externalId'
@params[:by] = by
2015-04-15 15:06:05 +03:00
@params[:site] = site
2015-04-13 03:14:25 +03:00
end
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
##
# === Create order
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.orders_create(order)
# => {...}
#
# Arguments:
# order (Array)
2015-04-15 15:06:05 +03:00
# site (String)
def orders_create(order, site = nil)
url = "#{@url}orders/create"
@params[:order] = order.to_json
2015-04-15 15:06:05 +03:00
@params[:site] = site
2015-04-13 18:07:40 +03:00
make_request(url, 'post')
2015-04-13 03:14:25 +03:00
end
##
# === Edit order
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.orders_edit(order)
# => {...}
#
# Arguments:
# order (Array)
2015-04-15 15:06:05 +03:00
# site (String)
def orders_edit(order, site = nil)
2015-04-13 03:14:25 +03:00
id = order[:externalId]
url = "#{@url}orders/#{id}/edit"
@params[:order] = order.to_json
2015-04-15 15:06:05 +03:00
@params[:site] = site
2015-04-13 18:07:40 +03:00
make_request(url, 'post')
2015-04-13 03:14:25 +03:00
end
##
# === Upload orders
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.orders_upload(orders)
# => {...}
#
# Arguments:
# orders (Array)
2015-04-15 15:06:05 +03:00
# site (String)
def orders_upload(orders, site = nil)
url = "#{@url}orders/upload"
@params[:orders] = orders.to_json
2015-04-15 15:06:05 +03:00
@params[:site] = site
2015-04-13 18:07:40 +03:00
make_request(url, 'post')
2015-04-13 03:14:25 +03:00
end
##
# === Set external ids for orders created into CRM
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.orders_fix_external_ids([{:id => 200, :externalId => 334}, {:id => 201, :externalId => 364}])
# => {...}
#
# Arguments:
# orders (Array)
def orders_fix_external_ids(orders)
url = "#{@url}orders/fix-external-ids"
@params[:orders] = orders.to_json
2015-04-13 18:07:40 +03:00
make_request(url, 'post')
2015-04-13 03:14:25 +03:00
end
##
# === Get orders history
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.orders_history('2015-04-10 22:23:12', '2015-04-10 23:33:12')
# => {...}
#
# Arguments:
2015-04-13 18:07:40 +03:00
# start_date (Time) (Time.strftime('%Y-%m-%d %H:%M:%S'))
# end_date (Time) (Time.strftime('%Y-%m-%d %H:%M:%S'))
2015-04-13 03:14:25 +03:00
# limit (Integer) (20|50|100)
# offset (Integer)
2015-04-13 18:07:40 +03:00
# skip_my_changes (Boolean)
def orders_history(start_date = nil, end_date = nil, limit = 100, offset = 0, skip_my_changes = true)
url = "#{@url}orders/history"
@params[:startDate] = start_date
@params[:endDate] = end_date
@params[:limit] = limit
@params[:offset] = offset
@params[:skipMyChanges] = skip_my_changes
make_request(url)
end
##
# === Get orders assembly history
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.orders_packs_history({:orderId => 26120, :startDate => '2015-04-10 23:33:12'}, 50, 2)
# => {...}
#
# Arguments:
# filter (Hash)
# limit (Integer) (20|50|100)
# page (Integer)
def orders_packs_history(filter = nil, limit = 20, page = 1)
url = "#{@url}orders/packs/history"
@params[:limit] = limit
@params[:page] = page
@filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join('&')
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
##
# === Get customers by filter
2015-04-13 03:14:25 +03:00
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.customers({:email => 'test@example.com'}, 50, 2)
2015-04-13 03:14:25 +03:00
# => {...}
#
# Arguments:
# filter (Hash)
# limit (Integer) (20|50|100)
# page (Integer)
def customers(filter = nil, limit = 20, page = 1)
url = "#{@url}customers"
@params[:limit] = limit
@params[:page] = page
@filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join('&')
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
##
# === Get customers by id (or externalId)
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.customers_get(345, 'id')
# => {...}
#
# Arguments:
# id (Integer)
# by (String)
2015-04-15 15:06:05 +03:00
# site (String)
def customers_get(id, by = 'externalId', site = nil)
url = "#{@url}customers/#{id}"
2015-04-15 15:06:05 +03:00
@params[:site] = site
2015-04-13 18:07:40 +03:00
if by != 'externalId'
@params[:by] = by
2015-04-13 03:14:25 +03:00
end
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
##
# === Create customer
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.customer_create(customer)
# => {...}
#
# Arguments:
# customer (Array)
2015-04-15 15:06:05 +03:00
# site (String)
def customers_create(customer, site = nil)
url = "#{@url}customers/create"
@params[:customer] = customer.to_json
2015-04-15 15:06:05 +03:00
@params[:site] = site
2015-04-13 18:07:40 +03:00
make_request(url, 'post')
2015-04-13 03:14:25 +03:00
end
##
# === Edit customer
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.customers_edit(customer)
# => {...}
#
# Arguments:
# customer (Array)
2015-04-15 15:06:05 +03:00
# site (String)
def customers_edit(customer, site = nil)
2015-04-13 18:07:40 +03:00
id = customer[:externalId]
url = "#{@url}customers/#{id}/edit"
@params[:customer] = customer.to_json
2015-04-15 15:06:05 +03:00
@params[:site] = site
2015-04-13 18:07:40 +03:00
make_request(url, 'post')
2015-04-13 03:14:25 +03:00
end
##
# === Upload customers
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.customers_upload(customers)
# => {...}
#
# Arguments:
# customers (Array)
2015-04-15 15:06:05 +03:00
# site (String)
def customers_upload(customers, site = nil)
url = "#{@url}customers/upload"
@params[:customers] = customers.to_json
2015-04-15 15:06:05 +03:00
@params[:site] = site
2015-04-13 18:07:40 +03:00
make_request(url, 'post')
2015-04-13 03:14:25 +03:00
end
##
# === Set external ids for customers created into CRM
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.customers_fix_external_ids([{:id => 200, :externalId => 334}, {:id => 201, :externalId => 364}])
# => {...}
#
# Arguments:
# customers (Array)
def customers_fix_external_ids(customers)
url = "#{@url}customers/fix-external-ids"
@params[:customers] = customers.to_json
2015-04-13 18:07:40 +03:00
make_request(url, 'post')
2015-04-13 03:14:25 +03:00
end
##
# === Get purchace prices & stock balance
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.store_inventories({:productExternalId => 26120, :details => 1}, 50, 2)
# => {...}
#
# Arguments:
# filter (Hash)
# limit (Integer) (20|50|100)
# page (Integer)
def store_inventories(filter = nil, limit = 20, page = 1)
url = "#{@url}store/inventories"
@params[:limit] = limit
@params[:page] = page
@filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join('&')
make_request(url)
end
2015-04-15 15:06:05 +03:00
##
# === Set purchace prices & stock balance
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
# Example:
# >> Retailcrm.store_inventories_upload({:offers => [{:externalId => 123, :stores => [{:code => 'store_1', :available => 15, :purchasePrice => 1000}]}]}, :site => 'main_site')
# => {...}
#
# Arguments:
# offers (Array)
# site (String)
def store_inventories_upload(offers = [], site = nil)
url = "#{@url}store/inventories/upload"
@params[:offers] = offers
@params[:site] = site
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
##
# === Get delivery services
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def delivery_services
url = "#{@url}reference/delivery-services"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit delivery service
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def delivery_services_edit(delivery_service)
code = delivery_service[:code]
url = "#{@url}reference/delivery-services/#{code}/edit"
@params[:deliveryService] = delivery_service.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
# Get delivery types
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def delivery_types
url = "#{@url}reference/delivery-types"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit delivery type
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def delivery_types_edit(delivery_type)
code = delivery_type[:code]
url = "#{@url}reference/delivery-types/#{code}/edit"
@params[:deliveryType] = delivery_type.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
##
# === Get order methods
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def order_methods
url = "#{@url}reference/order-methods"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit order method
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def order_methods_edit(order_method)
code = order_method[:code]
url = "#{@url}reference/order-methods/#{code}/edit"
@params[:orderMethod] = order_method.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
##
# === Get order types
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def order_types
url = "#{@url}reference/order-types"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit order type
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def order_types_edit(order_type)
code = order_type[:code]
url = "#{@url}reference/order-types/#{code}/edit"
@params[:orderType] = order_type.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
# Get payment statuses
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def payment_statuses
url = "#{@url}reference/payment-statuses"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit payment status
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def payment_statuses_edit(payment_status)
code = payment_status[:code]
url = "#{@url}reference/payment-statuses/#{code}/edit"
@params[:paymentStatus] = payment_status.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
##
# === Get payment types
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def payment_types
url = "#{@url}reference/payment-types"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit payment type
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def payment_types_edit(payment_type)
code = payment_type[:code]
url = "#{@url}reference/payment-types/#{code}/edit"
@params[:paymentType] = payment_type.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
##
# === Get product statuses
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def product_statuses
url = "#{@url}reference/product-statuses"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit product status
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def product_statuses_edit(product_status)
code = product_status[:code]
url = "#{@url}reference/product-statuses/#{code}/edit"
@params[:productStatus] = product_status.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
# Get sites list
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def sites
url = "#{@url}reference/sites"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit site
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def sites_edit(site)
code = site[:code]
url = "#{@url}reference/sites/#{code}/edit"
@params[:site] = site.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
##
# === Get status groups
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def status_groups
url = "#{@url}reference/status-groups"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
# Get statuses
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def statuses
url = "#{@url}reference/statuses"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit status
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def statuses_edit(status)
code = status[:code]
url = "#{@url}reference/statuses/#{code}/edit"
@params[:status] = status.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
##
# === Get stores
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def stores
url = "#{@url}reference/stores"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
2015-04-15 15:06:05 +03:00
##
# === Edit store
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def stores_edit(store)
code = store[:code]
url = "#{@url}reference/stores/#{code}/edit"
@params[:store] = store.to_json
2015-04-15 15:06:05 +03:00
make_request(url, 'post')
end
2015-04-13 03:14:25 +03:00
##
# === Statistic update
# http://www.retailcrm.ru/docs/Разработчики/СправочникМетодовAPIV3
#
def statistic_update
url = "#{@url}statistic/update"
2015-04-13 18:07:40 +03:00
make_request(url)
2015-04-13 03:14:25 +03:00
end
protected
def make_request(url, method='get')
raise ArgumentError, 'url must be not empty' unless !url.empty?
uri = URI.parse(url)
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
2015-04-13 03:14:25 +03:00
if method == 'post'
request = Net::HTTP::Post.new(uri)
request.set_form_data(@params)
2015-04-13 03:14:25 +03:00
elsif method == 'get'
request = Net::HTTP::Get.new(uri.path)
request.set_form_data(@params)
data = "#{request.body}"
2015-04-13 18:07:40 +03:00
unless @filter.nil?
data = data + "&#{@filter}"
end
unless @ids.nil?
data = data + "&#{@ids}"
end
request = Net::HTTP::Get.new("#{uri.path}?#{data}")
2015-04-13 03:14:25 +03:00
end
response = https.request(request)
2015-04-13 18:07:40 +03:00
Retailcrm::Response.new(response.code, response.body)
2015-04-13 03:14:25 +03:00
end
end
class Retailcrm::Response
2015-04-13 18:07:40 +03:00
attr_reader :status, :response
2015-04-13 03:14:25 +03:00
def initialize(status, body)
@status = status
2015-04-13 18:07:40 +03:00
@response = body.empty? ? [] : JSON.parse(body)
2015-04-13 03:14:25 +03:00
end
2015-04-13 18:07:40 +03:00
2015-04-13 03:14:25 +03:00
def is_successfull?
2015-04-13 18:07:40 +03:00
@status.to_i < 400
2015-04-13 03:14:25 +03:00
end
end