Class: Opera::MobileStore::Product

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Serialization, InspectableAttributes, Opera::MobileStoreSDK::APIAccessible
Defined in:
lib/opera/mobile_store/product.rb

Constant Summary collapse

FIELDS =
[
  :id, :code, :category_id, :cp_product_id, :app_type, :author_id,
  :support_url, :version, :requirements, :price, :adult_content, :rating,
  :currency, :product_type, :weight, :updated_at, :added_at, :keywords,
  :rating, :images, :eula, :subsites, :builds, :i18n,
  :released_at,    # Release Date
  :download_count # Download count at Opera Mobile Store
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InspectableAttributes

#inspect

Class Method Details

.build_from_nokogiri_node(node) ⇒ Object

TODO: Move this implementation to the SDK namespace



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/opera/mobile_store/product.rb', line 128

def self.build_from_nokogiri_node(node)

  # Initialize the category unless it's already in it's Identity Map:
  category_id = if (some_id = node.xpath 'number(category/@id)') && !some_id.nan?
    some_id.to_i
  end

  Category.new(
    id: category_id,
    code: node.xpath("string(category/@code)"),
    name: node.xpath("string(category)")
  ) unless category_id.blank? || Category.identity_mapped?(category_id)

  # Initialize the category unless it's already in it's Identity Map:
  author_id = node.xpath('number(author/@id)').to_i
  
  Developer.new(
    id: author_id,
    name: node.xpath("string(author)"),
    email: node.xpath("string(author_email)")
  ) unless author_id.blank? || Developer.identity_mapped?(author_id)

  data = {
    id:   node.xpath("number(@id)").to_i,
    code: node.xpath("string(@code)"),
    cp_product_id: node.xpath("number(cp_product_id)").to_i,

    category_id: category_id,
    author_id: author_id,

    app_type: node.xpath("string(apptype)"),
    released_at: DateTime.parse(node.xpath "string(release_date)"),
    download_count: node.xpath("number(downloads_count)").to_i,
    support_url: node.xpath("string(support_url)"),
    version: node.xpath("string(version)"),
    # TODO: process requirements node

    # Product localization in English:
    i18n: [
      ProductLocalization.new({
        language_code: "en",
        title: node.xpath("string(product_name)").strip,
        short_description: node.xpath("string(short_description)").strip,
        long_description: node.xpath("string(long_description)").strip
      }.select { |key, val| val.present? })
    ],

    price: node.xpath("number(price)"),
    currency: node.xpath("string(currency)"),
    product_type: node.xpath("string(type)"),
    weight: node.xpath("number(weight)").to_i,
    updated_at: DateTime.parse(node.xpath "string(update_date)"),
    added_at: DateTime.parse(node.xpath "string(add_date)"),

    keywords: node.xpath("keywords/keyword").map do |x|
      value = x.text.strip
      value.present? ? Opera::MobileStoreSDK.html_entities.decode(value) : nil
    end.compact,

    images: node.xpath("images/*").map do |i|
      ProductImage.build_from_nokogiri_node i
    end,

    builds: node.xpath("builds/build").map do |b|
      Build.build_from_nokogiri_node b
    end

  }

  node.xpath("translates/language").each do |language_node|
    data[:i18n] << ProductLocalization.new({
      # We'll ignore the "@code" attribute... only Opera Devs knows WTF with it.
      language_code: language_node.xpath("string(@iso)").strip,
      title: node.xpath("string(title)").strip,
      short_description: node.xpath("string(short_description)").strip,
      long_description: node.xpath("string(description)").strip
    }.select { |key, val| val.present? })
  end

  self.new data
end

.deserialize(serializable_hash) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/opera/mobile_store/product.rb', line 97

def self.deserialize(serializable_hash)
  attributes_hash = serializable_hash.inject({}) do |hsh, keyval|
    field_name, field_value = keyval

    case field_name
    when 'category'
      field_value = Category.deserialize field_value
    when 'author'
      field_value = Developer.deserialize field_value
    when 'i18n'
      field_value = field_value.map do |item_serializable_hash|
        ProductLocalization.deserialize item_serializable_hash
      end
    when 'images'
      field_value = field_value.map do |item_serializable_hash|
        ProductImage.deserialize item_serializable_hash
      end
    when 'builds' # Array of special objects
      field_value = field_value.map do |item_serializable_hash|
        Build.deserialize item_serializable_hash
      end
    end

    hsh[field_name] = field_value
    hsh
  end

  self.new attributes_hash
end

Instance Method Details

#attributesObject



66
67
68
69
70
71
72
# File 'lib/opera/mobile_store/product.rb', line 66

def attributes
  FIELDS.inject({}) do |hsh, field_name|
    field_value = self.public_send field_name
    hsh[field_name.to_s] = field_value if field_value.present?
    hsh
  end
end

#authorObject



33
34
35
# File 'lib/opera/mobile_store/product.rb', line 33

def author
  Developer.find author_id if author_id.present?
end

#author=(given_author) ⇒ Object



37
38
39
# File 'lib/opera/mobile_store/product.rb', line 37

def author=(given_author)
  @author_id = given_author.id
end

#available_language_codesObject



62
63
64
# File 'lib/opera/mobile_store/product.rb', line 62

def available_language_codes
  i18n.map(&:language_code)
end

#categoryObject



25
26
27
# File 'lib/opera/mobile_store/product.rb', line 25

def category
  Category.find category_id if category_id.present?
end

#category=(given_category) ⇒ Object



29
30
31
# File 'lib/opera/mobile_store/product.rb', line 29

def category=(given_category)
  @category_id = given_category.id
end

#long_description(key = "en") ⇒ Object



55
56
57
58
59
60
# File 'lib/opera/mobile_store/product.rb', line 55

def long_description(key = "en")
  required_data = i18n.detect do |x|
    x.language_code == key.to_s
  end.long_description
  required_data.present? ? required_data : long_description('en')
end

#serializable_hash(options = nil) ⇒ Object

Override of serializable_hash:

In the case of the category and author of a product, since we can’t query the OMS API for a single entity directly, we’ll replace the entity id field (category_id, author_id) with the serializable hash of the actual object (category, author) as part of the serializable hash:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/opera/mobile_store/product.rb', line 80

def serializable_hash(options = nil)
  attributes.inject({}) do |shsh, keyval|
    field_name, field_value = keyval

    case field_name
    when 'category_id', 'author_id'
      field_name = field_name[0..-4]
      field_value = self.send(field_name).serializable_hash
    when 'i18n', 'images', 'builds' # Array of special objects
      field_value = field_value.map(&:serializable_hash)
    end

    shsh[field_name] = field_value
    shsh
  end
end

#short_description(key = "en") ⇒ Object



48
49
50
51
52
53
# File 'lib/opera/mobile_store/product.rb', line 48

def short_description(key = "en")
  required_data = i18n.detect do |x|
    x.language_code == key.to_s
  end.short_description
  required_data.present? ? required_data : short_description('en')
end

#title(key = "en") ⇒ Object



41
42
43
44
45
46
# File 'lib/opera/mobile_store/product.rb', line 41

def title(key = "en")
  required_data = i18n.detect do |x|
    x.language_code == key.to_s
  end.title
  required_data.present? ? required_data : title('en')
end