Class: Prestashopper::API
- Inherits:
-
Object
- Object
- Prestashopper::API
- Defined in:
- lib/prestashopper/api.rb
Overview
Each instance represents a Prestashop API instance.
Instance Attribute Summary collapse
-
#api_uri ⇒ String
readonly
URI of the Prestashop API.
-
#key ⇒ String
readonly
API key.
Instance Method Summary collapse
-
#get_products ⇒ Array<Hash>
Get all products data.
-
#initialize(url, key) ⇒ API
constructor
Create a new instance.
-
#resources ⇒ Array<Symbol>
List resources that the API key can access.
Constructor Details
#initialize(url, key) ⇒ API
Create a new instance
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_uri ⇒ String (readonly)
Returns URI of the Prestashop API.
7 8 9 |
# File 'lib/prestashopper/api.rb', line 7 def api_uri @api_uri end |
#key ⇒ String (readonly)
Returns API key.
10 11 12 |
# File 'lib/prestashopper/api.rb', line 10 def key @key end |
Instance Method Details
#get_products ⇒ Array<Hash>
Get all products data
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 |
#resources ⇒ Array<Symbol>
List resources that the API key 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 |