Class: Paapi::Item

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/paapi/item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Item

Returns a new instance of Item.



10
11
12
# File 'lib/paapi/item.rb', line 10

def initialize(data)
  @hash = data
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



6
7
8
# File 'lib/paapi/item.rb', line 6

def hash
  @hash
end

Class Method Details

.to_items(data) ⇒ Object



157
158
159
# File 'lib/paapi/item.rb', line 157

def self.to_items(data)
  data.map {|d| Item.new(d)}
end

Instance Method Details

#actorsObject



61
62
63
# File 'lib/paapi/item.rb', line 61

def actors
  contributors_of 'actor'
end

#artistsObject



65
66
67
# File 'lib/paapi/item.rb', line 65

def artists
  contributors_of 'artist'
end

#asinObject



18
19
20
# File 'lib/paapi/item.rb', line 18

def asin
  get(['ASIN'])
end

#authorsObject



69
70
71
# File 'lib/paapi/item.rb', line 69

def authors
  contributors_of 'author'
end

#brandObject



113
114
115
# File 'lib/paapi/item.rb', line 113

def brand
  get(%w{ItemInfo ByLineInfo Brand DisplayValue})
end

#contributorsObject



52
53
54
# File 'lib/paapi/item.rb', line 52

def contributors
  Array(get(%w{ItemInfo ByLineInfo Contributors}))
end

#contributors_of(kind) ⇒ Object



56
57
58
59
# File 'lib/paapi/item.rb', line 56

def contributors_of(kind)
  kind = kind.to_s.gsub(/([[:alpha:]]+)/) { |w| w.downcase }
  contributors.select { |e| e['RoleType'] == kind }&.map { |e| e.dig('Name') }&.reject {|n| n.to_s.empty?}
end

#detail_urlObject



22
23
24
# File 'lib/paapi/item.rb', line 22

def detail_url
  get(['DetailPageURL'])
end

#directorObject



73
74
75
# File 'lib/paapi/item.rb', line 73

def director
  contributors_of 'director'
end

#eansObject



101
102
103
# File 'lib/paapi/item.rb', line 101

def eans
  get(%w{ItemInfo ExternalIds EANs DisplayValues})
end

#featuresObject



109
110
111
# File 'lib/paapi/item.rb', line 109

def features
  get(%w{ItemInfo Features DisplayValues})&.join(' ')
end

#get(keys) ⇒ Object



153
154
155
# File 'lib/paapi/item.rb', line 153

def get(keys)
  @hash.dig(*keys)
end

#heightObject



129
130
131
132
# File 'lib/paapi/item.rb', line 129

def height
  data = get(%w{ItemInfo ProductInfo ItemDimensions Height})
  [data&.dig('DisplayValue'), data&.dig('Unit')].join(' ')
end

#illustratorsObject



77
78
79
# File 'lib/paapi/item.rb', line 77

def illustrators
  contributors_of 'illustrator'
end

#image_urlObject



26
27
28
# File 'lib/paapi/item.rb', line 26

def image_url
  get(%w{Images Primary Large URL})
end

#kindle?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/paapi/item.rb', line 149

def kindle?
  !package.nil? && package&.include?('Kindle eBook')
end

#languagesObject



89
90
91
# File 'lib/paapi/item.rb', line 89

def languages
  get(%w{ItemInfo ContentInfo Languages DisplayValues})
end

#lengthObject



134
135
136
137
# File 'lib/paapi/item.rb', line 134

def length
  data = get(%w{ItemInfo ProductInfo ItemDimensions Length})
  [data&.dig('DisplayValue'), data&.dig('Unit')].join(' ')
end

#listingsObject



14
15
16
# File 'lib/paapi/item.rb', line 14

def listings
  get(['Offers', 'Listings'])&.map {|d| Listing.new(d)}
end

#manufacturerObject



34
35
36
# File 'lib/paapi/item.rb', line 34

def manufacturer
  get(%w{ItemInfo ByLineInfo Manufacturer DisplayValue})
end

#modelObject



121
122
123
# File 'lib/paapi/item.rb', line 121

def model
  get(%w{ItemInfo ManufactureInfo Model DisplayValue})
end

#narratorsObject



81
82
83
# File 'lib/paapi/item.rb', line 81

def narrators
  contributors_of 'narrator'
end

#original_languageObject



93
94
95
# File 'lib/paapi/item.rb', line 93

def original_language
  languages&.find {|l| l['Type'] == 'Original Language' }&.dig('DisplayValue')
end

#packageObject



125
126
127
# File 'lib/paapi/item.rb', line 125

def package
  get(%w{ItemInfo TechnicalInfo Formats DisplayValues})
end

#part_numberObject



117
118
119
# File 'lib/paapi/item.rb', line 117

def part_number
  get(%w{ItemInfo ManufactureInfo ItemPartNumber DisplayValue})
end

#publication_dateObject



42
43
44
45
# File 'lib/paapi/item.rb', line 42

def publication_date
  d = get(%w{ItemInfo ContentInfo PublicationDate DisplayValue})
  return Date.parse(d) rescue nil
end

#published_languageObject



97
98
99
# File 'lib/paapi/item.rb', line 97

def published_language
  languages&.find {|l| l['Type'] == 'Published' }&.dig('DisplayValue')
end

#publisherObject



38
39
40
# File 'lib/paapi/item.rb', line 38

def publisher
  manufacturer
end

#publishersObject



85
86
87
# File 'lib/paapi/item.rb', line 85

def publishers
  contributors_of 'publisher'
end

#release_dateObject



47
48
49
50
# File 'lib/paapi/item.rb', line 47

def release_date
  d = get(%w{ItemInfo ProductInfo ReleaseDate DisplayValue})
  return Date.parse(d) rescue nil
end

#titleObject



30
31
32
# File 'lib/paapi/item.rb', line 30

def title
  get(%w{ItemInfo Title DisplayValue})
end

#upcsObject



105
106
107
# File 'lib/paapi/item.rb', line 105

def upcs
  get(%w{ItemInfo ExternalIds UPCs DisplayValues})
end

#weightObject



144
145
146
147
# File 'lib/paapi/item.rb', line 144

def weight
  data = get(%w{ItemInfo ProductInfo ItemDimensions Weight})
  [data&.dig('DisplayValue'), data&.dig('Unit')].join(' ')
end

#widthObject



139
140
141
142
# File 'lib/paapi/item.rb', line 139

def width
  data = get(%w{ItemInfo ProductInfo ItemDimensions Width})
  [data&.dig('DisplayValue'), data&.dig('Unit')].join(' ')
end