lib/establish/app_metadata.rb in establish-0.0.26 vs lib/establish/app_metadata.rb in establish-0.0.27
- old
+ new
@@ -1,10 +1,14 @@
require 'nokogiri'
module Establish
+ class AppMetadataError < StandardError
+
+ end
+
class AppMetadata
- APPLE_ITUNES_NAMESPACE = "http://apple.com/itunes/importer"
+ ITUNES_NAMESPACE = "http://apple.com/itunes/importer"
attr_accessor :metadata_dir
def transporter
@transporter ||= ItunesTransporter.new
@@ -26,13 +30,11 @@
# Updating metadata information
#####################################################
# Update the app description which is shown in the AppStore
def update_description(hash)
- raise "Please pass a hash of languages to this method" unless hash.kind_of?Hash
-
- # TODO: Implement
+ update_localized_value('description', hash)
end
#####################################################
# Uploading the updated metadata
@@ -42,16 +44,62 @@
def upload!
transporter.upload(@app, @app.get_metadata_directory)
end
private
- def modify_value(xpath, new_value)
- binding.pry
- @data.xpath("//x:#{xpath}", "x" => APPLE_ITUNES_NAMESPACE)
+ # Usage: '//x:keyword'
+ def fetch_value(xpath)
+ @data.xpath(xpath, "x" => ITUNES_NAMESPACE)
+ end
+
+ def update_localized_value(xpath_name, new_value)
+ raise "Please pass a hash of languages to this method" unless new_value.kind_of?Hash
+
+ fetch_value("//x:locale").each do |locale|
+ key = locale['name']
+ if new_value[key]
+ description_field = locale.search(xpath_name).first
+ if description_field.content != new_value[key]
+ description_field.content = new_value[key]
+ Helper.log.debug "Updated #{xpath_name} for locale #{locale}"
+ end
+ else
+ Helper.log.error "Could not find '#{xpath_name}' for #{key}. It was provided before. Not updating this value"
+ end
end
+ end
+ # Parses the metadata using nokogiri
def parse_package(path)
- @data ||= Nokogiri::XML(File.read("#{self.metadata_dir}/#{@app.apple_id}.itmsp/metadata.xml"))
+ @data ||= Nokogiri::XML(File.read("#{path}/#{@app.apple_id}.itmsp/metadata.xml"))
+ verify_package
+ clean_package
end
+
+ # Checks if there is a non live version available
+ # (a new version, or a new app)
+ def verify_package
+ versions = fetch_value("//x:version")
+
+ # TODO: This does not work for new apps
+ raise AppMetadataError.new("You have to create a new version before modifying the app metadata") if versions.count == 1
+
+ raise AppMetadataError.new("metadata_token is missing") if fetch_value("//x:metadata_token").count != 1
+ end
+
+ # Cleans up the package of stuff we do not want to modify/upload
+ def clean_package
+
+
+ # Remove the live version (if it exists)
+ versions = fetch_value("//x:version")
+ while versions.count > 1
+ versions.last.remove
+ versions = fetch_value("//x:version")
+ end
+ Helper.log.info "Modifying version '#{versions.first.attr('string')}' of app #{@app.app_identifier}"
+
+
+ end
end
-end
+end
\ No newline at end of file