├── README.md ├── lib └── swapi.rb └── swapi.gemspec /README.md: -------------------------------------------------------------------------------- 1 | Swapi Ruby 2 | ==== 3 | 4 | A Ruby helper library for swapi.co - the Star Wars API 5 | 6 | ![Darth Vader](http://3.bp.blogspot.com/-RA6aaFC4fPY/T91VeiHEK8I/AAAAAAAAAWo/M6drwtR73es/s1600/vader.jpg) 7 | 8 | Description 9 | ----------- 10 | 11 | Documentation: http://swapi.co/documentation 12 | 13 | Installation 14 | ------------ 15 | 16 | ``` console 17 | $ gem install swapi 18 | ``` 19 | 20 | Basic Usage 21 | ----- 22 | 23 | ``` ruby 24 | require "swapi" 25 | 26 | # Will print a JSON response with Luke Skywalker information 27 | luke = Swapi.get_person 1 28 | 29 | # Will print a JSON response with Tatooine specs 30 | tatooine = Swapi.get_planet 1 31 | ``` 32 | 33 | Methods 34 | ======= 35 | 36 | These are the top-level methods you can use to get resources from swapi.co 37 | 38 | get_person(people_id) 39 | ------------ 40 | 41 | Return a single ``Person`` resource. 42 | 43 | Example:: 44 | 45 | Swapi.get_person 1 46 | >>> 47 | 48 | 49 | get_planet(planet_id) 50 | ------------ 51 | 52 | Return a single ``Planet`` resource. 53 | 54 | Example:: 55 | 56 | Swapi.get_planet 1 57 | >>> 58 | 59 | 60 | get_starship(starship_id) 61 | ------------ 62 | 63 | Return a single ``Starship`` resource. 64 | 65 | Example:: 66 | 67 | Swapi.get_starship 6 68 | >>> 69 | 70 | 71 | get_vehicle(vehicle_id) 72 | ------------ 73 | 74 | Return a single ``Vehicle`` resource. 75 | 76 | Example:: 77 | 78 | Swapi.get_vehicle 4 79 | >>> 80 | 81 | 82 | get_film(film_id) 83 | ------------ 84 | 85 | Return a single ``Film`` resource. 86 | 87 | Example:: 88 | 89 | Swapi.get_film 1 90 | >>> 91 | 92 | 93 | get_all("resource_type") 94 | ------------ 95 | 96 | Return all the items in a single resource. 97 | 98 | Example:: 99 | 100 | Swapi.get_all "films" 101 | -------------------------------------------------------------------------------- /lib/swapi.rb: -------------------------------------------------------------------------------- 1 | require 'open-uri' 2 | 3 | module Swapi 4 | class << self 5 | BASE_URL = 'http://swapi.co/api' 6 | PLANETS = 'planets' 7 | PEOPLE = 'people' 8 | STARSHIPS = 'starships' 9 | VEHICLES = 'vehicles' 10 | SPECIES = 'species' 11 | FILMS = 'films' 12 | 13 | def get_all(type) 14 | get type 15 | end 16 | 17 | def get_planet(planet_id) 18 | get PLANETS, planet_id 19 | end 20 | 21 | def get_person(people_id) 22 | get PEOPLE, people_id 23 | end 24 | 25 | def get_starship(starship_id) 26 | get STARSHIPS, starship_id 27 | end 28 | 29 | def get_vehicle(vehicle_id) 30 | get VEHICLES, vehicle_id 31 | end 32 | 33 | def get_species(species_id) 34 | get SPECIES, species_id 35 | end 36 | 37 | def get_film(film_id) 38 | get FILMS, film_id 39 | end 40 | 41 | private 42 | 43 | def get(type, id = '') 44 | response = execute_request("#{type}/#{id}") 45 | end 46 | 47 | def execute_request(uri) 48 | response = open("#{BASE_URL}/#{uri}", "User-Agent" => "swapi-ruby").read 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /swapi.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.name = "swapi" 3 | s.version = "0.0.2" 4 | s.date = "2014-12-22" 5 | s.summary = "Swapi" 6 | s.description = "A Ruby helper library for swapi.co - the Star Wars API" 7 | s.authors = ["Ezequiel Maraschio"] 8 | s.email = "ezequiel.maraschio@gmail.com" 9 | s.files = ["lib/swapi.rb"] 10 | s.homepage = "https://github.com/emaraschio/swapi-ruby" 11 | s.license = "MIT" 12 | end 13 | --------------------------------------------------------------------------------