Skip to content

Instantly share code, notes, and snippets.

@davelnewton
davelnewton / content_of_single_url.rb
Created November 6, 2011 23:14
Nokogiri results
# Get a URL's content to play with
content = contents[contents.keys.first]
# => [#<Nokogiri::XML::Element:0x48a9a86 name="p" children=[#<Nokogiri::XML::Text:0x48a9810 "The autobiography of Yohwan Lim, ">, #<Nokogiri::XML::Element:0x48a96ee name="i" children=[#<Nokogiri::XML::Text:0x48a9536 "Crazy As Me">]>, #<Nokogiri::XML::Text:0x48a931a " was released in Korea by BookRoad Publishers in October 25, 2004. This is my translation of the book, except the following four sections which were translated by BinaryStar of Teamliquid.net, which I have made minor changes: \"Hope on the Road Not Taken,\" \"Chapter One: The Game-crazed Kid,\" \"The Birth of the Emperor,\" and \"The Little Prince with Three Sisters.\"">]>, #<Nokogiri::XML::Element:0x48a9194 name="p" children=[#<Nokogiri::XML::Text:0x48a9022 "As of October 4, 2004:">]>, #<Nokogiri::XML::Element:0x48a8e92 name="p">, #<Nokogiri::XML::Element:0x48a8ce4 name="p" children=[#<Nokogiri::XML::Text:0x48a8af0 "The addition of e-sports organizations to major compani
@davelnewton
davelnewton / indentation-works.js
Created September 28, 2020 17:06
Indentation helps find structural issues
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return
let user = message.mentions.users.first()
const args = message.content.slice(prefix.length).split(/ +/)
const command = args.shift().toLowerCase()
if (command === 'marry') {
if (!user) return
const { details } = resp.Result
const keyMatch = i =>
`InternetGatewayDevice.TraceRouteDiagnostics.RouteHops.${i}.HopErrorCode`
for (let i = 1; i < details.length; i++) {
let resp2 = details.find(x => x.key == keyMatch(i)).value
return resp2
}
function extractParamStrings(s) {
if (s[0] === '?') s = s.slice(1, s.length)
return s.split('&')
}
const extractParamKeyValPair = paramString =>
paramString.split('=').map(decodeURIComponent)
const addParam = (toObj, [key, val]) => {
toObj[key] = val
export function fromUrlQuery(str) {
if (!str) return {}
if (str[0] === '?') str = str.slice(1, str.length)
return str.split('&').reduce((acc, vars) => {
const [key, val] = vars.split('=').map(decodeURIComponent)
acc[key] = val
return acc
}, {})
const ret = str.split('&').reduce((acc, vars) => {
const [key, val] = vars.split('=').map(decodeURIComponent)
console.log('fromURLQuery', { key, val })
acc[key] = val
return acc
}, {})
for (let i = 0; i < vars.length; i++) {
let pair = vars[i].split('=').map(decodeURIComponent)
ret[pair[0]] = pair[1]
console.log('fromURLQuery', pair)
}
export function fromURLQuery(str) {
console.log('fromURLQuery BEGIN', str)
if (!str) {
console.log('fromURLQuery return empty', ret)
return {}
}
export function fromURLQuery(str) {
console.log('fromURLQuery BEGIN', str)
let ret = {}
if (!str) {
console.log('fromURLQuery return empty', ret)
return ret
}
if (str[0] === '?') str = str.slice(1, str.length)
export function fromURLQuery(str) {
console.log('fromURLQuery BEGIN', str)
let ret = {}
if (!str) {
console.log('fromURLQuery return empty', ret)
return ret
}
if (str[0] === '?') str = str.slice(1, str.length)
let vars = str.split('&')
for (let i = 0; i < vars.length; i++) {