Class: Prestashopper::API

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

Overview

Each instance represents a Prestashop API instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, key) ⇒ API

Create a new instance

Parameters:

  • url (String)

    base URL of the Prestashop installation. Do not append “/api” to it, the gem does it internally. E.g. use “my.prestashop.com”, not “my.prestashop.com/api

  • key (String)

    a valid API key



16
17
18
19
20
# File 'lib/prestashopper/api.rb', line 16

def initialize(url, key)
  @api_uri = UriHandler.api_uri url
  @key = key
  @resources_res = RestClient::Resource.new @api_uri, user: @key, password: ''
end

Instance Attribute Details

#api_uriString (readonly)

Returns URI of the Prestashop API.

Returns:

  • (String)

    URI of the Prestashop API



7
8
9
# File 'lib/prestashopper/api.rb', line 7

def api_uri
  @api_uri
end

#keyString (readonly)

Returns API key.

Returns:

  • (String)

    API key



10
11
12
# File 'lib/prestashopper/api.rb', line 10

def key
  @key
end

Instance Method Details

#get_productsArray<Hash>

Get all products data

Returns:

  • (Array<Hash>)

    list of products. Each product is represented by a hash with all its attributes.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/prestashopper/api.rb', line 35

def get_products
  # /api/products returns XML with the IDs of each individual product
  xml_products = @resources_res['products'].get.body
  xml_products_doc = Nokogiri::XML xml_products
  products_nodes = xml_products_doc.xpath '/prestashop/products/*/@id'
  ids_list = []
  products_nodes.each{|n| ids_list << n.value}

  # GET each individual product to get the whole data
  products = []
  ids_list.each do |id|
    xml_product = @resources_res["products/#{id}"].get.body
    product = Product.xml2hash xml_product
    products << product
  end

  return products
end

#resourcesArray<Symbol>

List resources that the API key can access

Returns:

  • (Array<Symbol>)

    list of resources the API can access



24
25
26
27
28
29
30
31
# File 'lib/prestashopper/api.rb', line 24

def resources
  xml = @resources_res.get.body
  xml_doc = Nokogiri::XML xml
  nodes = xml_doc.xpath '/prestashop/api/*'
  resources_list = []
  nodes.each{|n| resources_list << n.name.to_sym}
  return resources_list
end