starwars 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/.travis.yml +1 -0
  4. data/README.md +63 -19
  5. data/lib/starwars.rb +16 -7
  6. data/lib/starwars/base.rb +129 -25
  7. data/lib/starwars/cursor.rb +110 -0
  8. data/lib/starwars/error.rb +63 -0
  9. data/lib/starwars/overrides/ostruct.rb +16 -0
  10. data/lib/starwars/request.rb +80 -0
  11. data/lib/starwars/resources/film.rb +79 -0
  12. data/lib/starwars/resources/paginated/films.rb +8 -0
  13. data/lib/starwars/resources/paginated/people.rb +8 -0
  14. data/lib/starwars/resources/paginated/planets.rb +9 -0
  15. data/lib/starwars/resources/paginated/species.rb +8 -0
  16. data/lib/starwars/resources/paginated/starships.rb +8 -0
  17. data/lib/starwars/resources/paginated/vehicles.rb +8 -0
  18. data/lib/starwars/resources/person.rb +124 -0
  19. data/lib/starwars/{planet.rb → resources/planet.rb} +0 -13
  20. data/lib/starwars/{specie.rb → resources/specie.rb} +1 -15
  21. data/lib/starwars/resources/starship.rb +115 -0
  22. data/lib/starwars/resources/vehicle.rb +106 -0
  23. data/lib/starwars/version.rb +1 -1
  24. data/spec/fixtures/404.json +1 -0
  25. data/spec/fixtures/planets_page1.json +1 -0
  26. data/spec/fixtures/planets_page2.json +1 -0
  27. data/spec/starwars/base_spec.rb +17 -0
  28. data/spec/starwars/cursor_spec.rb +76 -0
  29. data/spec/starwars/person_spec.rb +1 -1
  30. data/spec/starwars/request_spec.rb +29 -0
  31. data/spec/starwars/resources/paginated/planets_spec.rb +38 -0
  32. data/starwars.gemspec +0 -1
  33. metadata +32 -23
  34. data/lib/starwars/fetcher.rb +0 -40
  35. data/lib/starwars/film.rb +0 -35
  36. data/lib/starwars/person.rb +0 -39
  37. data/lib/starwars/starship.rb +0 -40
  38. data/lib/starwars/vehicle.rb +0 -38
@@ -1,6 +1,6 @@
1
1
  module Starwars
2
2
  #
3
- # A Film resource is an single film.
3
+ # A Species resource is a type of person or character within the Star Wars Universe.
4
4
  #
5
5
  class Specie < Starwars::Base
6
6
  #
@@ -8,9 +8,6 @@ module Starwars
8
8
  #
9
9
  RESOURCE_NAME = 'species'
10
10
 
11
- include Starwars::Fetcher
12
-
13
- # @return [String]
14
11
  property :name
15
12
  property :classification
16
13
  property :designation
@@ -18,20 +15,9 @@ module Starwars
18
15
  property :hair_colors
19
16
  property :eye_colors
20
17
  property :language
21
- property :url
22
-
23
- # @return [Starwars::Planet]
24
18
  property :homeworld, class: Starwars::Planet, deserialize: ->(_, fragment, _) { Planet.new(url: fragment) }
25
-
26
- # @return [Integer]
27
19
  property :average_height, type: Integer
28
20
  property :average_lifespan, type: Integer
29
-
30
- # @return [Time]
31
- property :created, type: Time
32
- property :edited, type: Time
33
-
34
- # @return [Array]
35
21
  collection :people, class: Starwars::Person, deserialize: ->(_, fragment, _) { Person.new(url: fragment) }
36
22
  collection :films, class: Starwars::Film, deserialize: ->(_, fragment, _) { Film.new(url: fragment) }
37
23
  end
