Class: Prestashop::Api::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/prestashop/api/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_url) ⇒ Connection

Create new connection. Raise error, when is not possible validate connection from any reason

Prestashop::Api::Connection.new 'TOKEN342', 'mystore.com'

Raises:


15
16
17
18
19
20
# File 'lib/prestashop/api/connection.rb', line 15

def initialize api_key, api_url
  @api_key = api_key
  self.api_url = api_url

  raise InvalidCredentials unless self.test
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key


9
10
11
# File 'lib/prestashop/api/connection.rb', line 9

def api_key
  @api_key
end

#api_urlObject

Returns the value of attribute api_url


9
10
11
# File 'lib/prestashop/api/connection.rb', line 9

def api_url
  @api_url
end

Instance Method Details

#connectionObject

Create connection based on connection instance, returns Faraday::Connection which can be usedo for API call


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/prestashop/api/connection.rb', line 37

def connection
  Faraday.new do |builder|
    builder.url_prefix = api_url
    builder.request     :multipart
    builder.request     :url_encoded
    builder.request     :retry, 5
    # builder.response    :logger
    builder.adapter     :net_http
    builder.basic_auth  api_key, ''
  end
end

#delete(resource, id) ⇒ Object Also known as: destroy

Call DELETE on WebService API, returns true if was request successfull or raise error, when request failed.

delete :customer, 1 # => true

175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/prestashop/api/connection.rb', line 175

def delete resource, id
  raise ArgumentError, "resource: #{resource} must be string or symbol" unless resource.kind_of?(String) or resource.kind_of?(Symbol)
  raise ArgumentError, "id: #{id} must be integer" unless id.to_i.kind_of?(Integer)

  request_path = path(resource, id)
  response = connection.delete request_path
  if response.success?
    true # response.body
  else
    raise RequestFailed.new(response), response.body.parse_error
  end
rescue ParserError
  raise ParserError, "Response couldn't be parsed for: #{request_path}. RESPONSE: #{response.body} XML SENT: #{payload}"
end

#get(resource, id = nil, opts = {}) ⇒ Object Also known as: read

Call GET on WebService API, returns parsed Prestashop response or raise error, when request failed. Can be called as read instead get

get :customer, 1       # => {id: 1 ...}
read :customer, [1,2]    # => [{id: 1}, {id: 2}]
available options
  • filter

  • display

  • sort

  • limit

  • schema

  • date


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/prestashop/api/connection.rb', line 104

def get resource, id = nil, opts = {}
  id.to_i unless id.kind_of?(Array)
  raise ArgumentError, "resource: #{resource} must be string or symbol" unless resource.kind_of?(String) or resource.kind_of?(Symbol)
  raise ArgumentError, "id: #{id} must be integer, array or nil" unless id.kind_of?(Integer) or id.kind_of?(Array) or id == nil

  white_list = %w(filter display sort limit schema date)
  params = {}
  opts.each do |name, value|
    if white_list.include? name.to_s.split('[').first
      params[name.to_sym] = value
    end
  end 

  request_path = path(resource, id)
  response = connection.get request_path, params
  if response.success? 
    response.body.parse
  else
    raise RequestFailed.new(response), response.body.parse_error
  end
rescue ParserError
  raise ParserError, "Response couldn't be parsed for: #{request_path} with #{params}. RESPONSE: #{response.body}"
end

#head(resource, id = nil) ⇒ Object Also known as: check

Call HEAD on WebService API, returns true if was request successfull or raise error, when request failed. Can be called as check instead head

head :customer, 2 # => true
check :customer, 3 # => true

74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/prestashop/api/connection.rb', line 74

def head resource, id = nil
  raise ArgumentError, "resource: #{resource} must be string or symbol" unless resource.kind_of?(String) or resource.kind_of?(Symbol)
  raise ArgumentError, "id: #{id} must be integer or nil" unless id.to_i.kind_of?(Integer) or id == nil
  
  request_path = path(resource, id)
  response = connection.head request_path
  if response.success?
    true # response.body 
  else
    raise RequestFailed.new(response), response.body.parse_error
  end
