Skip to content

Instantly share code, notes, and snippets.

View kitz99's full-sized avatar

Bogdan Timofte kitz99

View GitHub Profile
@kitz99
kitz99 / sh
Created June 11, 2018 06:26
LinuxTestBash
#!/bin/bash
echo "Hello world"
version: '2'
networks:
roachnet:
services:
master:
image: cockroachdb/cockroach:v2.0.3
command: start --insecure
ports:
$ docker exec -it goroach_master_1 ./cockroach sql --insecure
> create database blog_db;
> set database = blog_db;
> CREATE TABLE "posts" (
"id" SERIAL,
"name" STRING(100),
"category" STRING(50),
"author" STRING(50),
func init() {
roachConnection, err := sql.Open("postgres", "postgresql://root@localhost:26257/blog_db?sslmode=disable")
if err != nil {
panic("Could not establish connection to CockroachDB")
}
}
func createRecord(post Post) error {
qryString := fmt.Sprintf(
"INSERT INTO posts (name, category, author, created_at, updated_at) VALUES ('%s', '%s', '%s', NOW(), NOW())",
post.Name, post.Category, post.Author)
_, err := roachConnection.Exec(qryString)
return err
}
func readRecords() []Post {
var result []Post
rows, err := roachConnection.Query("select * FROM posts;")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
func deleteRecord(id int64) error {
qryString := fmt.Sprintf("DELETE from posts WHERE id = %d", id)
fmt.Printf("Query: %v\n", qryString)
_, err := roachConnection.Exec(qryString)
return err
}
func updateRecord(fieldName string, newVal string, condFieldName string, condFieldValue int64) error {
qryString := fmt.Sprintf("UPDATE posts set %s='%s' WHERE %s=%d", fieldName, newVal, condFieldName, condFieldValue)
fmt.Printf("Query: %v\n", qryString)
_, err := roachConnection.Exec(qryString)
return err
}
require 'open-uri'
require 'nokogiri'
document = Nokogiri::HTML(open(URL))
results = []
products = document.xpath("//ul[@class='products']/li")
products.each do |p|
json_product = {}
json_product[:title] = p.xpath("div/div[@class='product-header']").text.strip
json_product[:description] = p.xpath("div/div[@class='product-body']/p[contains(@class, 'description')]").text.strip
json_product[:price] = p.xpath("div/div[@class='product-footer']/p[contains(@class, 'price')]").text.strip
results << json_product
end