presta_shop 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +57 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/presta_shop/api.rb +149 -0
- data/lib/presta_shop/requestor/image.rb +25 -0
- data/lib/presta_shop/requestor.rb +29 -0
- data/lib/presta_shop/resource.rb +78 -0
- data/lib/presta_shop/uri_handler.rb +26 -0
- data/lib/presta_shop/version.rb +3 -0
- data/lib/presta_shop.rb +68 -0
- data/presta_shop.gemspec +33 -0
- metadata +186 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f9184be31c0226ec9a54ac046d76cb2db2976521
|
4
|
+
data.tar.gz: b670e96d7029f9e2bf0050471d4beb85ef942c66
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5e97db2ce5574b95d60d73d7b14e324a16ac200ac01062f1691e7cff629eee35a7eeaff519e1c70a717e522e6e880880e749e88b84f7ecbaaf191a470189be4d
|
7
|
+
data.tar.gz: 426886939f70dca08aa7cff65a12ff219cd23a15bda4bde58ff7622548061a0d040219e85f5e3dc0d83dc7d446a95fce26f16c08e82ddf8e2c48522368fee9bc
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 0.2.0 (August 19, 2016)
|
4
|
+
|
5
|
+
Features:
|
6
|
+
|
7
|
+
- Added badges to README (gem version, build status, rubydoc.info docs)
|
8
|
+
- Get list of resources an API key can access
|
9
|
+
- Get all products as an array of hashes
|
10
|
+
|
11
|
+
## 0.1.0 (August 12, 2016)
|
12
|
+
|
13
|
+
Features:
|
14
|
+
|
15
|
+
- check if the API is enabled for a Prestashop instance
|
16
|
+
- check if an API key is valid for a Prestashop instance
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 chaunce
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# PrestaShop
|
2
|
+
|
3
|
+
PrestaShop is a ruby gem to interact with a [Prestashop API](http://doc.prestashop.com/display/PS16/Using+the+PrestaShop+Web+Service)
|
4
|
+
|
5
|
+
this gem was originally forked from [Prestashopper](https://github.com/amatriain/prestashopper)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
add `presta_shop` Gemfile
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'presta_shop'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
### verify PrestaShop api is enabled
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
PrestaShop.api_enabled? 'my.prestashop.com'
|
21
|
+
=> true
|
22
|
+
```
|
23
|
+
|
24
|
+
### check api key is valid
|
25
|
+
```
|
26
|
+
PrestaShop.valid_key? 'my.prestashop.com', 'VALID_KEY'
|
27
|
+
=> true
|
28
|
+
```
|
29
|
+
|
30
|
+
### create a PrestaShop api object
|
31
|
+
```
|
32
|
+
api = PrestaShop::API.new 'my.prestashop.com', 'VALID_KEY'
|
33
|
+
```
|
34
|
+
|
35
|
+
### list resources available for the api key
|
36
|
+
```
|
37
|
+
api.resources
|
38
|
+
=> [:customers, :orders, :products]
|
39
|
+
```
|
40
|
+
|
41
|
+
### get a list of ids for an available resource
|
42
|
+
```
|
43
|
+
order_ids = api.orders.list
|
44
|
+
=> [1, 2, 3, 4, 5, 6]
|
45
|
+
```
|
46
|
+
|
47
|
+
### get a specific resource by id
|
48
|
+
```
|
49
|
+
order = api.order.find(1)
|
50
|
+
=> #<PrestaShop::Order id=1, ...>
|
51
|
+
```
|
52
|
+
|
53
|
+
### get an array of resources
|
54
|
+
```
|
55
|
+
orders = api.orders.find(1, 2, 3)
|
56
|
+
=> [#<PrestaShop::Order id=1, ...>, #<PrestaShop::Order id=2, ...>, #<PrestaShop::Order id=3, ...>]
|
57
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'presta_shop'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
module PrestaShop
|
2
|
+
|
3
|
+
# Each instance represents a Prestashop API instance.
|
4
|
+
class API
|
5
|
+
XPATH_MAP = {
|
6
|
+
content_management_system: {
|
7
|
+
find: :content,
|
8
|
+
list: :content_management_system
|
9
|
+
},
|
10
|
+
product_customization_fields: {
|
11
|
+
find: :customization_field,
|
12
|
+
list: :customization_fields
|
13
|
+
},
|
14
|
+
stock_movements: {
|
15
|
+
find: :stock_mvt,
|
16
|
+
list: :stock_mvts
|
17
|
+
},
|
18
|
+
order_discounts: {
|
19
|
+
find: :order_cart_rule,
|
20
|
+
list: :order_cart_rules
|
21
|
+
},
|
22
|
+
}
|
23
|
+
|
24
|
+
# @return [String] URI of the Prestashop API
|
25
|
+
attr_reader :api_uri
|
26
|
+
|
27
|
+
# @return [String] API key
|
28
|
+
attr_reader :key
|
29
|
+
attr_reader :client
|
30
|
+
|
31
|
+
# Create a new instance
|
32
|
+
# @param url [String] base URL of the Prestashop installation. Do not append "/api" to it, the gem does it internally.
|
33
|
+
# E.g. use "http://my.prestashop.com", not "http://my.prestashop.com/api"
|
34
|
+
# @param key [String] a valid API key
|
35
|
+
def initialize(url, key)
|
36
|
+
@api_uri = UriHandler.api_uri url
|
37
|
+
@key = key
|
38
|
+
@client = RestClient::Resource.new api_uri, user: key, password: ''
|
39
|
+
end
|
40
|
+
|
41
|
+
# List resources that the API key can access
|
42
|
+
# @return [Array<Symbol>] list of resources the API can access
|
43
|
+
def resources
|
44
|
+
@resources ||= Nokogiri::XML(client.get.body).xpath('/prestashop/api/*').collect { |resource| resource.name.to_sym }
|
45
|
+
end
|
46
|
+
|
47
|
+
# # # THIS NEEDS TO BE A NEW REQUESTOR TYPE
|
48
|
+
# # # api.images.find('general/header') << this works
|
49
|
+
# def image(*image_path)
|
50
|
+
# client[([:images]+image_path).join('/')].get.body
|
51
|
+
# end
|
52
|
+
|
53
|
+
def get(resource, *args)
|
54
|
+
resource = normalize_resource(resource)
|
55
|
+
raise RestClient::MethodNotAllowed unless resources.include?(resource)
|
56
|
+
return image(*args) if resource == :images
|
57
|
+
|
58
|
+
ids = extract_ids(args)
|
59
|
+
params = extract_params(args)
|
60
|
+
|
61
|
+
case ids
|
62
|
+
when Array then get_resources(resource, ids, params)
|
63
|
+
when NilClass then get_resource_ids(resource, params)
|
64
|
+
when /^schema$/, /^synopsis$/ then generate_resource_object(resource, nil, params.merge({schema: :synopsis}))
|
65
|
+
when /^blank$/ then generate_resource_object(resource, nil, params.merge({schema: :blank}))
|
66
|
+
else get_resource(resource, ids, params, true)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def method_missing(method, *args, &block)
|
71
|
+
if resources.include?(resource = normalize_resource(method))
|
72
|
+
resource_requestor(resource)
|
73
|
+
else
|
74
|
+
super
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def resource_class(resource)
|
81
|
+
resource_class_name = resource.to_s.classify
|
82
|
+
"PrestaShop::#{resource_class_name}".safe_constantize || PrestaShop.const_set(resource_class_name, Class.new(PrestaShop::Resource))
|
83
|
+
end
|
84
|
+
|
85
|
+
def resource_requestors
|
86
|
+
@resource_requestors ||= resources.zip({}).to_h
|
87
|
+
end
|
88
|
+
|
89
|
+
def resource_requestor(resource)
|
90
|
+
resource = normalize_resource(resource)
|
91
|
+
resource_requestors[resource] ||= ("PrestaShop::Requestor::#{resource.to_s.classify}".safe_constantize || PrestaShop::Requestor).new(
|
92
|
+
api: self, resource_name: resource, schema: generate_resource_object(resource, nil, {schema: :synopsis}))
|
93
|
+
end
|
94
|
+
|
95
|
+
def schema(resource)
|
96
|
+
resource_requestor(resource).schema
|
97
|
+
end
|
98
|
+
|
99
|
+
def normalize_resource(resource)
|
100
|
+
resource = resource.to_s.pluralize.to_sym
|
101
|
+
resource = resource.to_s.singularize.to_sym if [:order_slips, :content_management_systems].include?(resource)
|
102
|
+
resource
|
103
|
+
end
|
104
|
+
|
105
|
+
def build_query_params(params)
|
106
|
+
"?#{params.to_query}" unless params.none?
|
107
|
+
end
|
108
|
+
|
109
|
+
def extract_ids(args)
|
110
|
+
ids = args.dup
|
111
|
+
ids.pop if ids.last.is_a? Hash
|
112
|
+
ids = ids.first if ids.one? || ids.none?
|
113
|
+
raise ArgumentError, 'invalid arguments' unless [NilClass, Numeric, String, Symbol, Array].any? { |klass| ids.is_a? klass }
|
114
|
+
raise ArgumentError, 'invalid arguments' unless Array(ids).all? { |id| [Numeric, String, Symbol].any? { |klass| id.is_a? klass } }
|
115
|
+
ids
|
116
|
+
end
|
117
|
+
|
118
|
+
def extract_params(args)
|
119
|
+
args.last.is_a?(Hash) ? args.last : {}
|
120
|
+
end
|
121
|
+
|
122
|
+
def get_resource_ids(resource, params)
|
123
|
+
xpath = XPATH_MAP.dig(resource, :list) || resource
|
124
|
+
Nokogiri::XML(client[resource][build_query_params(params)].get.body).xpath("/prestashop/#{xpath}/*/@id").collect(&:value)
|
125
|
+
end
|
126
|
+
|
127
|
+
def get_resource(resource, id, params, raise_not_found_exception = false)
|
128
|
+
begin
|
129
|
+
generate_resource_object(resource, id, params, resource_class(resource), schema(resource))
|
130
|
+
rescue RestClient::NotFound
|
131
|
+
raise if raise_not_found_exception
|
132
|
+
nil
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def get_resources(resource, ids, params = {})
|
137
|
+
ids.uniq.sort.collect { |id| get_resource(resource, id, params) }.compact
|
138
|
+
end
|
139
|
+
|
140
|
+
def generate_resource_object(resource, id, params = {}, object_class = OpenStruct, object_schema = nil)
|
141
|
+
xpath = XPATH_MAP.dig(resource, :find) || resource.to_s.singularize
|
142
|
+
xml_response = Nokogiri::XML(client[resource][id][build_query_params(params)].get.body).remove_namespaces!.xpath("/prestashop/#{xpath}")
|
143
|
+
resource_object = JSON.parse(Hash.from_xml(xml_response.to_s).values.first.to_json, object_class: object_class)
|
144
|
+
resource_object.schema_synopsis = object_schema
|
145
|
+
resource_object
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module PrestaShop
|
2
|
+
class Requestor
|
3
|
+
class Image < PrestaShop::Requestor
|
4
|
+
undef_method :schema, :synopsis, :blank, :search
|
5
|
+
|
6
|
+
def find(*path)
|
7
|
+
Raise NotImplementedError
|
8
|
+
# this one should raise exception if the response is not an image file
|
9
|
+
get(*path)
|
10
|
+
end
|
11
|
+
|
12
|
+
def list(*path)
|
13
|
+
Raise NotImplementedError
|
14
|
+
get(*args)
|
15
|
+
Nokogiri::XML(api.client[:images].get.body).xpath("/prestashop/image_types/*").collect(&:name)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def get(*args)
|
21
|
+
api.get(self.resource_name, *args)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module PrestaShop
|
2
|
+
class Requestor
|
3
|
+
include ActiveModel::Model
|
4
|
+
attr_accessor :api, :resource_name, :schema
|
5
|
+
alias_method :synopsis, :schema
|
6
|
+
|
7
|
+
def find(id, *args)
|
8
|
+
get(id, *args)
|
9
|
+
end
|
10
|
+
|
11
|
+
def list(*args)
|
12
|
+
get(*args)
|
13
|
+
end
|
14
|
+
|
15
|
+
def blank(*args)
|
16
|
+
get(:blank, *args)
|
17
|
+
end
|
18
|
+
|
19
|
+
def search(*args)
|
20
|
+
get(args.collect{ |k,v| ["filter[#{k}]", v] }.to_h.merge({date: 1}))
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def get(*args)
|
26
|
+
api.get(self.resource_name, *args)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module PrestaShop
|
2
|
+
class Resource < OpenStruct
|
3
|
+
attr_reader :schema_synopsis
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
super
|
7
|
+
cast_attribute_data_types_from_schema_synopsis if self.schema_synopsis.present?
|
8
|
+
end
|
9
|
+
|
10
|
+
def schema_synopsis=(schema)
|
11
|
+
@schema_synopsis = schema
|
12
|
+
cast_attribute_data_types_from_schema_synopsis
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def cast_attribute_data_types_from_schema_synopsis
|
18
|
+
self.marshal_dump.keys.each { |attribute| cast_attribute_from_schema(attribute) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def cast_attribute_from_schema(attribute, nested = [])
|
22
|
+
nested_attribute = nested + [attribute]
|
23
|
+
format = case attribute
|
24
|
+
when :id then 'isUnsignedId'
|
25
|
+
else
|
26
|
+
nested_schema_synopsis = @schema_synopsis.dig(*nested_attribute.reject{ |v| v.is_a? Integer })
|
27
|
+
nested_schema_synopsis.try(:format) if nested_schema_synopsis.is_a? OpenStruct
|
28
|
+
end
|
29
|
+
|
30
|
+
if self.dig(*nested_attribute).class == self.class
|
31
|
+
self.dig(*nested_attribute).marshal_dump.keys.each { |attribute| cast_attribute_from_schema(attribute, nested_attribute) }
|
32
|
+
elsif self.dig(*nested_attribute).class == Array
|
33
|
+
self.dig(*nested_attribute).each_with_index do |_, i|
|
34
|
+
array_nested_attribute = nested_attribute + [i]
|
35
|
+
self.dig(*array_nested_attribute).marshal_dump.keys.each { |attribute| cast_attribute_from_schema(attribute, array_nested_attribute) }
|
36
|
+
end
|
37
|
+
else
|
38
|
+
puts [self.class, self.id].inspect if value == '1' && format == 'isBool'
|
39
|
+
segment = nested.any? ? self.dig(*nested) : self
|
40
|
+
segment[attribute] = cast_value_from_schema_format(segment[attribute], format)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def cast_value_from_schema_format(value, schema_format)
|
45
|
+
case schema_format
|
46
|
+
when 'isBool'
|
47
|
+
case value
|
48
|
+
when '1' then true
|
49
|
+
else false
|
50
|
+
end
|
51
|
+
when 'isFloat', 'isPrice', 'isUnsignedFloat'
|
52
|
+
(value.to_f * 100).round / 100.0
|
53
|
+
when 'isNegativePrice'
|
54
|
+
formated_value = (value.to_f * 100).round / 100.0
|
55
|
+
formated_value.zero? ? formated_value : formated_value * -1
|
56
|
+
when 'isInt', 'isNullOrUnsignedId', 'isUnsignedId', 'isImageSize', 'isUnsignedInt'
|
57
|
+
case value
|
58
|
+
when nil, /null/i then nil
|
59
|
+
else value.to_i
|
60
|
+
end
|
61
|
+
when 'isSerializedArray'
|
62
|
+
value.to_s.split(';')
|
63
|
+
when 'isBirthDate', 'isDateFormat', 'isDate'
|
64
|
+
case value
|
65
|
+
when '0000-00-00', '0000-00-00 00:00:00', nil then nil
|
66
|
+
when /^\d{4}-\d{2}-\d{2}$/ then value.to_date
|
67
|
+
when /^\d{4}-\d{2}-\d{2} \d/ then value.to_datetime
|
68
|
+
else value.to_date
|
69
|
+
end
|
70
|
+
when 'isPercentage'
|
71
|
+
"#{value}%"
|
72
|
+
else
|
73
|
+
value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module PrestaShop
|
4
|
+
|
5
|
+
# Handle URIs, converting from base Prestashop URL to API URIs
|
6
|
+
class UriHandler
|
7
|
+
API_PATH = 'api/'
|
8
|
+
|
9
|
+
# Convert a base Prestashop URL to its API URI
|
10
|
+
# @param base_url [String] a Prestashop base URL. Do not append "/api", it is appended internally. E.g. use
|
11
|
+
# "http://my.prestashop.com/", not "http://my.prestashop.com/api". URIs without scheme (e.g. "my.prestashop.com")
|
12
|
+
# are assumed to be http:// by default.
|
13
|
+
# @return [String] the URI of the API for this Prestashop instance
|
14
|
+
def self.api_uri(base_url)
|
15
|
+
# Base URL must end in '/' so that we can properly append other path fragments for the API paths
|
16
|
+
base_url.strip!
|
17
|
+
base_url += '/' unless base_url[-1] == '/'
|
18
|
+
|
19
|
+
# Add http:// scheme if the scheme is missing from the base url
|
20
|
+
base_url = 'http://' + base_url if URI.parse(base_url).scheme.nil?
|
21
|
+
|
22
|
+
uri = URI.join base_url, API_PATH
|
23
|
+
return uri.to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/presta_shop.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_support/core_ext/string'
|
3
|
+
require 'active_support/core_ext/hash'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'rest-client'
|
6
|
+
|
7
|
+
require 'presta_shop/api'
|
8
|
+
require 'presta_shop/resource'
|
9
|
+
require 'presta_shop/requestor'
|
10
|
+
require 'presta_shop/requestor/image'
|
11
|
+
require 'presta_shop/uri_handler'
|
12
|
+
require 'presta_shop/version'
|
13
|
+
|
14
|
+
module PrestaShop
|
15
|
+
|
16
|
+
# Check if there is an enabled API in a Prestashop instance.
|
17
|
+
# @param url [String] base URL of the Prestashop installation. Do not append "/api" to it, the gem does it internally.
|
18
|
+
# E.g. use "http://my.prestashop.com", not "http://my.prestashop.com/api"
|
19
|
+
# @return [boolean] true if the API is enabled, false if it is disabled
|
20
|
+
# @raise [RestClient::Exception] if there is an error during HTTP GET
|
21
|
+
# @raise [StandardError] if a response different from 401 or 503 is received (not counting redirect responses,
|
22
|
+
# which will be followed)
|
23
|
+
def self.api_enabled?(url)
|
24
|
+
api_uri = UriHandler.api_uri url
|
25
|
+
res = RestClient::Resource.new api_uri, user: '', password: ''
|
26
|
+
begin
|
27
|
+
response = res.get
|
28
|
+
# We don't send an API key, so an HTTP error code should be returned. Execution shouldn't reach here normally.
|
29
|
+
raise StandardError.new "Expected 401 or 503 response from Prestashop API #{api_uri} without key, instead received <#{response.code}> #{response.body}"
|
30
|
+
rescue RestClient::Exception => e
|
31
|
+
if e.response.code == 401
|
32
|
+
return true # "Unauthorized" response means API is enabled
|
33
|
+
elsif e.response.code == 503
|
34
|
+
return false # "Service Unavailable" response means API is disabled
|
35
|
+
else
|
36
|
+
raise e # Any other HTTP error code means something is wrong
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Check if an API key is valid.
|
42
|
+
# @param url [String] base URL of the Prestashop installation. Do not append "/api" to it, the gem does it internally.
|
43
|
+
# E.g. use "http://my.prestashop.com", not "http://my.prestashop.com/api"
|
44
|
+
# @param key [String] the key to check
|
45
|
+
# @return [boolean] true if key is valid, false otherwise
|
46
|
+
# @raise [RestClient::Exception] if there is an error during HTTP GET
|
47
|
+
# @raise [StandardError] if a response different from 200 or 401 is received (not counting redirect responses,
|
48
|
+
# which will be followed)
|
49
|
+
def self.valid_key?(url, key)
|
50
|
+
api_uri = UriHandler.api_uri url
|
51
|
+
res = RestClient::Resource.new api_uri, user: key, password: ''
|
52
|
+
begin
|
53
|
+
response = res.get
|
54
|
+
if response.code == 200 # OK response means API key is valid
|
55
|
+
return true
|
56
|
+
else
|
57
|
+
raise StandardError.new "Expected 200 or 401 response from Prestashop API #{api_uri} with key #{key}, instead received <#{response.code}> #{response.body}"
|
58
|
+
end
|
59
|
+
rescue RestClient::Exception => e
|
60
|
+
if e.response.code == 401
|
61
|
+
return false # "Unauthorized" response means API key is invalid
|
62
|
+
else
|
63
|
+
raise e # Any other HTTP error code means something is wrong
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/presta_shop.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'presta_shop/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'presta_shop'
|
8
|
+
spec.version = PrestaShop::VERSION
|
9
|
+
spec.authors = ['chaunce']
|
10
|
+
spec.email = ['chaunce.slc@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{prestashop webservice api}
|
13
|
+
spec.homepage = 'https://github.com/chaunce/presta_shop'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 2.3.0'
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'rest-client', '~> 2.0'
|
24
|
+
spec.add_runtime_dependency 'nokogiri', '~> 1.6'
|
25
|
+
spec.add_runtime_dependency 'activesupport', '~> 5.0'
|
26
|
+
spec.add_runtime_dependency 'activemodel', '~> 5.0'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.0'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
31
|
+
spec.add_development_dependency 'yard', '~> 0.9'
|
32
|
+
spec.add_development_dependency 'webmock', '~> 2.1'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: presta_shop
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- chaunce
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activemodel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: minitest
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '5.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '5.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: yard
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.9'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.9'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: webmock
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '2.1'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '2.1'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- chaunce.slc@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- CHANGELOG.md
|
148
|
+
- Gemfile
|
149
|
+
- LICENSE
|
150
|
+
- README.md
|
151
|
+
- Rakefile
|
152
|
+
- bin/console
|
153
|
+
- bin/setup
|
154
|
+
- lib/presta_shop.rb
|
155
|
+
- lib/presta_shop/api.rb
|
156
|
+
- lib/presta_shop/requestor.rb
|
157
|
+
- lib/presta_shop/requestor/image.rb
|
158
|
+
- lib/presta_shop/resource.rb
|
159
|
+
- lib/presta_shop/uri_handler.rb
|
160
|
+
- lib/presta_shop/version.rb
|
161
|
+
- presta_shop.gemspec
|
162
|
+
homepage: https://github.com/chaunce/presta_shop
|
163
|
+
licenses:
|
164
|
+
- MIT
|
165
|
+
metadata: {}
|
166
|
+
post_install_message:
|
167
|
+
rdoc_options: []
|
168
|
+
require_paths:
|
169
|
+
- lib
|
170
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: 2.3.0
|
175
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - ">="
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: '0'
|
180
|
+
requirements: []
|
181
|
+
rubyforge_project:
|
182
|
+
rubygems_version: 2.5.1
|
183
|
+
signing_key:
|
184
|
+
specification_version: 4
|
185
|
+
summary: prestashop webservice api
|
186
|
+
test_files: []
|