Skip to content

Instantly share code, notes, and snippets.

@chadhietala
Last active October 14, 2015 00:38
Show Gist options
  • Select an option

  • Save chadhietala/4280965 to your computer and use it in GitHub Desktop.

Select an option

Save chadhietala/4280965 to your computer and use it in GitHub Desktop.
Code Examples From Want
class Dataminer
mine: (type) ->
if this[type]
this[type]()
else
new Error 'Non-valid data type to mine. Acceptable types are image, url, and title'
image: ->
ogImage = queryOpenGraphParam "meta[property='og:image']"
imageUrl = if ogImage and isImageUrl(ogImage.content) then ogImage.content else null
return imageUrl
title: ->
ogTitle = queryOpenGraphParam "meta[property='og:title']"
title = if ogTitle then ogTitle.content else null
return title
url: ->
ogUrl = queryOpenGraphParam "meta[property='og:url']"
url = if ogUrl then ogUrl else null
return url
# Private Methods
queryOpenGraphParam = (selector) ->
results = document.querySelector selector
result = if results.length then results[0] else null
isImageUrl = (str) ->
reg = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?( /.*)?")
if reg.test str
return true
else
return false
# Create a new instance of the Dataminer
dataminer = new Dataminer
# Mine for an Open graph image
minedImage = dataminer.mine 'image'
class Router
@add: (path, callback) ->
@routes or= []
# Create a new object that creates a Regex for matching
# and the respective callback
@routes.push {
path: new RegExp(path.replace(/\//g, "\\/").replace(/:(\w*)/g,"(\\w*)")
callback: callback
}
@process: ->
for route in @routes
params = window.location.pathname.match(route.path)
if params?
route.callback(params)
return
# Assume we are using jQuery
$ ->
someFunc = ->
alert "Profile"
# Push the a route into the router
Router.add "/profile/:id", someFunc
$(document).on 'ready', ->
# Process all routes
Router.process()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment