vendoo_v1.0/Vendoo/AmazonXmlBody.swift
2016-08-30 13:50:47 -04:00

76 lines
2.2 KiB
Swift

//
// AmazonXmlBody.swift
// Vendoo
//
// Created by cole alban on 8/29/16.
// Copyright © 2016 Okechi Onyeje. All rights reserved.
//
import Foundation
/*
This class will represent the xml body of an xml feed for Amazon
*/
class AmazonXmlBody{
private var listing: AmazonListing
private var xmlString: String
//Init will take in a listing and parse it into the xml string
init(listing: AmazonListing){
self.listing = listing
self.xmlString = parseListing(self.listing)
}
/*
This helper function will take in a listing and insert the pertinent
information into the respective locations.
listing: The listing to parse into the xml string
return: The xml string with all pertinent order information
*/
private func parseListing(listing: AmazonListing)->String{
var xmlTemplate = getXmlTemplate()
xmlTemplate.replace("{{target}}", replacement: self.listing.title)
xmlTemplate.replace("{{description}}", replacement: self.listing.description)
xmlTemplate.replace("{{price}}", replacement: self.listing.title)
return xmlTemplate
}
/*
This function will get the initial XML template
return: String representing the xml template
*/
private func getXmlTemplate()->String{
var xmlString: String
let manager = NSFileManager.defaultManager()
let path = manager.currentDirectoryPath+"/feedSubmissionTemplate.xml"
do{
xmlString = try{
NSString(contentsOfURL: path, encoding: String.encoding.utf8.rawValue) as String
}
return xmlString
}
catch{
return "Error"
}
}
}
//A small extension to give string a replace function with persistence
extenstion String{
/*
Replaces target with replacement in the string instance.
Target: The string to replace
replacement: The new string to insert
return: the new string with the replaced snippet
*/
func replace(target: String, replacement: String)->String{
self = self.stringByReplacingOccurrencesOfString(target, withString: replacement)
}
}