rescue ParserError
  raise ParserError, "Response couldn't be parsed for: #{request_path}. RESPONSE: #{response.body}"
end

#path(resource, id = nil) ⇒ Object

Generate path for API request

path(:customers, 1) # => "customers/1"
path(:customers, [1, 5]) # => "customers?id=[1,5]"

54
55
56
57
58
# File 'lib/prestashop/api/connection.rb', line 54

def path resource, id = nil
  path = resource.to_s
  path += id.kind_of?(Array) ? "?id=[#{id.join(',')}]" : "/#{id}" if id
  path
end

#post(resource, payload) ⇒ Object Also known as: create

Call POST on WebService API, returns parsed Prestashop response if was request successfull or raise error, when request failed. Can be called as create insted of put

post :customer, { name: 'Steve' } # => true

134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/prestashop/api/connection.rb', line 134

def post resource, payload
  raise ArgumentError, "resource: #{resource} must be string or symbol" unless resource.kind_of?(String) or resource.kind_of?(Symbol)

  request_path = path(resource)
  response = connection.post request_path, payload
  if response.success? 
    response.body.parse
  else
    raise RequestFailed.new(response), "#{response.body.parse_error}. XML SENT: #{payload}"
  end
rescue ParserError
  raise ParserError, "Response couldn't be parsed for: #{request_path}. RESPONSE: #{response.body} XML SENT: #{payload}"
end

#put(resource, id, payload) ⇒ Object Also known as: update

Call PUT on WebService API, returns parsed Prestashop response if was request successfull or raise error, when request failed. Can be called as update instead put

put :customer, 1, {surname: 'Jobs'} # => true
update :customer, 1, {nope: 'Jobs'} # => false

155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/prestashop/api/connection.rb', line 155

def put resource, id, payload
  raise ArgumentError, "resource: #{resource} must be string or symbol" unless resource.kind_of?(String) or resource.kind_of?(Symbol)
  raise ArgumentError, "id: #{id} must be integer" unless id.to_i.kind_of?(Integer)

  request_path = path(resource, id)
  response = connection.put request_path, payload
  if response.success?
    response.body.parse
  else
    raise RequestFailed.new(response), "#{response.body.parse_error}. XML SENT: #{payload}"
  end
rescue ParserError
  raise ParserError, "Response couldn't be parsed for: #{request_path}. RESPONSE: #{response.body} XML SENT: #{payload}"
end

#testObject

Test connection based on current credentials and connection, return true or false, based if request was successfull or not.


220
221
222
# File 'lib/prestashop/api/connection.rb', line 220

def test
  connection.get.status == 200 ? true : false
end

#upload(type, resource, id, payload, file) ⇒ Object

Send file via payload After that call POST on WebService API, returns parsed Prestashop response if was request successfull or raise error, when request failed.

upload :image, :customer, 2, {image: '/file/to/path.png'}, file

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/prestashop/api/connection.rb', line 202

def upload type, resource, id, payload, file
  raise ArgumentError, "type: #{type} must be string or symbol" unless resource.kind_of?(String) or resource.kind_of?(Symbol)
  raise ArgumentError, "resource: #{resource} must be string or symbol" unless resource.kind_of?(String) or resource.kind_of?(Symbol)
  raise ArgumentError, "id: #{id} must be integer" unless id.to_i.kind_of?(Integer)

  request_path = upload_path(type, resource, id)
  response = connection.post request_path, payload
  file.destroy!
  if response.success?
    response.body.parse
  else
    raise RequestFailed.new(response), response.body.parse_error
  end
rescue ParserError
  raise ParserError, "Response couldn't be parsed for: #{request_path}. RESPONSE: #{response.body} XML SENT: #{payload}"
end

#upload_path(type, resource, id) ⇒ Object

Generate path for API upload request

upload_path :image, :products, 2 # => /images/products/2

64
65
66
# File 'lib/prestashop/api/connection.rb', line 64

def upload_path type, resource, id
  "#{type}/#{resource}/#{id}"
end