@@ -0,0 +1,115 @@
1
+ module Starwars
2
+ #
3
+ # A Starship resource is a single transport craft that has hyperdrive capability.
4
+ #
5
+ class Starship < Starwars::Base
6
+ #
7
+ # Define the source name in the starwars api
8
+ #
9
+ RESOURCE_NAME = 'starships'
10
+
11
+ # The name of this starship
12
+ #
13
+ # @return [String]
14
+ #
15
+ # @note The common name, such as "Death Star".
16
+ #
17
+ # @example
18
+ # starship.name #=> 'Death Star'
19
+ # @api public
20
+ property :name
21
+
22
+ # The model or official name of this starship
23
+ # @return [String]
24
+ #
25
+ # @example
26
+ # starship.model #=> 'DS-1 Orbital Battle Station'
27
+ # @api public
28
+ property :model
29
+
30
+ # The manufacturer of the starship
31
+ # @return [String]
32
+ # @example
33
+ # starship.manufacturer #=> 'Imperial Department of Military Research'
34
+ # @api public
35
+ property :manufacturer
36
+
37
+ # The class of this starship
38
+ # @return [String]
39
+ # @example
40
+ # starship.starship_class #=> 'Starfighter'
41
+ # @api public
42
+ property :starship_class
43
+
44
+ # consumables
45
+ # @return [String]
46
+ # @example
47
+ # starship.consumables
48
+ # @api public
49
+ property :consumables
50
+
51
+ # The cost of this new starship, in Galactic Credits
52
+ # @return [String]
53
+ # @example
54
+ # starship.cost_in_credits
55
+ # @api public
56
+ property :cost_in_credits, type: Integer
57
+
58
+ # The length of this starship in meters
59
+ # @return [String]
60
+ # @example
61
+ # starship.length
62
+ # @api public
63
+ property :length, type: Integer
64
+
65
+ # The maximum speed of this starship in atmosphere
66
+ # @return [Integer]
67
+ # @example
68
+ # starship.max_atmosphering_speed
69
+ # @api public
70
+ property :max_atmosphering_speed, type: Integer
71
+
72
+ # The number of personnel needed to run or pilot this starship
73
+ # @return [Integer]
74
+ # @example
75
+ # starship.crew
76
+ # @api public
77
+ property :crew, type: Integer
78
+
79
+ # The number of non-essential people this starship can transport
80
+ # @return [Integer]
81
+ # @example
82
+ # starship.passengers
83
+ # @api public
84
+ property :passengers, type: Integer
85
+
86
+ # The maximum number of kilograms that this starship can transport
87
+ # @return [Integer]
88
+ # @example
89
+ # starship.cargo_capacity
90
+ # @api public
91
+ property :cargo_capacity, type: Integer
92
+ # property :MGLT, as: :mglt
93
+
94
+ # The class of this starships hyperdrive
95
+ # @return [Float]
96
+ # @example
97
+ # starship.hyperdrive_rating
98
+ # @api public
99
+ property :hyperdrive_rating, type: Float
100
+
101
+ # List of pilots
102
+ # @return [Array<Pilot>]
103
+ # @example
104
+ # starship.pilots
105
+ # @api public
106
+ collection :pilots, class: Starwars::Person, deserialize: ->(_, fragment, _) { Person.new(url: fragment) }
107
+
108
+ # List of films
109
+ # @return [Array<Film>]
110
+ # @example
111
+ # starship.films
112
+ # @api public
113
+ collection :films, class: Starwars::Film, deserialize: ->(_, fragment, _) { Film.new(url: fragment) }
114
+ end
115
+ end
@@ -0,0 +1,106 @@
1
+ module Starwars
2
+ #
3
+ # A Vehicle resource is a single transport craft that does not have hyperdrive capability.
4
+ #
5
+ class Vehicle < Starwars::Base
6
+ #
7
+ # Define the source name in the starwars api
8
+ #
9
+ RESOURCE_NAME = 'vehicles'
10
+
11
+ # The name of this vehicle
12
+ #
13
+ # @return [String]
14
+ #
15
+ # @note The common name, such as "Sand Crawler" or "Speeder bike"
16
+ #
17
+ # @example
18
+ # vehicle.name #=> 'Snowspeeder'
19
+ # @api public
20
+ property :name
21
+
22
+ # The model or official name of this vehicle
23
+ # @return [String]
24
+ # @example
25
+ # vehicle.model #=> 't-47 airspeeder'
26
+ # @api public
27
+ property :model
28
+
29
+ # The manufacturer of this vehicle. Comma-seperated if more than one
30
+ # @return [String]
31
+ # @example
32
+ # vehicle.manufacturer #=> 'Incom corporation'
33
+ # @api public
34
+ property :manufacturer
35
+
36
+ # consumables
37
+ # @return [String]
38
+ # @example
39
+ # vehicle.consumables
40
+ # @api public
41
+ property :consumables
42
+
43
+ # The class of this vehicle, such as "Wheeled" or "Repulsorcraft"
44
+ # @return [String]
45
+ # @example
46
+ # vehicle.vehicle_class #=> 'airspeeder'
47
+ # @api public
48
+ property :vehicle_class
49
+
50
+ # The cost of this vehicle new, in Galactic Credits
51
+ # @return [String]
52
+ # @example
53
+ # vehicle.cost_in_credits
54
+ # @api public
55
+ property :cost_in_credits
56
+
57
+ # The maximum speed of this vehicle in atmosphere
58
+ # @return [Integer]
59
+ # @example
60
+ # vehicle.max_atmosphering_speed
61
+ # @api public
62
+ property :max_atmosphering_speed, type: Integer
63
+
64
+ # The number of personnel needed to run or pilot this vehicle
65
+ # @return [Integer]
66
+ # @example
67
+ # vehicle.crew
68
+ # @api public
69
+ property :crew, type: Integer
70
+
71
+ # The number of non-essential people this vehicle can transport
72
+ # @return [Integer]
73
+ # @example
74
+ # vehicle.passengers
75
+ # @api public
76
+ property :passengers, type: Integer
77
+
78
+ # The maximum number of kilograms that this vehicle can transport
79
+ # @return [Integer]
80
+ # @example
81
+ # vehicle.cargo_capacity
82
+ # @api public
83
+ property :cargo_capacity, type: Integer
84
+
85
+ # The length of this vehicle in meters
86
+ # @return [Float]
87
+ # @example
88
+ # vehicle.length
89
+ # @api public
90
+ property :length, type: Float
91
+
92
+ # List of pilots
93
+ # @return [Array<Pilot>]
94
+ # @example
95
+ # vehicle.pilots
96
+ # @api public
97
+ collection :pilots, class: Starwars::Person, deserialize: ->(_, fragment, _) { Person.new(url: fragment) }
98
+
99
+ # List of films
100
+ # @return [Array<Film>]
101
+ # @example
102
+ # vehicle.films
103
+ # @api public
104
+ collection :films, class: Starwars::Film, deserialize: ->(_, fragment, _) { Film.new(url: fragment) }
105
+ end
106
+ end
@@ -1,4 +1,4 @@
1
1
  # Define the gem version
