Module: PlentyClient::Request

Instance Method Summary collapse

Instance Method Details

#delete(path, body = {}) ⇒ Object



28
29
30
# File 'lib/plenty_client/request.rb', line 28

def delete(path, body = {})
  request(:delete, path, body)
end

#get(path, params = {}, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plenty_client/request.rb', line 32

def get(path, params = {}, &block)
  page = 1
  rval_array = []
  if block_given?
    loop do
      response = request(:get, path, params.merge(page: page))
      yield response['entries'] if block_given?
      page += 1
      break if response['isLastPage'] == true
    end
  else
    rval_array << request(:get, path, params.merge(page: page))
  end
  rval_array.flatten
end

#patch(path, body = {}) ⇒ Object



24
25
26
# File 'lib/plenty_client/request.rb', line 24

def patch(path, body = {})
  request(:patch, path, body)
end

#post(path, body = {}) ⇒ Object



16
17
18
# File 'lib/plenty_client/request.rb', line 16

def post(path, body = {})
  request(:post, path, body)
end

#put(path, body = {}) ⇒ Object



20
21
22
# File 'lib/plenty_client/request.rb', line 20

def put(path, body = {})
  request(:put, path, body)
end

#request(http_method, path, params = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/plenty_client/request.rb', line 4

def request(http_method, path, params = {})
  return false if http_method.nil? || path.nil?
  return false unless %w(post put patch delete get).include?(http_method.to_s)

   if PlentyClient::Config.access_token.nil?
  start_time = Time.now
  response = perform(http_method, path, params)
  body = parse_body(response, http_method, path, params)
  log_output(http_method, base_url(path), params, time_diff_ms(start_time, Time.now)) if PlentyClient::Config.log
  body
end