Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
 writing apps i want to use

Junda samwize

💭
 writing apps i want to use
View GitHub Profile
@samwize
samwize / mocha-guide-to-testing.js
Created Feb 8, 2014
Explain Mocha's testing framework - describe(), it() and before()/etc hooks
View mocha-guide-to-testing.js
// # Mocha Guide to Testing
// Objective is to explain describe(), it(), and before()/etc hooks
// 1. `describe()` is merely for grouping, which you can nest as deep
// 2. `it()` is a test case
// 3. `before()`, `beforeEach()`, `after()`, `afterEach()` are hooks to run
// before/after first/each it() or describe().
//
// Which means, `before()` is run before first it()/describe()
@samwize
samwize / DynamicKey.swift
Created Sep 28, 2017
A CodingKey that is dynamic -- it can be any string! Encode/decode with a Dictionary of `[String : Any]` in the model.
View DynamicKey.swift
/**
```
// Encode a model with properties of type [String : Any]
var propertiesContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: .properties)
if let properties = properties {
try propertiesContainer.encodeDynamicKeyValues(withDictionary: properties)
}
```
*/
struct DynamicKey: CodingKey {
View Setup NSFetchedResultsController with UITableView.swift
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
do {
try fetchedResultsController.performFetch()
} catch {
print("Error")
}
}
@samwize
samwize / UIImage+Resize.swift
Created Jun 1, 2016
The original code to resize UIImage
View UIImage+Resize.swift
extension UIImage {
func resizedImage(newSize:CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
View activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
@samwize
samwize / MainActivity.java
Last active Dec 25, 2015
Add custom font to timerTextView
View MainActivity.java
package com.example.stopwatch;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@samwize
samwize / HoiioSignature.java
Created Dec 14, 2012
Compute signature of a http from Hoiio. This is used to authenticate the notification from Hoiio. Match this computed signature with the HTTP header "X-Hoiio-Signature".
View HoiioSignature.java
public String computeHoiioSignature(byte[] payload, String accessToken) {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(payload);
md.update(accessToken.getBytes("UTF-8"));
byte[] bytes = md.digest();
String mySign = Hex.encodeHexString(bytes);
return mySign;
}
@samwize
samwize / hoiio_signature.php
Created Dec 14, 2012
Compute signature of a http from Hoiio. This is used to authenticate the notification from Hoiio. Match this computed signature with the HTTP header "X-Hoiio-Signature".
View hoiio_signature.php
<?php
function computeHoiioSignature($payload, $accessToken) {
// hash_hmac will return a hex of the digest
$signature =hash_hmac('sha256', $payload, $accessToken);
return $signature;
}
?>
@samwize
samwize / hoiio_signature.py
Created Dec 14, 2012
Compute signature of a http from Hoiio. This is used to authenticate the notification from Hoiio. Match this computed signature with the HTTP header "X-Hoiio-Signature".
View hoiio_signature.py
import hmac
import hashlib
def sign(payload, key):
dig = hmac.new(key, msg=payload, digestmod=hashlib.sha256).digest()
return dig.encode('hex')
if __name__ == "__main__":
import sys
if (len(sys.argv) == 3):
@samwize
samwize / sms.php
Created Oct 23, 2012
Hoiio Example: Send an SMS
View sms.php
<?php
/* Hoiio developer credentials */
$hoiioAppId = "YOUR_APP_ID_HERE";
$hoiioAccessToken = "YOUR_ACCESS_TOKEN_HERE";
$sendSmsURL = "https://secure.hoiio.com/open/sms/send";
/* Recipient of SMS */
$destination = "+6511111111";
$message = "Congrats! You have just sent your first SMS with Hoiio!";