Class: PrestaShop::API
- Inherits:
-
Object
- Object
- PrestaShop::API
- 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
-
#api_uri ⇒ String
readonly
URI of the Prestashop API.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#key ⇒ String
readonly
API key.
Instance Method Summary collapse
-
#get(resource, *args) ⇒ Object
# # THIS NEEDS TO BE A NEW REQUESTOR TYPE # # api.images.find(‘general/header’) << this works def image(*image_path) client[(+image_path).join(‘/’)].get.body end.
-
#initialize(url, key) ⇒ API
constructor
Create a new instance.
- #method_missing(method, *args, &block) ⇒ Object
-
#resources ⇒ Array<Symbol>
List resources that the API key can access.
Constructor Details
#initialize(url, key) ⇒ API
Create a new instance
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_uri ⇒ String (readonly)
Returns URI of the Prestashop API.
25 26 27 |
# File 'lib/presta_shop/api.rb', line 25 def api_uri @api_uri end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
29 30 31 |
# File 'lib/presta_shop/api.rb', line 29 def client @client end |
#key ⇒ String (readonly)
Returns 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
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 |
#resources ⇒ Array<Symbol>
List resources that the API key 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 |