2
2
  module Starwars
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
@@ -0,0 +1 @@
1
+ {"detail":"Not found"}
@@ -0,0 +1 @@
1
+ {"count":60,"next":"http://swapi.co/api/planets/?page=2","previous":null,"results":[{"name":"Alderaan","rotation_period":"24","orbital_period":"364","diameter":"12500","climate":"temperate","gravity":"1 standard","terrain":"grasslands, mountains","surface_water":"40","population":"2000000000","residents":["http://swapi.co/api/people/5/","http://swapi.co/api/people/68/","http://swapi.co/api/people/81/"],"films":["http://swapi.co/api/films/1/","http://swapi.co/api/films/6/"],"created":"2014-12-10T11:35:48.479000Z","edited":"2014-12-20T20:58:18.420000Z","url":"http://swapi.co/api/planets/2/"},{"name":"Yavin IV","rotation_period":"24","orbital_period":"4818","diameter":"10200","climate":"temperate, tropical","gravity":"1 standard","terrain":"jungle, rainforests","surface_water":"8","population":"1000","residents":[],"films":["http://swapi.co/api/films/1/"],"created":"2014-12-10T11:37:19.144000Z","edited":"2014-12-20T20:58:18.421000Z","url":"http://swapi.co/api/planets/3/"},{"name":"Hoth","rotation_period":"23","orbital_period":"549","diameter":"7200","climate":"frozen","gravity":"1.1 standard","terrain":"tundra, ice caves, mountain ranges","surface_water":"100","population":"unknown","residents":[],"films":["http://swapi.co/api/films/2/"],"created":"2014-12-10T11:39:13.934000Z","edited":"2014-12-20T20:58:18.423000Z","url":"http://swapi.co/api/planets/4/"},{"name":"Dagobah","rotation_period":"23","orbital_period":"341","diameter":"8900","climate":"murky","gravity":"N/A","terrain":"swamp, jungles","surface_water":"8","population":"unknown","residents":[],"films":["http://swapi.co/api/films/2/","http://swapi.co/api/films/3/","http://swapi.co/api/films/6/"],"created":"2014-12-10T11:42:22.590000Z","edited":"2014-12-20T20:58:18.425000Z","url":"http://swapi.co/api/planets/5/"},{"name":"Bespin","rotation_period":"12","orbital_period":"5110","diameter":"118000","climate":"temperate","gravity":"1.5 (surface), 1 standard (Cloud City)","terrain":"gas giant","surface_water":"0","population":"6000000","residents":["http://swapi.co/api/people/26/"],"films":["http://swapi.co/api/films/2/"],"created":"2014-12-10T11:43:55.240000Z","edited":"2014-12-20T20:58:18.427000Z","url":"http://swapi.co/api/planets/6/"},{"name":"Endor","rotation_period":"18","orbital_period":"402","diameter":"4900","climate":"temperate","gravity":"0.85 standard","terrain":"forests, mountains, lakes","surface_water":"8","population":"30000000","residents":["http://swapi.co/api/people/30/"],"films":["http://swapi.co/api/films/3/"],"created":"2014-12-10T11:50:29.349000Z","edited":"2014-12-20T20:58:18.429000Z","url":"http://swapi.co/api/planets/7/"},{"name":"Naboo","rotation_period":"26","orbital_period":"312","diameter":"12120","climate":"temperate","gravity":"1 standard","terrain":"grassy hills, swamps, forests, mountains","surface_water":"12","population":"4500000000","residents":["http://swapi.co/api/people/3/","http://swapi.co/api/people/21/","http://swapi.co/api/people/35/","http://swapi.co/api/people/36/","http://swapi.co/api/people/37/","http://swapi.co/api/people/38/","http://swapi.co/api/people/39/","http://swapi.co/api/people/42/","http://swapi.co/api/people/60/","http://swapi.co/api/people/61/","http://swapi.co/api/people/66/"],"films":["http://swapi.co/api/films/3/","http://swapi.co/api/films/4/","http://swapi.co/api/films/5/","http://swapi.co/api/films/6/"],"created":"2014-12-10T11:52:31.066000Z","edited":"2014-12-20T20:58:18.430000Z","url":"http://swapi.co/api/planets/8/"},{"name":"Coruscant","rotation_period":"24","orbital_period":"368","diameter":"12240","climate":"temperate","gravity":"1 standard","terrain":"cityscape, mountains","surface_water":"unknown","population":"1000000000000","residents":["http://swapi.co/api/people/34/","http://swapi.co/api/people/55/","http://swapi.co/api/people/74/"],"films":["http://swapi.co/api/films/3/","http://swapi.co/api/films/4/","http://swapi.co/api/films/5/","http://swapi.co/api/films/6/"],"created":"2014-12-10T11:54:13.921000Z","edited":"2014-12-20T20:58:18.432000Z","url":"http://swapi.co/api/planets/9/"},{"name":"Kamino","rotation_period":"27","orbital_period":"463","diameter":"19720","climate":"temperate","gravity":"1 standard","terrain":"ocean","surface_water":"100","population":"1000000000","residents":["http://swapi.co/api/people/22/","http://swapi.co/api/people/72/","http://swapi.co/api/people/73/"],"films":["http://swapi.co/api/films/5/"],"created":"2014-12-10T12:45:06.577000Z","edited":"2014-12-20T20:58:18.434000Z","url":"http://swapi.co/api/planets/10/"},{"name":"Geonosis","rotation_period":"30","orbital_period":"256","diameter":"11370","climate":"temperate, arid","gravity":"0.9 standard","terrain":"rock, desert, mountain, barren","surface_water":"5","population":"100000000000","residents":["http://swapi.co/api/people/63/"],"films":["http://swapi.co/api/films/5/"],"created":"2014-12-10T12:47:22.350000Z","edited":"2014-12-20T20:58:18.437000Z","url":"http://swapi.co/api/planets/11/"}]}
@@ -0,0 +1 @@
1
+ {"count":60,"next":"http://swapi.co/api/planets/?page=3","previous":"http://swapi.co/api/planets/?page=1","results":[{"name":"Utapau","rotation_period":"27","orbital_period":"351","diameter":"12900","climate":"temperate, arid, windy","gravity":"1 standard","terrain":"scrublands, savanna, canyons, sinkholes","surface_water":"0.9","population":"95000000","residents":["http://swapi.co/api/people/83/"],"films":["http://swapi.co/api/films/6/"],"created":"2014-12-10T12:49:01.491000Z","edited":"2014-12-20T20:58:18.439000Z","url":"http://swapi.co/api/planets/12/"},{"name":"Mustafar","rotation_period":"36","orbital_period":"412","diameter":"4200","climate":"hot","gravity":"1 standard","terrain":"volcanoes, lava rivers, mountains, caves","surface_water":"0","population":"20000","residents":[],"films":["http://swapi.co/api/films/6/"],"created":"2014-12-10T12:50:16.526000Z","edited":"2014-12-20T20:58:18.440000Z","url":"http://swapi.co/api/planets/13/"},{"name":"Kashyyyk","rotation_period":"26","orbital_period":"381","diameter":"12765","climate":"tropical","gravity":"1 standard","terrain":"jungle, forests, lakes, rivers","surface_water":"60","population":"45000000","residents":["http://swapi.co/api/people/13/","http://swapi.co/api/people/80/"],"films":["http://swapi.co/api/films/6/"],"created":"2014-12-10T13:32:00.124000Z","edited":"2014-12-20T20:58:18.442000Z","url":"http://swapi.co/api/planets/14/"},{"name":"Polis Massa","rotation_period":"24","orbital_period":"590","diameter":"0","climate":"artificial temperate ","gravity":"0.56 standard","terrain":"airless asteroid","surface_water":"0","population":"1000000","residents":[],"films":["http://swapi.co/api/films/6/"],"created":"2014-12-10T13:33:46.405000Z","edited":"2014-12-20T20:58:18.444000Z","url":"http://swapi.co/api/planets/15/"},{"name":"Mygeeto","rotation_period":"12","orbital_period":"167","diameter":"10088","climate":"frigid","gravity":"1 standard","terrain":"glaciers, mountains, ice canyons","surface_water":"unknown","population":"19000000","residents":[],"films":["http://swapi.co/api/films/6/"],"created":"2014-12-10T13:43:39.139000Z","edited":"2014-12-20T20:58:18.446000Z","url":"http://swapi.co/api/planets/16/"},{"name":"Felucia","rotation_period":"34","orbital_period":"231","diameter":"9100","climate":"hot, humid","gravity":"0.75 standard","terrain":"fungus forests","surface_water":"unknown","population":"8500000","residents":[],"films":["http://swapi.co/api/films/6/"],"created":"2014-12-10T13:44:50.397000Z","edited":"2014-12-20T20:58:18.447000Z","url":"http://swapi.co/api/planets/17/"},{"name":"Cato Neimoidia","rotation_period":"25","orbital_period":"278","diameter":"0","climate":"temperate, moist","gravity":"1 standard","terrain":"mountains, fields, forests, rock arches","surface_water":"unknown","population":"10000000","residents":["http://swapi.co/api/people/33/"],"films":["http://swapi.co/api/films/6/"],"created":"2014-12-10T13:46:28.704000Z","edited":"2014-12-20T20:58:18.449000Z","url":"http://swapi.co/api/planets/18/"},{"name":"Saleucami","rotation_period":"26","orbital_period":"392","diameter":"14920","climate":"hot","gravity":"unknown","terrain":"caves, desert, mountains, volcanoes","surface_water":"unknown","population":"1400000000","residents":[],"films":["http://swapi.co/api/films/6/"],"created":"2014-12-10T13:47:46.874000Z","edited":"2014-12-20T20:58:18.450000Z","url":"http://swapi.co/api/planets/19/"},{"name":"Stewjon","rotation_period":"unknown","orbital_period":"unknown","diameter":"0","climate":"temperate","gravity":"1 standard","terrain":"grass","surface_water":"unknown","population":"unknown","residents":["http://swapi.co/api/people/10/"],"films":[],"created":"2014-12-10T16:16:26.566000Z","edited":"2014-12-20T20:58:18.452000Z","url":"http://swapi.co/api/planets/20/"},{"name":"Eriadu","rotation_period":"24","orbital_period":"360","diameter":"13490","climate":"polluted","gravity":"1 standard","terrain":"cityscape","surface_water":"unknown","population":"22000000000","residents":["http://swapi.co/api/people/12/"],"films":[],"created":"2014-12-10T16:26:54.384000Z","edited":"2014-12-20T20:58:18.454000Z","url":"http://swapi.co/api/planets/21/"}]}
@@ -0,0 +1,17 @@
1
+ describe Starwars::Base do
2
+ before do
3
+ stub_get('/planets/100/').to_return(status: [404, 'NOT FOUND'], body: fixture('404.json'), headers: { content_type: 'application/json; charset=utf-8' })
4
+ stub_get('/planets/1/').to_return(body: fixture('planets_1.json'), headers: { content_type: 'application/json; charset=utf-8' })
5
+ end
6
+ describe '#fetch' do
7
+ it 'should raise an exception when argument error' do
8
+ base = Starwars::Base.new
9
+ expect { base.fetch }.to raise_error(Starwars::Error::ArgumentError)
10
+ end
11
+ end
12
+ describe '#link' do
13
+ it 'should return the same string if passed a string' do
14
+ expect(Starwars::Base.send('link', '/something')).to eq '/something'
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,76 @@
1
+ describe Starwars::Cursor do
2
+ let(:resource) { Starwars::Planet }
3
+ let(:uri) { 'http://swapi.co/api/planets/' }
4
+ let(:as) { Starwars::Request::FORMAT }
5
+ let(:params) { { two: 2 } }
6
+ let(:method) { :get }
7
+ let(:cursor) do
8
+ Starwars::Cursor.new
9
+ end
10
+
11
+ describe '#last_page?' do
12
+ it 'returns true if so' do
13
+ cursor.next = nil
14
+ expect(cursor.last_page?).to eq(true)
15
+ end
16
+
17
+ it 'returns false if not' do
18
+ cursor.next = '/something'
19
+ expect(cursor.last_page?).to eq(false)
20
+ end
21
+ end
22
+
23
+ describe '#next_page?' do
24
+ it 'returns true if so' do
25
+ cursor.next = '/something'
26
+ expect(cursor.next_page?).to eq(true)
27
+ end
28
+
29
+ it 'returns false if not' do
30
+ cursor.next = nil
31
+ expect(cursor.next_page?).to eq(false)
32
+ end
33
+ end
34
+
35
+ describe '#next_page' do
36
+ let(:request_response) do
37
+ double(body: { results: [], next: '/something', pervious: nil })
38
+ end
39
+ before :each do
40
+ allow(cursor).to receive(:perform_request).and_return(request_response)
41
+ end
42
+
43
+ it 'returns nil if there are no more pages' do
44
+ cursor.next = nil
45
+ expect(cursor.next_page).to eq(nil)
46
+ end
47
+
48
+ it 'performs a request' do
49
+ cursor.next = 'http://swapi.co/api/planets/?page=2'
50
+ expect(cursor).to receive(:perform_request)
51
+ cursor.next_page
52
+ end
53
+ end
54
+
55
+ describe '#each_page' do
56
+ it 'should return an enumerator if no block is provided' do
57
+ expect(cursor.each_page).to be_an(Enumerator)
58
+ end
59
+
60
+ it 'yields till no more pages' do
61
+ request_responses = [
62
+ double(body: { previous: nil, results: [1, 2, 3, 4] }),
63
+ double(body: { next: '/next/next', previous: '/previous', results: [1, 2, 3, 4] }),
64
+ double(body: { next: nil, previous: '/previous', results: [1, 2, 3, 4] })
65
+ ]
66
+ cursor.next = '/something'
67
+ allow(cursor).to receive(:perform_request).and_return(request_responses[0])
68
+ expect(cursor).to receive(:perform_request).exactly(4).times
69
+
70
+ cursor.each_with_index do |_, index|
71
+ allow(cursor).to receive(:perform_request).and_return(request_responses[index])
72
+ cursor.next = request_responses[index]
73
+ end
74
+ end
75
+ end
76
+ end
@@ -19,7 +19,7 @@ describe Starwars::Person do
19
19
  end
