Skip to content

Instantly share code, notes, and snippets.

@neektza
Created June 11, 2014 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neektza/f76e8a44267a669f564a to your computer and use it in GitHub Desktop.
Save neektza/f76e8a44267a669f564a to your computer and use it in GitHub Desktop.
JSON, Aeson and Template Haskell for fun and profit
{-# LANGUAGE OverloadedStrings #-}
module Meetup.Types.Event where
import Data.Text
import Data.Aeson
import Control.Applicative ((<$>), (<*>))
import Control.Monad (mzero)
import Data.ByteString.Lazy
data Event = Event { name :: Text
, created :: Int
, event_url :: Text
} deriving (Show, Eq)
instance FromJSON Event where
parseJSON (Object v) = Event
<$> v .: "name"
<*> v .: "created"
<*> v .: "event_url"
parseJSON _ = mzero
-- Sample
sampleEvent = do
sample_event_json <- Data.ByteString.Lazy.readFile "responses/event.json"
let event = decode sample_event_json :: Maybe Event
return event
{
"visibility" : "public",
"waitlist_count" : 0,
"time" : 1399939200000,
"status" : "upcoming",
"yes_rsvp_count" : 11,
"group" : {
"group_lat" : 42.9799995422363,
"who" : "UI Professionals",
"join_mode" : "open",
"urlname" : "HTML5-Milwaukee",
"name" : "HTML5 Milwaukee",
"group_lon" : -87.8899993896484,
"id" : 4130942
},
"utc_offset" : -18000000,
"event_url" : "http://www.meetup.com/HTML5-Milwaukee/events/173163622/",
"updated" : 1397848994000,
"id" : "173163622",
"headcount" : 0,
"name" : "HTML5 App Start to Finish : Pt 2 : observables, live binding, charts & grids",
"duration" : 4500000,
"rsvp_limit" : 35,
"description" : "<p>Part 2  of this series will focus on building page components with observable data structures, server data models, application state and simple live binding.  </p> <p>Please bring a laptop as we will be doing a 50/50 panel and work session.</p> <p><br/>Keeping an application state object helps track user selections and can be bound to a data model for easy server persistence. We'll show a simple CRUD api for this with a mock up backend in NodeJS.</p> <p><br/>Using Observable objects allow you to get progressive updates to a dataset and signal those changes to your page controls. Live bound page components will update themselves when your observable data changes. We'll be building a grid and a simple chart using D3 and <a href=\"http://canjs.com/\">CanJS</a> <a href=\"http://canjs.com/docs/can.Component.html\">can.Component</a> web component tooling.</p> <p>Please install NodeJS and <a href=\"http://git-scm.com/\">Git</a> (or the Github application for <a href=\"https://mac.github.com/\">Mac</a> or <a href=\"https://windows.github.com/\">Windows</a>) for your platform before you arrive. You can clone <a href=\"https://github.com/shcarrico/skel-html5\"><a href=\"https://github.com/shcarrico/skel-html5\" class=\"linkified\">https://github.com/shcarrico/skel-html5</a></a> and follow the readme.md for installation directions to get the sample app up and running. I will be making changes to the sample repo up to the meetup day, so check back for new stuff ~ day or so ahead of time.</p>",
"created" : 1395693245000,
"venue" : {
"country" : "us",
"lat" : 43.051735,
"name" : "Corvisa Services",
"address_1" : "1610 North 2nd Street #101",
"state" : "WI",
"city" : "Milwaukee",
"zip" : "53212",
"id" : 11046872,
"lon" : -87.912354,
"repinned" : false
},
"maybe_rsvp_count" : 0
}
{-# LANGUAGE OverloadedStrings,TemplateHaskell #-}
module Meetup.Types.RSVP where
import Data.Aeson
import Data.Aeson.TH
import qualified Data.Text as T
import qualified Data.ByteString.Lazy as BL
data RSVP = RSVP { response :: T.Text
, created :: Int
} deriving (Show, Eq)
$(deriveJSON defaultOptions ''RSVP)
sampleRSVP = do
sample_event_json <- BL.readFile "responses/rsvp.json"
let rsvp = decode sample_event_json :: Maybe RSVP
return rsvp
{
"rsvp_id" : 1203820722,
"mtime" : 1395693245000,
"member_photo" : {
"photo_id" : 59311832,
"thumb_link" : "http://photos3.meetupstatic.com/photos/member/7/c/5/8/thumb_59311832.jpeg",
"photo_link" : "http://photos1.meetupstatic.com/photos/member/7/c/5/8/member_59311832.jpeg"
},
"member" : {
"member_id" : 51992492,
"name" : "Stan Carrico"
},
"response" : "yes",
"event" : {
"time" : 1399939200000,
"name" : "HTML5 App Start to Finish : Pt 2 : observables, live binding, charts & grids",
"event_url" : "http://www.meetup.com/HTML5-Milwaukee/events/173163622/",
"id" : "173163622"
},
"created" : 1395693245000,
"group" : {
"group_lat" : 42.9799995422363,
"join_mode" : "open",
"urlname" : "HTML5-Milwaukee",
"group_lon" : -87.8899993896484,
"id" : 4130942
},
"guests" : 0,
"venue" : {
"country" : "us",
"lat" : 43.051735,
"name" : "Corvisa Services",
"address_1" : "1610 North 2nd Street #101",
"state" : "WI",
"city" : "Milwaukee",
"zip" : "53212",
"id" : 11046872,
"lon" : -87.912354,
"repinned" : false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment