Git Product home page Git Product logo

crystal-swapi's Introduction

crystal-swapi

Build Status

Star Wars API (SWAPI) for Crystal.

Documentation: https://swapi.co/documentation

Installation

Add this to your application's shard.yml:

dependencies:
  swapi:
    github: sb89/crystal-swapi

Basic Usage

require "swapi"

luke = Swapi::Person.get 1

p luke.birth_year

tatooine = Swapi::Planet.get 1

p tatooine.climate

Get

get can be called with an index (as seen above) or with no index, which will return all:

planets = Swapi::Planet.get

planets.results.each do |planet|
  p planet.name
end

if planets.next?
  planets = planets.get_next

  planets.results.each do |planet|
    p planet.name
  end
end 

Search

planets = Swapi::Planet.search "oine"

planets.results.each do |planet|
  p planet.name
end

List

Both search and get (without an index) return a List:

planets = Swapi::Planet.get

planets.count # Total number of planets

planets.results # Contains the current batch of planets

planets.next? # Is there another batch of planets?

planets = planets.get_next # Get list for next batch of planets

planets.previous? # Is there a previous batch of planets?

planets = planets.get_previous # Get list for previous batch of planets

Linked Resources

All resources are linked to some form of other resource. For example, a Person has films, species, starships and vehicles. The API returns these as urls to another resource. We can call get_<resource> e.g. get_films to get an array of the actual objects:

planet = Swapi::Planet.get 1

p planet.films # This will print an array of urls

planet.get_films.each { |f| p f.title } # Use get_films to get the actual film objects and print their titles

Contributing

  1. Fork it ( https://github.com/sb89/crystal-swapi/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • sb89 Steven - creator, maintainer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.