presta_shop 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9184be31c0226ec9a54ac046d76cb2db2976521
4
- data.tar.gz: b670e96d7029f9e2bf0050471d4beb85ef942c66
3
+ metadata.gz: 58a45d67672ef4cf07a754d5a1203c903fda79c2
4
+ data.tar.gz: 376dfe2a83ba2dee386108991eeb74ca64adacf1
5
5
  SHA512:
6
- metadata.gz: 5e97db2ce5574b95d60d73d7b14e324a16ac200ac01062f1691e7cff629eee35a7eeaff519e1c70a717e522e6e880880e749e88b84f7ecbaaf191a470189be4d
7
- data.tar.gz: 426886939f70dca08aa7cff65a12ff219cd23a15bda4bde58ff7622548061a0d040219e85f5e3dc0d83dc7d446a95fce26f16c08e82ddf8e2c48522368fee9bc
6
+ metadata.gz: 3a006fd6d9a34251bb030d920adf029167b156b8e797fe1c2be7b54a80dde41279186831613f382bbd39040438d67877a9e9a8ffacb7c3aedf5643d0554bea73
7
+ data.tar.gz: 52be8e038fa3d80c1c1713229cfeeda05ff1d178044373ce0b377335dd57c548ac15d419f53b036d16e910e9bc05829c39c4c900157e58de9241152caed331c3
data/.gitignore CHANGED
@@ -7,4 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
- /.idea/
10
+ /.idea/
11
+ *.gem
@@ -28,6 +28,14 @@ module PrestaShop
28
28
  attr_reader :key
29
29
  attr_reader :client
30
30
 
31
+ class << self
32
+ def normalize_resource(resource)
33
+ resource = resource.to_s.pluralize.to_sym
34
+ resource = resource.to_s.singularize.to_sym if [:order_slips, :content_management_systems].include?(resource)
35
+ resource
36
+ end
37
+ end
38
+
31
39
  # Create a new instance
32
40
  # @param url [String] base URL of the Prestashop installation. Do not append "/api" to it, the gem does it internally.
33
41
  # E.g. use "http://my.prestashop.com", not "http://my.prestashop.com/api"
@@ -62,7 +70,7 @@ module PrestaShop
62
70
  when Array then get_resources(resource, ids, params)
63
71
  when NilClass then get_resource_ids(resource, params)
64
72
  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}))
73
+ when /^blank$/ then generate_resource_object(resource, nil, params.merge({schema: :blank}), resource_class(resource))
66
74
  else get_resource(resource, ids, params, true)
67
75
  end
68
76
  end
@@ -92,18 +100,27 @@ module PrestaShop
92
100
  api: self, resource_name: resource, schema: generate_resource_object(resource, nil, {schema: :synopsis}))
93
101
  end
94
102
 
103
+ def resource_languages
104
+ @resource_languages ||= resources.include?(:languages) ? get(:languages).zip({}).to_h : {}
105
+ end
106
+
107
+ def resource_language(language_id)
108
+ resource_languages.include?(language_id) ? resource_languages[language_id] ||= get(:languages, language_id).name : language_id
109
+ end
110
+
95
111
  def schema(resource)
96
112
  resource_requestor(resource).schema
97
113
  end
98
114
 
99
115
  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
116
+ self.class.normalize_resource(resource)
103
117
  end
104
118
 
105
119
  def build_query_params(params)
106
- "?#{params.to_query}" unless params.none?
120
+ case
121
+ when params.include?(:schema) then "?#{params.to_query}"
122
+ when params.any? then "?date=1&#{params.map{ |k,v| ["filter[#{k}]", v] }.to_h.to_query}"
123
+ end
107
124
  end
108
125
 
109
126
  def extract_ids(args)
@@ -141,6 +158,7 @@ module PrestaShop
141
158
  xpath = XPATH_MAP.dig(resource, :find) || resource.to_s.singularize
142
159
  xml_response = Nokogiri::XML(client[resource][id][build_query_params(params)].get.body).remove_namespaces!.xpath("/prestashop/#{xpath}")
143
160
  resource_object = JSON.parse(Hash.from_xml(xml_response.to_s).values.first.to_json, object_class: object_class)
161
+ resource_object.xml = xml_response
144
162
  resource_object.schema_synopsis = object_schema
145
163
  resource_object
146
164
  end
@@ -1,8 +1,8 @@
1
1
  module PrestaShop
2
2
  class Resource < OpenStruct
3
- attr_reader :schema_synopsis
3
+ attr_reader :schema_synopsis, :xml
4
4
 
5
- def initialize
5
+ def initialize(*args)
6
6
  super
7
7
  cast_attribute_data_types_from_schema_synopsis if self.schema_synopsis.present?
8
8
  end
@@ -12,6 +12,10 @@ module PrestaShop
12
12
  cast_attribute_data_types_from_schema_synopsis
13
13
  end
14
14
 
15
+ def xml=(xml)
16
+ @xml = xml
17
+ end
18
+
15
19
  private
16
20
 
17
21
  def cast_attribute_data_types_from_schema_synopsis
@@ -27,15 +31,25 @@ module PrestaShop
27
31
  nested_schema_synopsis.try(:format) if nested_schema_synopsis.is_a? OpenStruct
28
32
  end
29
33
 
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
+ nested_attribute_data = self.dig(*nested_attribute)
35
+ if nested_attribute_data.class == self.class
36
+ nested_attribute_data.marshal_dump.keys.each { |attribute| cast_attribute_from_schema(attribute, nested_attribute) }
37
+ elsif nested_attribute_data.class == Array && ( nested_attribute_data.none? && nested_attribute_data.first.class == self.class )
38
+ nested_attribute_data.each_with_index do |_, i|
34
39
  array_nested_attribute = nested_attribute + [i]
35
40
  self.dig(*array_nested_attribute).marshal_dump.keys.each { |attribute| cast_attribute_from_schema(attribute, array_nested_attribute) }
36
41
  end
42
+ elsif nested_attribute_data.class == Array
43
+ # resource = PrestaShop::API.normalize_resource(self.class.name.demodulize.underscore)
44
+ # xpath = PrestaShop::API::XPATH_MAP.dig(resource, :find) || resource.to_s.singularize
45
+
46
+ # builds an array of nested objects
47
+ # could be better, but will work for now
48
+ segment = nested.any? ? self.dig(*nested) : self
49
+ segment[attribute] = xml.xpath(nested_attribute.join('/')).collect do |element|
50
+ self.class.new(element.collect.to_h.merge({nested.last || :name => Hash.from_xml(element.to_s).values.first}))
51
+ end
37
52
  else
38
- puts [self.class, self.id].inspect if value == '1' && format == 'isBool'
39
53
  segment = nested.any? ? self.dig(*nested) : self
40
54
  segment[attribute] = cast_value_from_schema_format(segment[attribute], format)
41
55
  end
@@ -56,6 +70,7 @@ module PrestaShop
56
70
  when 'isInt', 'isNullOrUnsignedId', 'isUnsignedId', 'isImageSize', 'isUnsignedInt'
57
71
  case value
58
72
  when nil, /null/i then nil
73
+ when /\D/ then value
59
74
  else value.to_i
60
75
  end
61
76
  when 'isSerializedArray'
@@ -1,3 +1,3 @@
1
1
  module PrestaShop
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: presta_shop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - chaunce