Class: TecDoc::Article

Inherits:
Object
  • Object
show all
Defined in:
lib/tec_doc/article.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, scope = {}) ⇒ Article

Returns a new instance of Article.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tec_doc/article.rb', line 81

def initialize(attributes = {}, scope = {})
  article_data = (attributes[:direct_article] || attributes)
  
  @id                 = article_data[:article_id].to_i
  @name               = article_data[:article_name].to_s
  @number             = article_data[:article_no].to_s
  @brand_name         = article_data[:brand_name].to_s
  @brand_number       = article_data[:brand_no].to_i
  @generic_article_id = article_data[:generic_article_id].to_i
  @number_type        = article_data[:number_type].to_i
  @search_number      = article_data[:article_search_no].to_s
  @state              = article_data[:article_state_name]
  @scope              = scope

  @ean_number   = attributes[:ean_number].to_a.map(&:values).flatten.first
  @trade_number = attributes[:usage_numbers].to_a.map(&:values).flatten.first
  
  if attributes[:article_attributes]
    @attributes = attributes[:article_attributes].map do |attrs|
      ArticleAttribute.new(attrs)
    end
  end

  if attributes[:oen_numbers]
    @oe_numbers = attributes[:oen_numbers].map do |attrs|
      ArticleOENumber.new(attrs)
    end
  end
end

Instance Attribute Details

#brand_nameObject

Returns the value of attribute brand_name.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def brand_name
  @brand_name
end

#brand_numberObject

Returns the value of attribute brand_number.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def brand_number
  @brand_number
end

#generic_article_idObject

Returns the value of attribute generic_article_id.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def generic_article_id
  @generic_article_id
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def name
  @name
end

#numberObject

Returns the value of attribute number.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def number
  @number
end

#number_typeObject

Returns the value of attribute number_type.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def number_type
  @number_type
end

#packing_unitObject

Returns the value of attribute packing_unit.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def packing_unit
  @packing_unit
end

#scopeObject

Returns the value of attribute scope.



6
7
8
# File 'lib/tec_doc/article.rb', line 6

def scope
  @scope
end

#search_numberObject

Returns the value of attribute search_number.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def search_number
  @search_number
end

#stateObject

Returns the value of attribute state.



3
4
5
# File 'lib/tec_doc/article.rb', line 3

def state
  @state
end

Class Method Details

.all(options) ⇒ Array<TecDoc::Article>

All articles by linked vehicle and brand number, generic article and assembly group

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :lang (String)
  • :country (String)
  • :linking_target_type (String)
  • :linking_target_id (Integer)
  • :brand_id (LongList)
  • :generic_article_id (LongList)
  • :article_assembly_group_node_id (Long)

Returns:



42
43
44
45
46
47
48
49
50
# File 'lib/tec_doc/article.rb', line 42

def self.all(options)
  options = {
    :lang => I18n.locale.to_s,
    :country => TecDoc.client.country
  }.merge(options)
  TecDoc.client.request(:get_article_ids3, options).map do |attributes|
    new(attributes, options)
  end
end

.all_with_details(options) ⇒ Array<TecDoc::Article>

All requested articles detail information

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :lang (String)
  • :country (String)
  • :ids (Array)
  • :attributs (Boolean)
  • :ean_numbers (Boolean)
  • :info (Boolean)
  • :oe_numbers (Boolean)
  • :usage_numbers (Boolean)

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tec_doc/article.rb', line 64

def self.all_with_details(options)
  options = {
    :lang => I18n.locale.to_s,
    :country => TecDoc.client.country,
    :attributs => true,
    :ean_numbers => true,
    :info => true,
    :oe_numbers => true,
    :usage_numbers => true,
    :article_id => { :array => { :ids => options.delete(:ids) } }
  }.merge(options)

  TecDoc.client.request(:get_direct_articles_by_ids2, options).map do |attributes|
    new(attributes, options)
  end
end

.search(options = {}) ⇒ Array<TecDoc::Article>

