<commit_before>//
//  Store.swift
//  SwiftGoal
//
//  Created by <NAME> on 10/05/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation
import ReactiveCocoa
import Argo

class Store: NSObject {

    private static let baseURL = NSURL(string: "http://localhost:3000/api/v1/")!
    private static let matchesURL = NSURL(string: "matches", relativeToURL: baseURL)!
    private static let playersURL = NSURL(string: "players", relativeToURL: baseURL)!

    // MARK: - Matches

    func fetchMatches() -> SignalProducer<[Match], NoError> {
        let request = NSURLRequest(URL: Store.matchesURL)
        return NSURLSession.sharedSession().rac_dataWithRequest(request)
            |> map { data, response in
                let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil)
                if let j: AnyObject = json, matches: [Match] = decode(j) {
                    return matches
                } else {
                    return []
                }
            }
            |> catch { _ in SignalProducer<[Match], NoError>.empty }
    }

    func createMatch(#homePlayers: Set<Player>, awayPlayers: Set<Player>, homeGoals: Int, awayGoals: Int) -> SignalProducer<Bool, NoError> {

        let request = NSMutableURLRequest(URL: Store.matchesURL)
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.HTTPMethod = "POST"
        request.HTTPBody = httpBodyForParameters([
            "home_player_ids": Array(homePlayers).map { $0.identifier },
            "away_player_ids": Array(awayPlayers).map { $0.identifier },
            "home_goals": homeGoals,
            "away_goals": awayGoals
        ])

        return NSURLSession.sharedSession().rac_dataWithRequest(request)
            |> map { data, response in
                if let httpResponse = response as? NSHTTPURLResponse {
                    return httpResponse.statusCode == 201
                } else {
                    return false
                }
            }
            |> catch { _ in
                return SignalProducer<Bool, NoError>(value: false)
            }
    }

    // MARK: Players

    func fetchPlayers() -> SignalProducer<[Player], NoError> {
        let request = NSURLRequest(URL: Store.playersURL)
        return NSURLSession.sharedSession().rac_dataWithRequest(request)
            |> map { data, response in
                let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil)
                if let j: AnyObject = json, players: [Player] = decode(j) {
                    return players
                } else {
                    return []
                }
            }
            |> catch { _ in SignalProducer<[Player], NoError>.empty }
    }

    // MARK: Internal Helpers

    private func httpBodyForParameters(parameters: [String: AnyObject]) -> NSData? {
        return NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: nil)
    }
}<commit_msg>Allow deletion of matches through store<commit_after>//
//  Store.swift
//  SwiftGoal
//
//  Created by <NAME> on 10/05/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation
import ReactiveCocoa
import Argo

class Store: NSObject {

    private static let baseURL = NSURL(string: "http://localhost:3000/api/v1/")!
    private static let matchesURL = NSURL(string: "matches", relativeToURL: baseURL)!
    private static let playersURL = NSURL(string: "players", relativeToURL: baseURL)!

    // MARK: - Matches

    func fetchMatches() -> SignalProducer<[Match], NoError> {
        let request = NSURLRequest(URL: Store.matchesURL)
        return NSURLSession.sharedSession().rac_dataWithRequest(request)
            |> map { data, response in
                let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil)
                if let j: AnyObject = json, matches: [Match] = decode(j) {
                    return matches
                } else {
                    return []
                }
            }
            |> catch { _ in SignalProducer<[Match], NoError>.empty }
    }

    func createMatch(#homePlayers: Set<Player>, awayPlayers: Set<Player>, homeGoals: Int, awayGoals: Int) -> SignalProducer<Bool, NoError> {

        let request = NSMutableURLRequest(URL: Store.matchesURL)
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.HTTPMethod = "POST"
        request.HTTPBody = httpBodyForParameters([
            "home_player_ids": Array(homePlayers).map { $0.identifier },
            "away_player_ids": Array(awayPlayers).map { $0.identifier },
            "home_goals": homeGoals,
            "away_goals": awayGoals
        ])

        return NSURLSession.sharedSession().rac_dataWithRequest(request)
            |> map { data, response in
                if let httpResponse = response as? NSHTTPURLResponse {
                    return httpResponse.statusCode == 201
                } else {
                    return false
                }
            }
            |> catch { _ in
                return SignalProducer<Bool, NoError>(value: false)
            }
    }

    func deleteMatch(match: Match) -> SignalProducer<Bool, NoError> {
        let request = NSMutableURLRequest(URL: Store.matchesURL.URLByAppendingPathComponent(match.identifier))
        request.HTTPMethod = "DELETE"

        return NSURLSession.sharedSession().rac_dataWithRequest(request)
            |> map { data, response in
                if let httpResponse = response as? NSHTTPURLResponse {
                    return httpResponse.statusCode == 200
                } else {
                    return false
                }
            }
            |> catch { _ in
                return SignalProducer<Bool, NoError>(value: false)
            }
    }

    // MARK: Players

    func fetchPlayers() -> SignalProducer<[Player], NoError> {
        let request = NSURLRequest(URL: Store.playersURL)
        return NSURLSession.sharedSession().rac_dataWithRequest(request)
            |> map { data, response in
                let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil)
                if let j: AnyObject = json, players: [Player] = decode(j) {
                    return players
                } else {
                    return []
                }
            }
            |> catch { _ in SignalProducer<[Player], NoError>.empty }
    }

    // MARK: Internal Helpers

    private func httpBodyForParameters(parameters: [String: AnyObject]) -> NSData? {
        return NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: nil)
    }
}
{'fraction_non_alphanumeric': 0.1031154014918824, 'fraction_numerical': 0.006874360099458827, 'mean_word_length': 2.1011337868480724, 'pattern_counts': {'":': 8, '<': 25, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1179956', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1787, 'n_words': 434}