Class: PrestaShop::API

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

Overview

Each instance represents a Prestashop API instance.

Constant Summary collapse

XPATH_MAP =
{
  content_management_system: {
    find: :content,
    list: :content_management_system
  },
  product_customization_fields: {
    find: :customization_field,
    list: :customization_fields
  },
  stock_movements: {
    find: :stock_mvt,
    list: :stock_mvts
  },
  order_discounts: {
    find: :order_cart_rule,
    list: :order_cart_rules
  },
}

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



35
36
37
38
39
# File 'lib/presta_shop/api.rb', line 35

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/presta_shop/api.rb', line 70

def method_missing(method, *args, &block)
  if resources.include?(resource = normalize_resource(method))
    resource_requestor(resource)
  else
    super
  end
end

Instance Attribute Details

#api_uriString (readonly)

Returns URI of the Prestashop API.

Returns:

  • (String)

    URI of the Prestashop API



25
26
27
# File 'lib/presta_shop/api.rb', line 25

def api_uri
  @api_uri
end

#clientObject (readonly)

Returns the value of attribute client.



29
30
31
# File 'lib/presta_shop/api.rb', line 29

def client
  @client
end

#keyString (readonly)

Returns API key.

Returns:

  • (String)

    API key



28
29
30
# File 'lib/presta_shop/api.rb', line 28

def key
  @key
end

Instance Method Details

#get(resource, *args) ⇒ Object

# # THIS NEEDS TO BE A NEW REQUESTOR TYPE # # api.images.find(‘general/header’) << this works def image(*image_path)

client[([:images]+image_path).join('/')].get.body

end

Raises:

  • (RestClient::MethodNotAllowed)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/presta_shop/api.rb', line 53

def get(resource, *args)
  resource = normalize_resource(resource)
  raise RestClient::MethodNotAllowed unless resources.include?(resource)
  return image(*args) if resource == :images

  ids = extract_ids(args)
  params = extract_params(args)

  case ids
  when Array then get_resources(resource, ids, params)
  when NilClass then get_resource_ids(resource, params)
  when /^schema$/, /^synopsis$/ then generate_resource_object(resource, nil, params.merge({schema: :synopsis}))
  when /^blank$/ then generate_resource_object(resource, nil, params.merge({schema: :blank}))
  else get_resource(resource, ids, params, true)
  end
end

#resourcesArray<Symbol>

List resources that the API key can access

Returns:

  • (Array<Symbol>)

    list of resources the API can access



43
44
45
# File 'lib/presta_shop/api.rb', line 43

def resources
  @resources ||= Nokogiri::XML(client.get.body).xpath('/prestashop/api/*').collect { |resource| resource.name.to_sym }
end