lib/apprank/app.rb in apprank-1.0.0 vs lib/apprank/app.rb in apprank-1.0.1
- old
+ new
@@ -1,63 +1,78 @@
module Apprank
class App
attr_accessor :name, :icon_urls, :summary, :price, :content_type, :rights, :title,
- :link, :itunes_url, :itunes_id, :bundle_id, :artist, :artist, :category, :release_date
+ :link, :preview, :itunes_url, :itunes_id, :bundle_id, :artist, :artist, :category, :release_date
alias_method :developer, :artist
def initialize(hash)
- @name = hash["im:name"]["label"]
- @icon_urls = {}
- hash["im:image"].each do |image|
- height =
- if image["attributes"]["height"] == '53'
- :small
- elsif image["attributes"]["height"] == '75'
- :medium
- else
- :large
- end
- @icon_urls[height] = image["label"]
- end
- @summary = hash["summary"]["label"]
+ @name = App.get_label(hash, "im:name")
+ @icon_urls = App.get_icon_urls(hash["im:image"])
+ @summary = App.get_label(hash, "summary")
+
@price = {
- :amount => hash["im:price"]["attributes"]["amount"].to_f,
- :currency => hash["im:price"]["attributes"]["currency"]
+ amount: App.get_attributes(hash, "im:price")["amount"].to_f,
+ currency: App.get_attributes(hash, "im:price")["currency"],
}
- @content_type = hash["rights"]["contentType"]
- @rights = hash["rights"]["label"]
- @title = hash["title"]["label"]
+ @content_type = App.get_attributes(hash, "im:contentType")["term"]
+ @rights = App.get_label(hash, "rights")
+ @title = App.get_label(hash, "title")
+ @link = App.get_attributes(hash, "link")["href"]
- link = hash["link"]
- if link["attributes"]["title"] == "Preview"
- @preview = link["attributes"]["href"]
- else
- @link = link["attributes"]["href"]
- end
+ @itunes_url = App.get_label(hash, "id")
+ @itunes_id = App.get_attributes(hash, "id")['im:id']
+ @bundle_id = App.get_attributes(hash, "id")['im:bundleId']
- @itunes_url = hash["id"]["label"]
- @itunes_id = hash["id"]['attributes']['im:id']
- @bundle_id = hash["id"]['attributes']['im:bundleId']
-
@artist = {
- :name => hash["im:artist"]["label"],
- :url => hash["im:artist"]["attributes"]["href"]
+ :name => App.get_label(hash, "im:artist"),
+ :url => App.get_attributes(hash, "im:artist")["href"],
}
+ category = App.get_attributes(hash, "category")
@category = {
- :name => hash["category"]["attributes"]["term"],
- :url => hash["category"]["attributes"]["scheme"]
+ :name => category["term"],
+ :url => category["scheme"],
}
- @release_date = Time.parse(hash["im:releaseDate"]["label"])
+ @release_date = Time.parse(App.get_label(hash, "im:releaseDate"))
end
def itunes_id
- self.itunes_url.split(/\//).last.split(/\?/).first.gsub(/[^\d]/,'')
+ self.itunes_url.split(/\//).last.split(/\?/).first.gsub(/[^\d]/, '')
end
+ def category_id
+ self.category[:url].split(/\//).last.split(/\?/).first.gsub(/[^\d]/, '')
+ end
+
def is_free?
self.price[:amount].zero?
+ end
+
+ private
+
+ def self.get_attributes(hash, field)
+ hash[field]["attributes"]
+ end
+
+ def self.get_label(hash, field)
+ hash[field]["label"]
+ end
+
+ def self.get_icon_urls(images)
+ icon_urls = {}
+ images.each do |image|
+ height =
+ if image["attributes"]["height"] == '53'
+ :small
+ elsif image["attributes"]["height"] == '75'
+ :medium
+ else
+ :large
+ end
+ icon_urls[height] = image["label"]
+ end
+ icon_urls
end
end
end