Class: BrightpearlApi::Service

Inherits:
Object
  • Object
show all
Includes:
Contact, Order, Product, Warehouse
Defined in:
lib/brightpearl_api/service.rb,
lib/brightpearl_api/services/order.rb,
lib/brightpearl_api/services/contact.rb,
lib/brightpearl_api/services/product.rb,
lib/brightpearl_api/services/warehouse.rb

Defined Under Namespace

Modules: Contact, Order, Product, Warehouse

Instance Method Summary collapse

Methods included from Warehouse

included

Methods included from Product

included

Methods included from Order

included

Methods included from Contact

included

Constructor Details

#initializeService

Returns a new instance of Service.



13
14
15
# File 'lib/brightpearl_api/service.rb', line 13

def initialize
  raise BrightpearlException, "Configuration is invalid" unless Configuration.instance.valid?
end

Instance Method Details

#call(type, path, data = {}) ⇒ Object



17
18
19
# File 'lib/brightpearl_api/service.rb', line 17

def call(type, path, data = {})
  Client.instance.call(type, path, data)
end

#create_resource(service, resource) {|body| ... } ⇒ Object

Yields:

  • (body)


34
35
36
37
38
# File 'lib/brightpearl_api/service.rb', line 34

def create_resource(service, resource)
  body = {}
  yield(body)
  call(:post, "/#{service}-service/#{resource}/", body)
end

#delete_resource(service, resource, resource_id) ⇒ Object



55
56
57
# File 'lib/brightpearl_api/service.rb', line 55

def delete_resource(service, resource, resource_id)
  call(:delete, "/#{service}-service/#{resource}/#{resource_id.to_i}")
end

#get_resource(service, resource, idset = nil) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/brightpearl_api/service.rb', line 40

def get_resource(service, resource, idset = nil)
  if !idset.nil?
    id_set = parse_idset(idset)
    call(:get, "/#{service}-service/#{resource}/#{id_set}")
  else
    call(:get, "/#{service}-service/#{resource}")
  end
end

#get_resource_range(service, resource, idset = nil) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/brightpearl_api/service.rb', line 59

def get_resource_range(service, resource, idset = nil)
  if !idset.nil?
    id_set = parse_idset(idset)
    call(:options, "/#{service}-service/#{resource}/#{id_set}")
  else
    call(:options, "/#{service}-service/#{resource}")
  end
end

#multi_message {|body| ... } ⇒ Object

Yields:

  • (body)


93
94
95
96
97
# File 'lib/brightpearl_api/service.rb', line 93

def multi_message
  body = {}
  yield(body)
  call(:post, "/multi-message", body)
end

#parse_idset(idset) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/brightpearl_api/service.rb', line 21

def parse_idset(idset)
  id_set = nil
  case idset
  when Range
    id_set = "#{idset.min.to_i}-#{idset.max.to_i}"
  when Array
    id_set = idset.collect{ |x| x.to_i }.join(',')
  else
    id_set = idset
  end
  id_set
end

#search_resource(service, resource) {|body| ... } ⇒ Object

Yields:

  • (body)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/brightpearl_api/service.rb', line 68

def search_resource(service, resource)
  body = {}
  yield(body)
  body[:pageSize] = 500
  body[:firstResult] = 1
  result_hash = []
  results_returned = 0
  results_available = 1
  while results_returned < results_available
    response = call(:get, "/#{service}/#{resource}-search?#{body.to_query}")
    results_returned += response['metaData']['resultsReturned']
    results_available = response['metaData']['resultsAvailable']
    body[:firstResult] = results_returned + 1
    properties = response['metaData']['columns'].map { |x| x['name'] }
    response['results'].each do |result|
      hash = {}
      properties.each_with_index do |item, index|
        hash[item] = result[index]
      end
      result_hash << hash
    end
  end
  result_hash
end

#update_resource(service, resource, resource_id) {|body| ... } ⇒ Object

Yields:

  • (body)


49
50
51
52
53
# File 'lib/brightpearl_api/service.rb', line 49

def update_resource(service, resource, resource_id)
  body = {}
  yield(body)
  call(:put, "/#{service}-service/#{resource}/#{resource_id.to_i}", body)
end