<commit_before># Kuggle API Swift class
Class to interface with the Kuggle backend

This is a work in progress.

## Sample code to query

### Instantiating this class
```swift
  var kuggle = KuggleAPI.sharedInstance
```

### Registration using facebook
#### Parameters
* 'fbtoken' - This is the token returned from the *OAUTH Dance* from the application. This endpoint will validate the users profile and decide whether or not to create it.

#### Expected Response
* 'fromfb' - This is what the app has pulled from facebook. Which contains a 'birthday' (Javascript Date format), 'birthdaystring' (format which you can actually serialize back into a javascript date object), 'firstname', 'gender' (0 = female, 1 = male, 2 = other or undisclosed), 'id' (the users facebook ID)
* 'token' - This is the login token that you need to use on protected endpoints. Only returned if registration was successful. Check NSError and display error.domain to user (this response is localized if you're using this API)

#### Other things to note
* Profile is automatically created. No need to call the create profile endpoint.

```swift
        k.postRequest("register", token: nil, params: ["fbtoken": "<PASSWORD>"], postRequestCompletionHandler: {json,err -> Void in
            if (err == nil) {
                let meta = (json as! NSDictionary)["meta"] as! NSDictionary
                let metaCode = meta.objectForKey("code") as! NSInteger
                let metaMsg = meta.objectForKey("msg") as! String
                println(metaCode)
                println(metaMsg)
                println(json)
            } else {
                if let error : NSError = err as NSError!
                {
                    println(error.code)
                    println(error.domain)
                }
            }

        })
```

### Registration by Phone Number

* replace '+11234567' with a phone number thats valid.

This only returns a 'meta' key. 200 is always good. Its automatically read and returns in NSError format. error.code is the HTTP code, and error.domain is the error message (this is also localized depending on the users phone settings)

```swift
  kuggle.postRequest("test", token: nil, params: ["phonenumber": "+11234567"], postRequestCompletionHandler: {json,err -> Void in
    if (err == nil) {
      let meta = (json as! NSDictionary)["meta"] as! NSDictionary
      let metaCode = meta.objectForKey("code") as! NSInteger
      let metaMsg = meta.objectForKey("msg") as! String
      println(metaCode)
      println(metaMsg)
      println(json)
    } else {
      if let error : NSError = err as NSError!
      {
        println(error.code)
        println(error.domain)
      }
    }
  })
```

### Verify PIN

* replace '+11234567' with a phone number thats valid.
* replace '12345' with a PIN thats valid

This functionality, returns the 'meta' key, as well as 'token' if successfully validated.

```swift
  kuggle.postRequest("test", token: nil, params: ["phonenumber": "+11234567", "pin": "12345"], postRequestCompletionHandler: {json,err -> Void in
    if (err == nil) {
      let meta = (json as! NSDictionary)["meta"] as! NSDictionary
      let metaCode = meta.objectForKey("code") as! NSInteger
      let metaMsg = meta.objectForKey("msg") as! String
      println(metaCode)
      println(metaMsg)
      println(json)
    } else {
      if let error : NSError = err as NSError!
      {
        println(error.code)
        println(error.domain)
      }
    }
  })
```
### Get User Profile

This is an example how to fetch a user profile.
It will return the following keys:<commit_msg>Fix up verify PIN API<commit_after># Kuggle API Swift class
Class to interface with the Kuggle backend

This is a work in progress.

## Sample code to query

### Instantiating this class
```swift
  var kuggle = KuggleAPI.sharedInstance
```

### Registration using facebook
#### Parameters
* 'fbtoken' - This is the token returned from the *OAUTH Dance* from the application. This endpoint will validate the users profile and decide whether or not to create it.

#### Expected Response
* 'fromfb' - This is what the app has pulled from facebook. Which contains a 'birthday' (Javascript Date format), 'birthdaystring' (format which you can actually serialize back into a javascript date object), 'firstname', 'gender' (0 = female, 1 = male, 2 = other or undisclosed), 'id' (the users facebook ID)
* 'token' - This is the login token that you need to use on protected endpoints. Only returned if registration was successful. Check NSError and display error.domain to user (this response is localized if you're using this API)

#### Other things to note
* Profile is automatically created. No need to call the create profile endpoint.

```swift
        k.postRequest("register", token: nil, params: ["fbtoken": "<PASSWORD>"], postRequestCompletionHandler: {json,err -> Void in
            if (err == nil) {
                let meta = (json as! NSDictionary)["meta"] as! NSDictionary
                let metaCode = meta.objectForKey("code") as! NSInteger
                let metaMsg = meta.objectForKey("msg") as! String
                println(metaCode)
                println(metaMsg)
                println(json)
            } else {
                if let error : NSError = err as NSError!
                {
                    println(error.code)
                    println(error.domain)
                }
            }

        })
```

### Registration by Phone Number

* replace '+11234567' with a phone number thats valid.

This only returns a 'meta' key. 200 is always good. Its automatically read and returns in NSError format. error.code is the HTTP code, and error.domain is the error message (this is also localized depending on the users phone settings)

```swift
  kuggle.postRequest("test", token: nil, params: ["phonenumber": "+11234567"], postRequestCompletionHandler: {json,err -> Void in
    if (err == nil) {
      let meta = (json as! NSDictionary)["meta"] as! NSDictionary
      let metaCode = meta.objectForKey("code") as! NSInteger
      let metaMsg = meta.objectForKey("msg") as! String
      println(metaCode)
      println(metaMsg)
      println(json)
    } else {
      if let error : NSError = err as NSError!
      {
        println(error.code)
        println(error.domain)
      }
    }
  })
```

### Verify PIN

* replace '+11234567' with a phone number thats valid.
* replace '12345' with a PIN thats valid

This functionality, returns the 'meta' key, as well as 'token' if successfully validated.

```swift
  kuggle.postRequest("register", token: nil, params: ["phonenumber": "+11234567", "pin": "12345"], postRequestCompletionHandler: {json,err -> Void in
    if (err == nil) {
      let meta = (json as! NSDictionary)["meta"] as! NSDictionary
      let metaCode = meta.objectForKey("code") as! NSInteger
      let metaMsg = meta.objectForKey("msg") as! String
      println(metaCode)
      println(metaMsg)
      println(json)
    } else {
      if let error : NSError = err as NSError!
      {
        println(error.code)
        println(error.domain)
      }
    }
  })
```
### Get User Profile

This is an example how to fetch a user profile.
It will return the following keys:
{'fraction_non_alphanumeric': 0.10193905817174516, 'fraction_numerical': 0.013296398891966758, 'mean_word_length': 2.8760064412238324, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 11, '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': '3969269', 'n_tokens_mistral': 2168, 'n_tokens_neox': 1958, 'n_words': 791}