Skip to content

Instantly share code, notes, and snippets.

@bjmllr
Created September 14, 2015 23:13
Show Gist options
  • Save bjmllr/a366d137f0d52d95f405 to your computer and use it in GitHub Desktop.
Save bjmllr/a366d137f0d52d95f405 to your computer and use it in GitHub Desktop.
crystal client lib for swapi.io
require "http/client"
require "json"
class StarWars
BASE_URI = "http://swapi.co/api"
def self.get(uri)
new.get(uri)
end
def get(uri)
response = HTTP::Client.get uri
@last_response = response
Response.new(response)
end
class Response
def initialize(@response)
end
def body
@response.body
end
end
class Resource
def self.fragment
name.downcase.match(/[^:]+$/).not_nil![0]
end
def self.get(id : Int32)
get("#{BASE_URI}/#{fragment}/#{id}/")
end
def self.get(id : String)
from_json(StarWars.new.get(id).body)
end
macro has_many(key, cls)
def {{key}}(index)
{{cls}}.get({{key}}[index])
end
end
macro has_many(key)
def {{key}}(index)
{{key.stringify.camelcase.id}}.get({{key}}[index])
end
end
macro has_one(key, cls)
def {{key}}
{{cls}}.from_json(StarWars.get(@{{key}}).body)
end
end
end
class Films < Resource; end
class People < Resource; end
class Planets < Resource; end
class Species < Resource; end
class Starships < Resource; end
class Vehicles < Resource; end
class Films < Resource
json_mapping({
title: String,
episode_id: Int32,
opening_crawl: String,
director: String,
producer: String,
release_date: String,
species: Array(String),
starships: Array(String),
vehicles: Array(String),
characters: Array(String),
planets: Array(String),
url: String,
created: String,
edited: String,
})
has_many species
has_many starships
has_many vehicles
has_many characters, People
has_many planets
end
class People < Resource
json_mapping({
name: String,
birth_year: String,
eye_color: String,
gender: String,
hair_color: String,
height: String,
mass: String,
skin_color: String,
homeworld: String,
films: Array(String),
species: Array(String),
starships: Array(String),
vehicles: Array(String),
url: String,
created: String,
edited: String
})
has_many films
has_many species
has_many starships
has_many vehicles
end
class Planets < Resource
json_mapping({
name: String,
diameter: String,
rotation_period: String,
orbital_period: String,
gravity: String,
population: String,
climate: String,
terrain: String,
surface_water: String,
residents: Array(String),
films: Array(String),
url: String,
created: String,
edited: String
})
has_many residents, People
has_many films
end
class Species < Resource
json_mapping({
name: String,
classification: String,
designation: String,
average_height: String,
average_lifespan: String,
eye_colors: String,
hair_colors: String,
skin_colors: String,
language: String,
homeworld: String,
people: Array(String),
films: Array(String),
url: String,
created: String,
edited: String
})
has_many residents, People
has_many films
has_one homeworld, Planets
end
class Starships < Resource
json_mapping({
name: String,
model: String,
starship_class: String,
manufacturer: String,
cost_in_credits: String,
length: String,
crew: String,
passengers: String,
max_atmosphering_speed: String,
hyperdrive_rating: String,
mglt: String,
cargo_capacity: String,
consumables: String,
films: Array(String),
pilots: Array(String),
url: String,
created: String,
edited: String
})
has_many films
has_many pilots, People
end
class Vehicles < Resource
json_mapping({
name: String,
model: String,
vehicle_class: String,
manufacturer: String,
length: String,
cost_in_credits: String,
crew: String,
passengers: String,
max_atmosphering_speed: String,
cargo_capacity: String,
consumables: String,
films: Array(String),
pilots: Array(String),
url: String,
created: String,
edited: String,
})
has_many films
has_many pilots, People
end
end
luke = StarWars::People.get(1)
p luke.name
p luke.films
p luke.films(0).title
humans = luke.species(0)
p humans.name
p humans.homeworld.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment