Starwars
A Ruby interface for http://swapi.co/ - the Star Wars API
API Support
- [x] Films API
- [x] People API
- [x] Planet API
- [x] Species API
- [x] Vehicles API
- [x] Starship API
Installation
Add this line to your application's Gemfile:
gem 'starwars'
And then execute:
$ bundle
Or install it yourself as:
$ gem install starwars
Available Resources
Starwars::Person
(see docs)Starwars::Film
(see docs)Starwars::Planet
(see docs)Starwars::Specie
(see docs)Starwars::Starship
(see docs)Starwars::Vehicle
(see docs)
Usage
All the resources defined above have 2 functions, #fetch
and #fetch_all
#Fetch
#fetch
is used to fetch a single resource via ID or Resource URL
Starwars::Person.fetch(1).name
Starwars::Person.fetch('http://swapi.co/api/people/3/').name
#fetch_all
#fetch_all
is used to fetch all resources via pagination
planets = Starwars::Planet.fetch_all
# to access the items use #items
planets.items.map(&:name)
planets.items.each{|item| p item.inspect}
# you can also use #results instead of iteams
planets.results.map(&:name)
Pagination
using #fetch_all
will yield a pagination response:
starships = Starwars::Starship.fetch_all
p starships.number_of_pages
p starships.last_page?
p starships.next_page?
To iterate pages you can use the following interface:
Starwars::Starship.fetch_all.each{|page|
p page.next
p page.previous
p page.results.size
}
Documentation
the gem is fairly documented. Browse the YARD documentaion for more information.
Contributing
- Fork it ( https://github.com/moski/starwars/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request