Find article by all number types and brand number and generic article id.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :article_number (String)

    Article number will be converted within the search process to a simplified article number

  • :brand_no (Integer)

    result of brand selection (optional)

  • :country (String)

    country code according to ISO 3166

  • :generic_article_id (Integer)

    result of generic article selection (optional)

  • :lang (String)

    language code according to ISO 639

  • :number_type (String)

    Number type (0: Article number, 1: OE number, 2: Trade number, 3: Comparable number, 4: Replacement number, 5: Replaced number, 6: EAN number, 10: Any number)

  • :search_exact (TrueClass, FalseClass)

    Search mode (true: exact search, false: similar search)

  • :sort_type (Integer)

    Sort mode (1: Brand, 2: Product group)

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tec_doc/article.rb', line 19

def self.search(options = {})
  options = {
    :country => TecDoc.client.country,
    :lang => I18n.locale.to_s,
    :number_type => 10,
    :search_exact => 1,
    :sort_type => 1
  }.merge(options)
  TecDoc.client.request(:get_article_direct_search_all_numbers2, options).map do |attributes|
    new(attributes, options)
  end
end

Instance Method Details

#attributesObject



140
141
142
143
144
# File 'lib/tec_doc/article.rb', line 140

def attributes
  @attributes ||= article_details[:article_attributes].map do |attrs|
    ArticleAttribute.new(attrs)
  end
end

#brandObject



119
120
121
122
123
124
125
126
# File 'lib/tec_doc/article.rb', line 119

def brand
  unless defined?(@brand)
    @brand = Brand.new
    @brand.number = brand_number
    @brand.name = brand_name
  end
  @brand
end

#documentsObject



128
129
130
131
132
133
134
# File 'lib/tec_doc/article.rb', line 128

def documents
  @documents ||= ArticleDocument.all({
    :lang => scope[:lang],
    :country => scope[:country],
    :article_id => id
  })
end

#ean_numberObject



146
147
148
# File 'lib/tec_doc/article.rb', line 146

def ean_number
  @ean_number ||= article_details[:ean_number].map(&:values).flatten.first
end

#informationObject



160
161
162
# File 'lib/tec_doc/article.rb', line 160

def information
  @information ||= direct_article_data_en[:article_info]
end

#linked_manufacturersObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/tec_doc/article.rb', line 164

def linked_manufacturers
  unless @linked_manufacturers
    response = TecDoc.client.request(:get_article_linked_all_linking_target_manufacturer, {
      :country => scope[:country],
      :linking_target_type => "C",
      :article_id => id
    })
    
    @linked_manufacturers = response.map do |attrs|
      VehicleManufacturer.new(attrs)
    end
  end
  @linked_manufacturers
end

#linked_targetsObject

Array with all linked target vehicles with article link id If request entity is too large, we have to request linked targets by manufacturers



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/tec_doc/article.rb', line 181

def linked_targets
  unless @linked_targets
    options = {
      :lang => scope[:lang],
      :country => scope[:country],
      :linking_target_type => "C",
      :linking_target_id => -1,
      :article_id => id
    }
    begin
      @linked_targets = TecDoc.client.request(:get_article_linked_all_linking_target_2, options)
    rescue
      @linked_targets = linked_manufacturers.inject([]) do |result, manufacturer|
        options[:linking_target_manu_id] = manufacturer.id
        result += TecDoc.client.request(:get_article_linked_all_linking_target_2, options)
      end
    end
  end
  @linked_targets
end

#linked_vehicle_idsObject



202
203
204
# File 'lib/tec_doc/article.rb', line 202

def linked_vehicle_ids
  @linked_vehicle_ids ||= linked_targets.map{ |attrs| attrs[:linking_target_id].to_i }.uniq
end

#linked_vehicles(options = {}) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/tec_doc/article.rb', line 206

