Class: ApiBucket::Itunes::Item

Inherits:
Base::Item show all
Defined in:
lib/api_bucket/itunes/item.rb

Instance Attribute Summary

Attributes inherited from Base::Item

#availablity, #description, #detail_url, #image, #image_l, #image_m, #image_s, #preview_url, #price, #product_code, #release_date, #title

Instance Method Summary collapse

Methods inherited from Base::Item

#adult?, #hash_all

Constructor Details

#initialize(element) ⇒ Item

Returns a new instance of Item.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/api_bucket/itunes/item.rb', line 6

def initialize(element)

  # product_code
  if element['wrapperType'] == 'collection'
    @product_code = element['collectionId']
  else
    if element['trackId']
      @product_code = element['trackId']
    elsif element['collectionId']
      @product_code = element['collectionId']
    else
      matches = element['trackViewUrl'].match(/\/id([0-9]+)/)
      @product_code = matches[1]
    end
  end
  @product_code = @product_code.to_s

  # title
  if element['collectionName']
    @title = element['collectionName']
  elsif element['trackName']
    @title = element['trackName']
  else
    @title = element['artistName']
  end

  # detail_url
  if element['collectionViewUrl']
    url = element['collectionViewUrl']
  elsif element['trackViewUrl']
    url = element['trackViewUrl']
  else
    url = element['artistViewUrl']
  end
  @detail_url = url

  # contents
  @description = element['description']

  # price
  if element['collectionPrice']
    @price = element['tcollectionPrice']
  elsif element['trackPrice']
    @price = element['trackPrice']
  else
    @price = element['price']
  end

  # release date
  @release_date = element['release_date']

  # image
  @image = {}
  if element.key?('artworkUrl512')
    %w(s m l).collect {|key| @image[:"#{key}"] = {url: element['artworkUrl512'], width: 512, height: 512}}
  elsif element.key?('artworkUrl100')
    %w(s m l).collect {|key| @image[:"#{key}"] = {url: element['artworkUrl100'], width: 100, height: 100}}
  else
    %w(s m l).collect {|key| @image[:"#{key}"] = {url: element['artworkUrl60'], width: 60, height: 60}}
  end

  # PV
  @preview_url = element['previewUrl']
end