Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embedding social medial posts #2

Merged
merged 2 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[alias]
build-wasm = "build --target wasm32-wasi"
build-native = "build"
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ license-file = "LICENSE.txt"
handlebars = {version = "4.1", features = ["dir_source", "script_helper"]}
serde_json = "1.0"
chrono = {version = "0.4", features = ["serde"]}
rand = "0.8.5"
rand = "0.8.5"
http = "0.2"
wasi-experimental-http = "0.7.0"
serde = { version = "1.0", features = ["derive"] }
10 changes: 10 additions & 0 deletions src/gist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use handlebars::{handlebars_helper, Handlebars};

pub fn addhelpers(x: &mut Handlebars) {
handlebars_helper!(gist: |user: String, id: String| {
let s = format!(r#"<script type="application/javascript" src="https://gist.github.com/{}/{}.js"></script>"#, user, id);
s
});

x.register_helper("gist", Box::new(gist));
}
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use handlebars::Handlebars;

pub mod strings;
pub mod math;
pub mod date;
pub mod gist;
pub mod math;
pub mod strings;
pub mod tweet;

pub fn addhelpers(x: &mut Handlebars) {
strings::addhelpers(x);
math::addhelpers(x);
date::addhelpers(x)
}
date::addhelpers(x);
tweet::addhelpers(x);
gist::addhelpers(x);
}
26 changes: 26 additions & 0 deletions src/tweet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use handlebars::{handlebars_helper, Handlebars};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct Publish {
html: String,
}

pub fn addhelpers(x: &mut Handlebars) {
handlebars_helper!(tweet: |user: String, id: String| {
let url = format!("https://publish.twitter.com/oembed?url=https://twitter.com/{}/status/{}", user, id);
let req = http::request::Builder::new().uri(&url).body(None).unwrap();
let mut res = wasi_experimental_http::request(req).expect("cannot make get request");

let mut html = "".to_string();
if res.status_code == 200 {
let str = std::str::from_utf8(&res.body_read_all().unwrap()).unwrap().to_string();
let deserialized: Publish = serde_json::from_str(&str).unwrap();
html = deserialized.html.to_string()
}

html
});

x.register_helper("tweet", Box::new(tweet));
}