def linked_vehicles(options = {})
  unless @linked_vehicles
    batch_list = linked_vehicle_ids.each_slice(25).to_a
    
    # Response from all batches
    response = batch_list.inject([]) do |result, long_list|
      result += TecDoc.client.request(:get_vehicle_by_ids_2,
        {:car_ids => {
          :array => {:ids => long_list}
        },
        :lang => scope[:lang],
        :country => scope[:country],
        :country_user_setting => scope[:country],
        :countries_car_selection => scope[:country],
        :motor_codes => true,
        :axles => false,
        :cabs => false,
        :secondary_types => false,
        :vehicle_details2 => true,
        :vehicle_terms => false,
        :wheelbases => false
      })
      result
    end
    
    @linked_vehicles = response.map do |attrs|
      details = (attrs[:vehicle_details2] || {})
      vehicle = Vehicle.new
      vehicle.id = attrs[:car_id].to_i
      vehicle.name = "#{details[:manu_name]} - #{details[:model_name]} - #{details[:type_name]}"
      vehicle.power_hp_from             = details[:power_hp].to_i
      vehicle.power_kw_from             = details[:power_kw].to_i
      vehicle.cylinder_capacity         = details[:cylinder_capacity_ccm].to_i
      vehicle.date_of_construction_from = DateParser.new(details[:year_of_construction_from]).to_date
      vehicle.date_of_construction_to   = DateParser.new(details[:year_of_construction_to]).to_date
      vehicle.motor_codes               = attrs[:motor_codes].map { |mc| mc[:motor_code] }
      vehicle
    end
  end
  @linked_vehicles
end

#linked_vehicles_with_details(options = {}) ⇒ Object

Linked vehicles for article with car specific attributes



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/tec_doc/article.rb', line 249

def linked_vehicles_with_details(options = {})
  unless @linked_vehicles_with_details
    links = linked_targets.map do |link|
      new_link = link.dup
      new_link.delete(:linking_target_type)
      new_link
    end
    batch_list = links.each_slice(25).to_a
    
    # Response from all batches
    response = batch_list.inject([]) do |result, long_list|
      result += TecDoc.client.request(:get_article_linked_all_linking_targets_by_ids_2, {
        :linked_article_pairs => { :array => {:pairs => long_list} },
        :lang => scope[:lang],
        :country => scope[:country],
        :linking_target_type => "C",
        :immediate_attributs => true,
        :article_id => id
      })
      result
    end

    @linked_vehicles_with_details = response.map do |attrs|
      details = (attrs[:linked_vehicles].to_a[0] || {})
      vehicle                           = Vehicle.new
      vehicle.id                        = details[:car_id].to_i
      vehicle.name                      = "#{details[:manu_desc]} - #{details[:model_desc]} - #{details[:car_desc]}"
      vehicle.manu_id                   = details[:manu_id].to_i
      vehicle.mod_id                    = details[:model_id].to_i
      vehicle.power_hp_from             = details[:power_hp_from].to_i
      vehicle.power_kw_from             = details[:power_kw_from].to_i
      vehicle.power_hp_to               = details[:power_hp_to].to_i
      vehicle.power_kw_to               = details[:power_kw_to].to_i
      vehicle.cylinder_capacity         = details[:cylinder_capacity].to_i
      vehicle.date_of_construction_from = DateParser.new(details[:year_of_construction_from]).to_date
      vehicle.date_of_construction_to   = DateParser.new(details[:year_of_construction_to]).to_date
      vehicle.attributes                = attrs[:linked_article_immediate_attributs].to_a
      vehicle.article_link_id           = attrs[:article_link_id].to_i
      vehicle
    end
  end
  @linked_vehicles_with_details
end

#oe_numbersObject



150
151
152
153
154
# File 'lib/tec_doc/article.rb', line 150

def oe_numbers
  @oe_numbers ||= article_details[:oen_numbers].map do |attrs|
    ArticleOENumber.new(attrs)
  end
end

#thumbnailsObject



136
137
138
# File 'lib/tec_doc/article.rb', line 136

def thumbnails
  @thumbnails ||= ArticleThumbnail.all(:article_id => id)
end

#trade_numbersObject



156
157
158
# File 'lib/tec_doc/article.rb', line 156

def trade_numbers
  @trade_numbers ||= article_details[:usage_numbers].map(&:values).flatten.join(", ")
end