20
20
 
21
21
  describe 'getters' do
22
- subject(:person_2) { Starwars::Person.fetch(2) }
22
+ subject(:person_2) { Starwars::Person.new(id: 2).fetch }
23
23
  subject(:person_1) { Starwars::Person.fetch(1) }
24
24
  describe '#created' do
25
25
  it 'returns a Time when updated is set' do
@@ -0,0 +1,29 @@
1
+ describe Starwars::Request do
2
+ let(:resource) { Starwars::Planet }
3
+ let(:uri) { 'http://swapi.co/api/planets/100/' }
4
+ let(:as) { Starwars::Request::FORMAT }
5
+ let(:params) { { two: 2 } }
6
+ let(:method) { :get }
7
+
8
+ let(:request) do
9
+ Starwars::Request.new(resource: resource.new, uri: uri, method: method, as: as, params: { moski: true })
10
+ end
11
+
12
+ before do
13
+ stub_get('/planets/100/').to_return(status: [404, 'NOT FOUND'], body: fixture('404.json'), headers: { content_type: 'application/json; charset=utf-8' })
14
+ stub_get('/planets/1/').to_return(body: fixture('planets_1.json'), headers: { content_type: 'application/json; charset=utf-8' })
15
+ end
16
+
17
+ describe '#perform_request' do
18
+ let(:perform_request) { -> { request.send(:perform_request) } }
19
+ it 'runs the request and verifies it' do
20
+ allow(request).to receive(:perform_request)
21
+ expect(request).to receive(:perform_request)
22
+ perform_request.call
23
+ end
24
+
25
+ it 'raises an exception when data not found' do
26
+ expect { request.perform_request }.to raise_error(Starwars::Error::NotFound)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,38 @@
1
+ describe Starwars::Planets do
2
+ before do
3
+ stub_get('/planets/').to_return(body: fixture('planets_page1.json'), headers: { content_type: 'application/json; charset=utf-8' })
4
+ stub_get('/planets/?page=2').to_return(body: fixture('planets_page2.json'), headers: { content_type: 'application/json; charset=utf-8' })
5
+ end
6
+ describe 'getters' do
7
+ subject(:planets) { Starwars::Planet.fetch_all }
8
+ describe '#number_of_pages' do
9
+ it 'should have the correct value' do
10
+ expect(planets.number_of_pages).to eq 60
11
+ end
12
+ end
13
+ describe '#next' do
14
+ it 'should have the correct value' do
15
+ expect(planets.next).to eq 'http://swapi.co/api/planets/?page=2'
16
+ end
17
+ end
18
+ describe '#previous' do
19
+ it 'should have the correct value' do
20
+ expect(planets.previous).to eq nil
21
+ end
22
+ end
23
+ describe '#results' do
24
+ it 'should have the correct size' do
25
+ expect(planets.results.size).to eq 10
26
+ end
27
+ it 'should have the correct type' do
28
+ expect(planets.results.first).to be_a Starwars::Planet
29
+ end
30
+ end
31
+ describe '#next_page' do
32
+ it 'should fetch the next page' do
33
+ planets.next_page
34
+ expect(planets.next).to eq 'http://swapi.co/api/planets/?page=3'
35
+ end
36
+ end
37
+ end
38
+ end