Skip to content

Instantly share code, notes, and snippets.

@brentsowers1
brentsowers1 / build.sbt
Created March 6, 2014 02:45
Dispatch in build file
val dispatch = "net.databinder" %% "dispatch-http" % "0.8.6"
val liftJson = "net.liftweb" %% "lift-json" % "2.3"
@brentsowers1
brentsowers1 / GithubApi.scala
Created March 6, 2014 02:44
Github API example creating a class
import dispatch._
import net.liftweb.json._
// Nothing to this class
class GithubClass
/**
* A github user
*/
case class User(login: String, // login name of hte user
@brentsowers1
brentsowers1 / NewInstance.scala
Created March 6, 2014 02:42
Creating a new instance of a generic class
class DynamicTestClass() {
def output() {
println("Hello from a dynamically sent class")
}
}
def testFunc[T : Manifest]() : T = {
manifest[T].erasure.newInstance().asInstanceOf[T]
}
val dynamicTestClassInstance = testFunc[DynamicTestClass]()
@brentsowers1
brentsowers1 / DispatchWithAuth.scala
Created March 6, 2014 02:41
HTTP Authentication with Dispatch
val h = new Http
val req = url("url to request").as_!("user name", "password")
val rsp = h(req as_str)
@brentsowers1
brentsowers1 / JsonToMap.scala
Created March 6, 2014 02:40
Parsing JSON in to a Map with Lift JSON
import net.liftweb.json._
val jsonStr: String = """{"key1":"value1","key2":1}"""
val jsonJVal: JValue = parse(jsonStr)
val jsonMap: Map[String,Any] = jsonJVal.values.asInstanceOf[Map[String,Any]]
for ((key,value) <- jsonMap) {
println("key = " + key + ", value = " + value.toString)
}
println("Key 1's value is " + jsonMap("key1"))
@brentsowers1
brentsowers1 / build.sbt
Created March 6, 2014 02:39
build.sbt for Lift JSON
// 2.4 is currently the latest but will only work with Scala 2.9
val liftJson = "net.liftweb" %% "lift-json" % "2.3"
@brentsowers1
brentsowers1 / ExecuteCommands.scala
Created March 6, 2014 02:38
Executing system commands in Scala
import sbt._
val output : String = Process("command to run").!!
val returnCode: Int = Process("command to run").!
@brentsowers1
brentsowers1 / verify_facebook_sig.rb
Created March 6, 2014 02:36
Verifying Facebook's sig
require 'openssl'
require 'base64'
require 'json'
# Facebook's base64 algorithm is a special "URL" version of the algorithm.
def base64_url_decode(str)
str += '=' * (4 - str.length.modulo(4))
Base64.decode64(str.tr('-_','+/'))
end
@brentsowers1
brentsowers1 / publish_to_wall.rb
Created March 6, 2014 02:34
Publishing to a users wall with Facebooker
# load access token and expiration from database
client = Mogli::Client.new(access_token, expiration)
user = Mogli::User.find(facebook_id, client)
post = {:name => "main title for the post",
:caption => "Sub title for the post",
:description => "Long description for the post",
:picture => "url to a picture for the post",
:link => "url to go to when the user clicks on the title",
:from => {:name => "Your app's name",
:category => "Your app's category",
@brentsowers1
brentsowers1 / helpers.rb
Created March 6, 2014 02:33
/lib/facebooker/rails/helpers.rb
module Facebooker
def self.api_key
unless @_api_key
config = (YAML.load(ERB.new(File.read(File.join(::Rails.root,"config","facebooker.yml"))).result)[::Rails.env])
@_api_key = config["api_key"]
end
@_api_key
end