Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / DispatchRequest.scala
Created March 6, 2014 02:46
Making an HTTP Request with Dispatch
import dispatch._
import net.liftweb.json._
val h = new Http
val req = url("https://api.github.com/users/brentsowers1/repos")
// Gets both the headers and response body
val rspStr = h(req >:+ { (headers, req) =>
val hdrs = headers
req as_str
@brentsowers1
brentsowers1 / GithubRepo.scala
Created March 6, 2014 02:47
Github Repo case class
/**
* A github user
*/
case class User(login: String, // login name of hte user
id: Int, // numeric github ID
avatar_url: String, // URL to their avatar pic
url: String) // API url to retrieve JSON of the user
/**
* A single code repo.
@brentsowers1
brentsowers1 / ParseGithub.scala
Created March 6, 2014 02:48
Parsing Github data in to case classes
// Don't or modify remove this, it's needed by the Lift JSON library
implicit val formats = net.liftweb.json.DefaultFormats
val rspJson = parse(rspStr)
// Github response is an array of objects, this will get that array
// as a List
val rspList = rspJVal.children
val rspRepos = for (repoJObj <- rspList) yield repoJObj.extract[Repo]
@brentsowers1
brentsowers1 / session_store.rb
Created March 6, 2014 02:59
config/initializers/session_store.rb
# Be sure to restart your server when you modify this file.
# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
ActionController::Base.session = {
:key => '_<%= app_name %>_session',
:secret => '<%= app_secret %>'
}
@brentsowers1
brentsowers1 / application_helper.rb
Created March 6, 2014 03:00
application_helper
module ApplicationHelper
def inner_helper
str = content_tag(:p, "inside p content tag")
str << "a space should be between the following words: hello"
str << "&nbsp;"
str << "world"
str << "more <span style=\"font-weight:bold;\">dirty HTML</span>"
str << content_tag(:div, "inside div content tag")
end