Class: Akeneo::API

Inherits:
Object
  • Object
show all
Includes:
Services
Defined in:
lib/akeneo/api.rb

Constant Summary collapse

MASTER_CATEGORY_CODE =
'master'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Services

#attribute_service, #authorization_service, #category_service, #family_service, #image_service, #measure_family_service, #media_files_service, #product_model_service, #product_service, #published_product_service

Constructor Details

#initialize(url:, client_id:, secret:, username:, password:) ⇒ API

Returns a new instance of API.



13
14
15
16
# File 'lib/akeneo/api.rb', line 13

def initialize(url:, client_id:, secret:, username:, password:)
  @url = url
  authorization_service.authorize!(client_id: client_id, secret: secret, username: username, password: password)
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



11
12
13
# File 'lib/akeneo/api.rb', line 11

def access_token
  @access_token
end

#last_refreshObject

Returns the value of attribute last_refresh.



11
12
13
# File 'lib/akeneo/api.rb', line 11

def last_refresh
  @last_refresh
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/akeneo/api.rb', line 11

def url
  @url
end

Instance Method Details

#attribute(code) ⇒ Object



104
105
106
# File 'lib/akeneo/api.rb', line 104

def attribute(code)
  attribute_service.find(code)
end

#attribute_option(code, option_code) ⇒ Object



108
109
110
# File 'lib/akeneo/api.rb', line 108

def attribute_option(code, option_code)
  attribute_service.option(code, option_code)
end

#brothers_and_sisters(code) ⇒ Object



100
101
102
# File 'lib/akeneo/api.rb', line 100

def brothers_and_sisters(code)
  product_service.brothers_and_sisters(code)
end

#categories(code, categories: []) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/akeneo/api.rb', line 120

def categories(code, categories: [])
  category = category_service.find(code)

  return [] if category.nil?

  categories << category

  return categories if category['parent'].nil? || category['parent'] == MASTER_CATEGORY_CODE

  categories(category['parent'], categories: categories)
end

#category(code) ⇒ Object



116
117
118
# File 'lib/akeneo/api.rb', line 116

def category(code)
  category_service.find(code)
end

#create_or_update_product(code:, options:) ⇒ Object



42
43
44
# File 'lib/akeneo/api.rb', line 42

def create_or_update_product(code:, options:)
  product_service.create_or_update(code, options)
end

#create_or_update_product_model(code:, options:) ⇒ Object



50
51
52
# File 'lib/akeneo/api.rb', line 50

def create_or_update_product_model(code:, options:)
  product_model_service.create_or_update(code, options)
end

#create_product(options:) ⇒ Object



46
47
48
# File 'lib/akeneo/api.rb', line 46

def create_product(options:)
  product_service.create(options)
end

#download_file(code) ⇒ Object



72
73
74
# File 'lib/akeneo/api.rb', line 72

def download_file(code)
  media_files_service.download(code)
end

#download_image(code) ⇒ Object



68
69
70
# File 'lib/akeneo/api.rb', line 68

def download_image(code)
  image_service.download(code)
end

#family(code) ⇒ Object



85
86
87
# File 'lib/akeneo/api.rb', line 85

def family(code)
  family_service.find(code)
end

#family_variant(family_code, family_variant_code) ⇒ Object



89
90
91
# File 'lib/akeneo/api.rb', line 89

def family_variant(family_code, family_variant_code)
  family_service.variant(family_code, family_variant_code)
end

#fresh_access_tokenObject



18
19
20
# File 'lib/akeneo/api.rb', line 18

def fresh_access_token
  authorization_service.fresh_access_token
end

#image(code) ⇒ Object



58
59
60
# File 'lib/akeneo/api.rb', line 58

def image(code)
  image_service.find(code)
end

#measure_family(code) ⇒ Object



112
113
114
# File 'lib/akeneo/api.rb', line 112

def measure_family(code)
  measure_family_service.find(code)
end

#option_values_of(family_code, family_variant_code) ⇒ Object



93
94
95
96
97
98
# File 'lib/akeneo/api.rb', line 93

def option_values_of(family_code, family_variant_code)
  family_variant = family_service.variant(family_code, family_variant_code)
  return [] unless family_variant

  [find_attribute_code_for_level(family_variant, 1), find_attribute_code_for_level(family_variant, 2)].compact
end

#parents(with_family: nil) ⇒ Object



38
39
40
# File 'lib/akeneo/api.rb', line 38

def parents(with_family: nil)
  product_model_service.all(with_family: with_family)
end

#product(code) ⇒ Object



22
23
24
# File 'lib/akeneo/api.rb', line 22

def product(code)
  product_service.find(code)
end

#product_parent(code) ⇒ Object



54
55
56
# File 'lib/akeneo/api.rb', line 54

def product_parent(code)
  product_model_service.find(code)
end

#product_parent_or_grand_parent(code) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/akeneo/api.rb', line 76

def product_parent_or_grand_parent(code)
  product_parent = product_model_service.find(code)

  return if product_parent.nil?
  return product_parent unless product_parent['parent']

  product_model_service.find(product_parent['parent'])
end

#products(with_family: nil, with_completeness: nil, updated_after: nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/akeneo/api.rb', line 26

def products(with_family: nil, with_completeness: nil, updated_after: nil)
  product_service.all(
    with_family: with_family,
    with_completeness: with_completeness,
    updated_after: updated_after
  )
end

#published_products(updated_after: nil) ⇒ Object



34
35
36
# File 'lib/akeneo/api.rb', line 34

def published_products(updated_after: nil)
  published_product_service.published_products(updated_after: updated_after)
end

#upload_image(code:, file:, filename:, options: {}) ⇒ Object



62
63
64
65
66
# File 'lib/akeneo/api.rb', line 62

def upload_image(code:, file:, filename:, options: {})
  locale = options.delete(:locale) || 'no-locale'
  image_service.create_asset(code, options)
  image_service.create_reference(code, locale, file, filename)
end