Skip to content

Instantly share code, notes, and snippets.

View rippinrobr's full-sized avatar

Rob Rowe rippinrobr

View GitHub Profile
@rippinrobr
rippinrobr / get-repos.sh
Created January 20, 2019 16:12
A script to clone multiple github repos
#!/bin/bash
repos=("rippinrobr/csv-to.git" "rippinrobr/retrosheet-rs" "rippinrobr/gh-tools"
"rippinrobr/cli-book-apps" "rippinrobr/barrel" "rippinrobr/baseball-stats-db" "rippinrobr/hockey-databank")
src_base_dir="../src"
github_base_uri="git@github.com:"
cd $src_base_dir
@rippinrobr
rippinrobr / click.rs
Created October 30, 2018 01:11
The two functions used to create a click event handler for the Rust Click button
// click_examples_init is called from the init() function in src/lib.rs
// it is responsible for registering a click event handler. If there is no
// element found with the given id, then an alert button is displayed with
// an error message
pub fn click_examples_init(elem_id: String, txt_id: String) {
match super::get_element_by_id(&elem_id) {
Some(btn) => register_click_handler(btn.as_ref() as &web_sys::EventTarget, txt_id),
None => super::alert(&format!("No element was found with the id {}", elem_id)),
};
}
@rippinrobr
rippinrobr / click.rs
Created October 30, 2018 00:39
The rust function that is used in the index.js file to handle the clicks on the JS click button
// js_click_event_handler is called by the js code in a more
// 'tradiitional event listerner approach. It will popup an
// alert dialog and display the click event information
#[wasm_bindgen]
pub fn js_click_event_handler(evt: web_sys::MouseEvent) {
super::alert(&format!("[js handler]\n{:#?}", Click::new(evt)));
}
@rippinrobr
rippinrobr / index.js
Last active October 30, 2018 00:57
The index.js file for my rust-wasm cookbok blog series - handling click events
import * as wasm from "wasmcookbook";
// setup the listener to call the rust function
document.getElementById("btn-js-click").addEventListener('click', wasm.js_click_event_handler);
// This struct that is being created is used
// to pass the ids of the HTML elements that
// the code interacts with. I want to avoid
// hard-coding the ids in the rust code, so I
// decided to do it in JS
@rippinrobr
rippinrobr / Conspiracies.elm
Created July 21, 2018 15:55
The Conspiracy type alias and the conspiracyDecoder function in one gist
type alias Conspiracy = {
title : String
,page_id : String
,summary : String
,content : String
,background: String
}
-- conspiracyDecoder is the code that pulls the data out of the JSON object
@rippinrobr
rippinrobr / getConspiracies.elm
Created July 21, 2018 15:11
the getConspiracies function - retrieves a paginated list of conspiracies by category
-- getConspiracies is responsible for making the HTTP GET
-- call to fetch the conspiracies for a given category. The |> is a pipe operator and I'm
-- using to create a 'pipeline'. The Json.Decode.list conspiracyDecoder is passed to
-- the Http.get call as the last parameter and is responsible for turning the JSON
-- Array of Category object into an Elm list of Conspiracies. The List of conspiracies
-- from the decoder become the parameter of the DataRecieved Msg and
-- eventually become the content on the right of the page
getConspiracies : Int -> Cmd Msg
getConspiracies category_id =
Json.Decode.list conspiracyDecoder
@rippinrobr
rippinrobr / main.elm
Last active July 18, 2018 21:52
The case matches that pertain to retrieving the categories
- update drives the changes in the UI and interaction with the server.
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
DataReceived (Ok categories) ->
({ model | categories = categories, errorMsg = Nothing }, Cmd.none)
DataReceived (Err httpError) ->
({ model | errorMsg = Just (createErrorMessage httpError) }, Cmd.none)
-- getCategoriesCommand is responsible for making the HTTP GET
-- call to fetch the tags. |> is a pipe operator and I'm using to create a 'pipeline'.
-- The Json.Decode.list tagDecoder is passed to the Http.get call as the last parameter
-- and is responsible for turning the JSON Array of categories into an Elm list of Categories.
-- The List of Categories from the decoder become the parameter of the DataRecieved Msg and
-- eventually become the navigation list
getCategoriesCommand : Cmd Msg
getCategoriesCommand =
Json.Decode.list categoryDecoder
|> Http.get "http://localhost:8088/categories"
@rippinrobr
rippinrobr / Category.elm
Last active July 18, 2018 21:19
Elm Category type alias and the decode function for a category JSON object
type alias Category = {
id : Int
,name : String
,approved : Int
}
-- categoryDecoder is the code that pulls the data out of the JSON object
-- and creates a Category object
categoryDecoder : Decoder Category
categoryDecoder =
@rippinrobr
rippinrobr / categories.json
Created July 18, 2018 21:04
List of categories returned from conspiracies api HTTP GET/categories
[
{"id":5,
"name":"9/11",
"approved":1},
{"id":9,"name":"Assassinations","approved":1},
{"id":10,"name":"CIA/FBI/NSA","approved":1},
{"id":8,"name":"Conspiracy Studies","approved":1},
{"id":7,"name":"End of the World","approved":1},
{"id":13,"name":"Paranormal","approved":1},
{"id":12,"name":"Political","approved":1},