starwars 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +26 -0
- data/Gemfile +17 -0
- data/Guardfile +16 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +27 -0
- data/lib/starwars.rb +19 -0
- data/lib/starwars/base.rb +54 -0
- data/lib/starwars/fetcher.rb +40 -0
- data/lib/starwars/film.rb +35 -0
- data/lib/starwars/person.rb +39 -0
- data/lib/starwars/planet.rb +37 -0
- data/lib/starwars/specie.rb +38 -0
- data/lib/starwars/starship.rb +40 -0
- data/lib/starwars/vehicle.rb +38 -0
- data/lib/starwars/version.rb +4 -0
- data/spec/fixtures/films_1.json +1 -0
- data/spec/fixtures/people_1.json +1 -0
- data/spec/fixtures/people_2.json +1 -0
- data/spec/fixtures/planets_1.json +1 -0
- data/spec/fixtures/species_1.json +1 -0
- data/spec/fixtures/starships_10.json +1 -0
- data/spec/fixtures/starships_5.json +1 -0
- data/spec/fixtures/vehicles_14.json +1 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/starwars/film_spec.rb +132 -0
- data/spec/starwars/person_spec.rb +150 -0
- data/spec/starwars/planet_spec.rb +128 -0
- data/spec/starwars/species_spec.rb +125 -0
- data/spec/starwars/starship_spec.rb +144 -0
- data/spec/starwars/vehicle_spec.rb +130 -0
- data/starwars.gemspec +26 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ad657666a52a78f6326d5781ee9f1c12f19e5b54
|
4
|
+
data.tar.gz: 07fefe6fd93d76fb5bf6184093608379a7252147
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b9c5802184691e8e1337a9048488e5b2d0fd8d258c799e9526fc065c7456d1f3bfff9a3ccc83a3a05d8978007ea55d661fae4f38f75dc91b8d36589034ce952
|
7
|
+
data.tar.gz: bda6c580a84b436c3eb057ee174de48f3253556eb13f227c140d5d92c23c60f09ced5d58088b97640ee233bbd3d1367714dbe7bfd04718a104c2557ec9a9d279
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
language: ruby
|
2
|
+
bundler_args: --without guard
|
3
|
+
script: "bundle exec rake"
|
4
|
+
rvm:
|
5
|
+
- 1.9.3
|
6
|
+
- 2.0.0
|
7
|
+
- 2.1.0
|
8
|
+
- ruby-head
|
9
|
+
matrix:
|
10
|
+
include:
|
11
|
+
- rvm: jruby-19mode
|
12
|
+
- rvm: jruby-20mode
|
13
|
+
- rvm: jruby-21mode
|
14
|
+
- rvm: jruby-head
|
15
|
+
- rvm: rbx-2
|
16
|
+
allow_failures:
|
17
|
+
- rvm: ruby-head
|
18
|
+
- rvm: jruby-head
|
19
|
+
- rvm: jruby-20mode
|
20
|
+
- rvm: jruby-21mode
|
21
|
+
- rvm: rbx-2
|
22
|
+
fast_finish: true
|
23
|
+
branches:
|
24
|
+
only: master
|
25
|
+
notifications:
|
26
|
+
email: false
|
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
group :test do
|
4
|
+
gem 'coveralls'
|
5
|
+
gem 'rspec', '>= 2.14'
|
6
|
+
gem 'simplecov', '>= 0.9'
|
7
|
+
gem 'rubocop', '>= 0.27'
|
8
|
+
gem 'yardstick'
|
9
|
+
if RUBY_VERSION >= '1.9.3'
|
10
|
+
gem 'guard', '~> 2.6'
|
11
|
+
gem 'guard-rspec', '~> 4.2'
|
12
|
+
end
|
13
|
+
gem 'webmock'
|
14
|
+
end
|
15
|
+
|
16
|
+
# Specify your gem's dependencies in flood.gemspec
|
17
|
+
gemspec
|
data/Guardfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
2
|
+
require 'guard/rspec/dsl'
|
3
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
4
|
+
|
5
|
+
# Feel free to open issues for suggestions and improvements
|
6
|
+
|
7
|
+
# RSpec files
|
8
|
+
rspec = dsl.rspec
|
9
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
10
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
11
|
+
watch(rspec.spec_files)
|
12
|
+
|
13
|
+
# Ruby files
|
14
|
+
ruby = dsl.ruby
|
15
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
16
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Moski Doski
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Starwars
|
2
|
+
|
3
|
+
A Ruby interface for http://swapi.co/ - the Star Wars API
|
4
|
+
![Starwars](https://s3.amazonaws.com/f.cl.ly/items/1r2F2K460v1R2o011n1M/star-wars-evolution-evolution-funny.jpg)
|
5
|
+
|
6
|
+
## API Support
|
7
|
+
- [x] Films API
|
8
|
+
- [x] People API
|
9
|
+
- [x] Planet API
|
10
|
+
- [x] Species API
|
11
|
+
- [x] Vehicles API
|
12
|
+
- [x] Starship API
|
13
|
+
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'starwars'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install starwars
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
require "starwars"
|
31
|
+
luke = Starwars::Person.fetch(1)
|
32
|
+
puts luke.name
|
33
|
+
puts luke.height
|
34
|
+
|
35
|
+
falcon = Starwars::Spaceship.new(id: 10)
|
36
|
+
falcon.fetch
|
37
|
+
puts falcon.name
|
38
|
+
puts falcon.pilots
|
39
|
+
|
40
|
+
you can also nest the quries, such as:
|
41
|
+
|
42
|
+
falcon.pilots.each do |piolt|
|
43
|
+
puts piolt.fetch.name
|
44
|
+
end
|
45
|
+
|
46
|
+
You can load a resouce by url:
|
47
|
+
|
48
|
+
aldeeran = Starwars::Planet.new(url: 'http://swapi.co/api/planets/3/').fetch
|
49
|
+
puts aldeeran.name
|
50
|
+
puts aldeeran.residents
|
51
|
+
puts aldeeran.population
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it ( https://github.com/moski/starwars/fork )
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
task test: :spec
|
9
|
+
|
10
|
+
require 'rubocop/rake_task'
|
11
|
+
RuboCop::RakeTask.new
|
12
|
+
|
13
|
+
require 'yard'
|
14
|
+
YARD::Rake::YardocTask.new
|
15
|
+
|
16
|
+
require 'yardstick/rake/measurement'
|
17
|
+
Yardstick::Rake::Measurement.new do |measurement|
|
18
|
+
measurement.output = 'measurement/report.txt'
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'yardstick/rake/verify'
|
22
|
+
Yardstick::Rake::Verify.new do |verify|
|
23
|
+
verify.threshold = 55
|
24
|
+
verify.require_exact_threshold = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: [:spec, :rubocop, :verify_measurements]
|
data/lib/starwars.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'starwars/version'
|
2
|
+
require 'starwars/base'
|
3
|
+
require 'starwars/fetcher'
|
4
|
+
|
5
|
+
# Avoid circuler require issues :(
|
6
|
+
module Starwars
|
7
|
+
class Planet < Starwars::Base; end
|
8
|
+
class Person < Starwars::Base; end
|
9
|
+
class Film < Starwars::Base; end
|
10
|
+
class Specie < Starwars::Base; end
|
11
|
+
class Vehicle < Starwars::Base; end
|
12
|
+
class Starship < Starwars::Base; end
|
13
|
+
end
|
14
|
+
require 'starwars/planet'
|
15
|
+
require 'starwars/person'
|
16
|
+
require 'starwars/specie'
|
17
|
+
require 'starwars/vehicle'
|
18
|
+
require 'starwars/starship'
|
19
|
+
require 'starwars/film'
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'roar/json'
|
2
|
+
require 'roar/client'
|
3
|
+
require 'roar/coercion'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'equalizer'
|
6
|
+
require 'virtus'
|
7
|
+
|
8
|
+
module Starwars
|
9
|
+
#
|
10
|
+
# Base Class for fetching all the data from the remote api.
|
11
|
+
# All Classes should inhert from this class.
|
12
|
+
#
|
13
|
+
class Base < OpenStruct
|
14
|
+
include Roar::JSON
|
15
|
+
include Roar::Client
|
16
|
+
include Roar::Coercion
|
17
|
+
|
18
|
+
BASE_URL = 'http://swapi.co/api'
|
19
|
+
FORMAT = 'application/json'
|
20
|
+
|
21
|
+
def ==(other)
|
22
|
+
id == other.id && url == other.url
|
23
|
+
end
|
24
|
+
|
25
|
+
def fetch(resource_name)
|
26
|
+
#
|
27
|
+
# If the url is set, then fetch using the resource url
|
28
|
+
#
|
29
|
+
if url
|
30
|
+
get(uri: url, as: FORMAT)
|
31
|
+
#
|
32
|
+
# Try to fetch using the resource name and ID
|
33
|
+
#
|
34
|
+
elsif id
|
35
|
+
get(uri: "#{BASE_URL}/#{resource_name}/#{id}/", as: FORMAT)
|
36
|
+
else
|
37
|
+
fail 'error'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
class << self
|
41
|
+
def fetch(resources, id)
|
42
|
+
#
|
43
|
+
# Special case for person and people, instead of removing the s
|
44
|
+
#
|
45
|
+
if (resources == 'people')
|
46
|
+
resources = 'person'
|
47
|
+
else
|
48
|
+
resources.slice!(-1, 1)
|
49
|
+
end
|
50
|
+
Starwars.const_get(resources.capitalize).new(id: id).fetch
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Starwars
|
2
|
+
#
|
3
|
+
# Common Module for the fetcher behaviour across all api references
|
4
|
+
#
|
5
|
+
module Fetcher
|
6
|
+
def self.included(base)
|
7
|
+
base.send :include, InstanceMethods
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
# Fetcher Methods to be included in every resource
|
12
|
+
module InstanceMethods
|
13
|
+
#
|
14
|
+
# Fetch a resouce give the resource name
|
15
|
+
#
|
16
|
+
def fetch
|
17
|
+
super(resouces_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def resouces_name
|
23
|
+
resource = self.class.name.split('::').last.downcase
|
24
|
+
(resource == 'person') ? 'people' : "#{resource}s"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Fetcher Class Method to be included in every resource
|
29
|
+
module ClassMethods
|
30
|
+
def fetch(id)
|
31
|
+
super(resouces_name, id)
|
32
|
+
end
|
33
|
+
|
34
|
+
def resouces_name
|
35
|
+
resource = name.split('::').last.downcase
|
36
|
+
(resource == 'person') ? 'people' : "#{resource}s"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Starwars
|
2
|
+
#
|
3
|
+
# A Film resource is an single film.
|
4
|
+
#
|
5
|
+
class Film < Starwars::Base
|
6
|
+
#
|
7
|
+
# Define the source name in the starwars api
|
8
|
+
#
|
9
|
+
RESOURCE_NAME = 'films'
|
10
|
+
|
11
|
+
include Starwars::Fetcher
|
12
|
+
|
13
|
+
# @return [String]
|
14
|
+
property :title
|
15
|
+
property :opening_crawl
|
16
|
+
property :director
|
17
|
+
property :producer
|
18
|
+
property :url
|
19
|
+
|
20
|
+
# @return [Integer]
|
21
|
+
property :id, type: Integer
|
22
|
+
property :episode_id, type: Integer
|
23
|
+
|
24
|
+
# @return [Time]
|
25
|
+
property :created, type: Time
|
26
|
+
property :edited, type: Time
|
27
|
+
|
28
|
+
# @return [Array]
|
29
|
+
collection :characters, class: Starwars::Person, deserialize: ->(_, fragment, _) { Person.new(url: fragment) }
|
30
|
+
collection :planets, class: Starwars::Planet, deserialize: ->(_, fragment, _) { Planet.new(url: fragment) }
|
31
|
+
collection :starships, class: Starwars::Starship, deserialize: ->(_, fragment, _) { Starship.new(url: fragment) }
|
32
|
+
collection :vehicles, class: Starwars::Vehicle, deserialize: ->(_, fragment, _) { Vehicle.new(url: fragment) }
|
33
|
+
collection :species, class: Starwars::Specie, deserialize: ->(_, fragment, _) { Specie.new(url: fragment) }
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Starwars
|
2
|
+
#
|
3
|
+
# A People resource is an individual person or character within the Star Wars universe.
|
4
|
+
#
|
5
|
+
class Person < Starwars::Base
|
6
|
+
#
|
7
|
+
# Define the source name in the starwars api
|
8
|
+
#
|
9
|
+
RESOURCE_NAME = 'people'
|
10
|
+
|
11
|
+
include Starwars::Fetcher
|
12
|
+
|
13
|
+
# @return [String]
|
14
|
+
property :name
|
15
|
+
property :skin_color
|
16
|
+
property :eye_color
|
17
|
+
property :hair_color
|
18
|
+
property :birth_year
|
19
|
+
property :gender
|
20
|
+
property :url
|
21
|
+
|
22
|
+
# @return [Integer]
|
23
|
+
property :height, type: Integer
|
24
|
+
property :mass, type: Integer
|
25
|
+
|
26
|
+
# @return [Starwars::Planet]
|
27
|
+
property :homeworld, class: Starwars::Planet, deserialize: ->(_, fragment, _) { Planet.new(url: fragment) }
|
28
|
+
|
29
|
+
# @return [Time]
|
30
|
+
property :created, type: Time
|
31
|
+
property :edited, type: Time
|
32
|
+
|
33
|
+
# @return [Array]
|
34
|
+
collection :films, class: Starwars::Film, deserialize: ->(_, fragment, _) { Film.new(url: fragment) }
|
35
|
+
collection :species, class: Starwars::Specie, deserialize: ->(_, fragment, _) { Specie.new(url: fragment) }
|
36
|
+
collection :vehicles, class: Starwars::Vehicle, deserialize: ->(_, fragment, _) { Vehicle.new(url: fragment) }
|
37
|
+
collection :starships, class: Starwars::Starship, deserialize: ->(_, fragment, _) { Starship.new(url: fragment) }
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Starwars
|
2
|
+
#
|
3
|
+
# A Planet resource is a large mass, planet or planetoid in
|
4
|
+
# the Star Wars Universe, at the time of 0 ABY.
|
5
|
+
#
|
6
|
+
class Planet < Starwars::Base
|
7
|
+
#
|
8
|
+
# Define the source name in the starwars api
|
9
|
+
#
|
10
|
+
RESOURCE_NAME = 'planets'
|
11
|
+
|
12
|
+
include Starwars::Fetcher
|
13
|
+
|
14
|
+
# @return [String]
|
15
|
+
property :name
|
16
|
+
property :climate
|
17
|
+
property :gravity
|
18
|
+
property :terrain
|
19
|
+
property :url
|
20
|
+
|
21
|
+
# @return [Integer]
|
22
|
+
property :id, type: Integer
|
23
|
+
property :population, type: Integer
|
24
|
+
property :rotation_period, type: Integer
|
25
|
+
property :orbital_period, type: Integer
|
26
|
+
property :surface_water, type: Integer
|
27
|
+
property :diameter, type: Integer
|
28
|
+
|
29
|
+
# @return [Time]
|
30
|
+
property :created, type: Time
|
31
|
+
property :edited, type: Time
|
32
|
+
|
33
|
+
# @return [Array]
|
34
|
+
collection :residents, class: Starwars::Person, deserialize: ->(_, fragment, _) { Person.new(url: fragment) }
|
35
|
+
collection :films, class: Starwars::Film, deserialize: ->(_, fragment, _) { Film.new(url: fragment) }
|
36
|
+
end
|
37
|
+
end
|