<commit_before>//
//  ImageAPIService.swift
//  AbTastyDemo
//
//  Created by <NAME> on 9/3/15.
//  Copyright (c) 2015 ABTasty. All rights reserved.
//

import Foundation
import Alamofire
import PromiseKit

class ImageAPIService
{
    // MARK: - Request
    
    class func fetchImage(url: NSURL) -> Promise<NSData>
    {
        return Promise<NSData> { fullfil, reject in
            let imageRouter = ImageRouter.FetchImage(url)
            let request = Alamofire.request(imageRouter)
                .response() { (request, response, data, error) in
                    if let statusCode = response?.statusCode {
                        switch statusCode {
                        case 200...299:
                            if let imageData = data {
                                fullfil(imageData)
                            }
                            else {
                                let missingDatasError = ImageAPIError.MissingData(imageRouter).error
                                reject(missingDatasError)<commit_msg>Cache images with images URL. Fix #11.<commit_after>//
//  ImageAPIService.swift
//  AbTastyDemo
//
//  Created by <NAME> on 9/3/15.
//  Copyright (c) 2015 ABTasty. All rights reserved.
//

import Foundation
import Alamofire
import PromiseKit

class ImageCache: NSCache
{
    func cacheImageData(imageData: NSData, forURL url: NSURL)
    {
        if let urlString = url.absoluteString {
            self.setObject(imageData, forKey: urlString)
        }
    }
    
    func cachedImageData(forURL url: NSURL) -> NSData?
    {
        if let urlString = url.absoluteString {
            return self.objectForKey(urlString) as? NSData
        }
        return nil
    }
}

class ImageAPIService
{
    // MARK: - Property
    
    static let imageCache: ImageCache = ImageCache()
    
    // MARK: - Request
    
    class func fetchImage(url: NSURL, cacheImage: Bool = true) -> Promise<NSData>
    {
        return Promise<NSData> { fullfil, reject in

            if let cachedImageData = ImageAPIService.imageCache.cachedImageData(forURL: url) {
                fullfil(cachedImageData)
                return
            }

            let imageRouter = ImageRouter.FetchImage(url)
            let request = Alamofire.request(imageRouter)
                .response() { (request, response, data, error) in
                    if let statusCode = response?.statusCode {
                        switch statusCode {
                        case 200...299:
                            if let imageData = data {
                                ImageAPIService.imageCache.cacheImageData(imageData, forURL: url)
                                fullfil(imageData)
                            }
                            else {
                                let missingDatasError = ImageAPIError.MissingData(imageRouter).error
                                reject(missingDatasError)
{'fraction_non_alphanumeric': 0.07905544147843943, 'fraction_numerical': 0.01026694045174538, 'mean_word_length': 1.4940273037542662, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3829771', 'n_tokens_mistral': 795, 'n_tokens_neox': 743, 'n_words': 186}