urbandict

package module
v0.0.0-...-83a04bc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2016 License: BSD-2-Clause Imports: 7 Imported by: 1

README

go-urbandict

go-urbandict is a Go library for accessing the Urban Dictionary REST API.

Get

Fetch and build go-urbandict:

go get github.com/davidscholberg/go-urbandict

Library overview

func Trending() ([]string, error)
func Define(term string) (*Definition, error)
func Random() (*Definition, error)
func WordOfTheDay() (*Definition, error)
func DefineRaw(term string) (*DefinitionResponse, error)
func RandomRaw() (*DefinitionResponse, error)
type Definition struct { ... }
type DefinitionResponse struct { ... }

NOTE: The Trending and WordOfTheDay functions scrape Urban Dictionary's website since these features do not appear to be exposed in their REST API.

Usage

package main

import (
	"fmt"
	"os"
	urbandict "github.com/davidscholberg/go-urbandict"
)

func main () {
	// get top definition of "1337"
	def, err := urbandict.Define("1337")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	fmt.Printf("JSON representation of \"1337\" definition:\n%s\n\n", def)
	fmt.Printf("Accessing individual elements:\ndef: %s\nexample: %s\n\n",
		def.Definition,
		def.Example)

	// get the word of the day
	def, err = urbandict.WordOfTheDay()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	fmt.Printf("JSON representation of the word of the day:\n%s\n\n", def)

	// get a random definition
	def, err = urbandict.Random()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	fmt.Printf("JSON representation of random definition:\n%s\n\n", def)

	// get trending words
	trending, err := urbandict.Trending()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	fmt.Printf("Trending words: %v\n\n", trending)

	// get raw response object for a search query
	defRaw, err := urbandict.DefineRaw("w00t")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	fmt.Printf("JSON representation of raw response to \"w00t\" query:\n%s\n\n",
		defRaw)

	// get raw response object for random word query
	defRaw, err = urbandict.RandomRaw()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	fmt.Printf("JSON representation of raw response to random query:\n%s\n\n",
		defRaw)
}

Documentation

Overview

Package urbandict provides a Go wrapper for the Urban Dictionary REST API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Trending() ([]string, error)

Trending returns Urban Dictionary's currently trending words.

Types

type Definition

type Definition struct {
	Author       string `json:"author"`
	Current_vote string `json:"current_vote"`
	Defid        int    `json:"defid"`
	Definition   string `json:"definition"`
	Example      string `json:"example"`
	Permalink    string `json:"permalink"`
	Thumbs_down  int    `json:"thumbs_down"`
	Thumbs_up    int    `json:"thumbs_up"`
	Word         string `json:"word"`
}

Definition represents a single urban dictionary definition.

func Define

func Define(term string) (*Definition, error)

Define gets the top definition for a search term.

func Random

func Random() (*Definition, error)

Random gets a random definition.

func WordOfTheDay

func WordOfTheDay() (*Definition, error)

WordOfTheDay returns the definition for Urban Dictionary's word of the day.

func (*Definition) String

func (d *Definition) String() string

type DefinitionResponse

type DefinitionResponse struct {
	List        []Definition `json:"list"`
	Result_type string       `json:"result_type"`
	Sounds      []string     `json:"sounds"`
	Tags        []string     `json:"tags"`
}

DefinitionResponse represents the JSON response from urban dictionary.

func DefineRaw

func DefineRaw(term string) (*DefinitionResponse, error)

DefineRaw gets the full response object for a search query.

func RandomRaw

func RandomRaw() (*DefinitionResponse, error)

RandomRaw gets a full response object for a random definition api call.

func (*DefinitionResponse) String

func (d *DefinitionResponse) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL