red5 0.0.2 → 0.0.3

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 (59) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -0
  3. data/README.md +55 -1
  4. data/Rakefile +3 -1
  5. data/lib/red_5.rb +19 -0
  6. data/lib/red_5/exceptions/red_5_exception.rb +9 -0
  7. data/lib/red_5/helpers.rb +8 -0
  8. data/lib/red_5/models/entities.rb +54 -0
  9. data/lib/{red5 → red_5/models}/films.rb +1 -1
  10. data/lib/{red5 → red_5/models}/people.rb +1 -1
  11. data/lib/{red5 → red_5/models}/planets.rb +1 -1
  12. data/lib/{red5 → red_5/models}/species.rb +1 -1
  13. data/lib/{red5 → red_5/models}/starships.rb +1 -1
  14. data/lib/{red5 → red_5/models}/vehicles.rb +1 -1
  15. data/lib/{red5 → red_5}/root.rb +1 -1
  16. data/lib/red_5/version.rb +3 -0
  17. data/red5.gemspec +4 -1
  18. data/spec/cassettes/Failure/404s/raises_an_exception_on_a_ludicrous_request.yml +96 -0
  19. data/spec/cassettes/Failure/404s/raises_an_exception_on_a_non-existent_thing.yml +96 -0
  20. data/spec/cassettes/Failure/Non-existent_attributes/raises_an_exception_on_a_bogus_lookup.yml +101 -0
  21. data/spec/cassettes/Red5_Films/finds_a_particular_film.yml +111 -0
  22. data/spec/cassettes/Red5_Films/has_OpenStruct-type_lookups.yml +111 -0
  23. data/spec/cassettes/Red5_Films/returns_a_film.yml +111 -0
  24. data/spec/cassettes/Red5_People/fetches_the_species_for_a_person.yml +155 -0
  25. data/spec/cassettes/{Red_5_People/should_find_a_person.yml → Red5_People/finds_a_particular_person.yml} +13 -13
  26. data/spec/cassettes/Red5_People/has_an_id.yml +101 -0
  27. data/spec/cassettes/Red5_People/returns_a_person.yml +101 -0
  28. data/spec/cassettes/{Red_5_Planets/should_find_a_planet.yml → Red5_Planets/finds_a_planet.yml} +18 -18
  29. data/spec/cassettes/Red5_Species/fetches_the_homeworld_for_a_species.yml +157 -0
  30. data/spec/cassettes/Red5_Species/finds_a_homeworld_for_a_species.yml +101 -0
  31. data/spec/cassettes/Red5_Species/finds_a_species.yml +101 -0
  32. data/spec/cassettes/Red5_Starships/Failure/raises_an_exception_on_a_ludicrous_request.yml +96 -0
  33. data/spec/cassettes/Red5_Starships/Failure/raises_an_exception_on_a_non-existent_thing.yml +96 -0
  34. data/spec/cassettes/Red5_Starships/finds_a_starship.yml +102 -0
  35. data/spec/cassettes/Red5_Vehicles/finds_a_vehicle.yml +102 -0
  36. data/spec/cassettes/Red_5_Films/finds_a_particular_film.yml +111 -0
  37. data/spec/cassettes/Red_5_Films/returns_a_film.yml +111 -0
  38. data/spec/cassettes/Red_5_People/fetches_the_species_for_a_person.yml +155 -0
  39. data/spec/cassettes/Red_5_People/finds_a_particular_person.yml +101 -0
  40. data/spec/cassettes/Red_5_People/gets_the_species_for_a_person.yml +252 -0
  41. data/spec/cassettes/Red_5_People/has_an_id.yml +101 -0
  42. data/spec/cassettes/Red_5_People/returns_a_person.yml +101 -0
  43. data/spec/cassettes/Red_5_Species/fetches_the_homeworld_for_a_species.yml +157 -0
  44. data/spec/cassettes/Red_5_Species/finds_a_homeworld_for_a_species.yml +101 -0
  45. data/spec/cassettes/Red_5_Species/gets_the_homeworld_for_a_species.yml +157 -0
  46. data/spec/cassettes/Red_5_Starships/Failure/raises_an_exception_on_a_non-existent_thing.yml +96 -0
  47. data/spec/failure_spec.rb +31 -0
  48. data/spec/models/films_spec.rb +15 -0
  49. data/spec/models/people_spec.rb +25 -0
  50. data/spec/models/planets_spec.rb +10 -0
  51. data/spec/models/species_spec.rb +20 -0
  52. data/spec/models/starships_spec.rb +10 -0
  53. data/spec/models/vehicles_spec.rb +10 -0
  54. data/spec/red5_spec.rb +2 -45
  55. data/spec/spec_helper.rb +1 -1
  56. metadata +129 -17
  57. data/lib/red5.rb +0 -14
  58. data/lib/red5/entities.rb +0 -9
  59. data/lib/red5/version.rb +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1cc0bb9c1ac63bfe093999941d97cc3c23669dde
4
- data.tar.gz: e637e3d181140613b101c1f3cad14380fc5c0e04
3
+ metadata.gz: d84a79a3bcb4b6cca90a470703d6dca96f67c080
4
+ data.tar.gz: 203496fc2fd5b6590d8cb20fd75bd2aeb28f2319
5
5
  SHA512:
6
- metadata.gz: ec3efe49f6f54dfe101d859670c9fce2d55d0a73b2f7378a5e3461fc34ffe8092070746edaaf17955d2d83b4cdfe6ef56d9531c413ba71b305379e42aa68e305
7
- data.tar.gz: faee78ecede73f6f88cfaf3fbda81c92eb322da9cb0b665dfd13b254333ff519d13497f2867ccdcaffa0a077961c9ce11b915d78718d46aa1506abb38392d17d
6
+ metadata.gz: df3ef0cb1d6a0a11dd16dfff9267fbb9d684009e22f26617eca62272ef3a4d90501bc1d29d8c83fe3735954544da149ddbde5ff045ea1331afb8ca36a84f93a7
7
+ data.tar.gz: 7931602b478f9b31dbccfeaf02bdcdc3da19345c8a998913f3ab556bc46e21ffe3d4c060b68ccd5f01976cf8874ca003ad574380ef337dcdeb742e525d1873f5
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
2
  --require spec_helper
3
+ --format documentation
data/README.md CHANGED
@@ -1,10 +1,64 @@
1
1
  [![Build Status](https://travis-ci.org/pikesley/red5.svg)](https://travis-ci.org/pikesley/red5)
2
2
  [![Dependency Status](http://img.shields.io/gemnasium/pikesley/red5.svg)](https://gemnasium.com/pikesley/red5)
3
+ [![Coverage Status](http://img.shields.io/coveralls/pikesley/red5.svg)](https://coveralls.io/r/pikesley/red5)
3
4
  [![Code Climate](http://img.shields.io/codeclimate/github/pikesley/red5.svg)](https://codeclimate.com/github/pikesley/red5)
4
5
  [![Gem Version](http://img.shields.io/gem/v/red5.svg)](https://rubygems.org/gems/red5)
5
6
  [![License](http://img.shields.io/:license-mit-blue.svg)](http://pikesley.mit-license.org)
6
- [![Badges](http://img.shields.io/:badges-6/6-ff6799.svg)](https://github.com/badges/badgerbadgerbadger)
7
+ [![Badges](http://img.shields.io/:badges-7/7-ff6799.svg)](https://github.com/badges/badgerbadgerbadger)
7
8
 
8
9
  # Red5
9
10
 
10
11
  Ruby bindings for the [Star Wars API](http://swapi.co)
12
+
13
+ git clone https://github.com/pikesley/red5
14
+ cd red5
15
+ bundle
16
+ rake
17
+
18
+ or just
19
+
20
+ gem install red5
21
+
22
+ ##API
23
+
24
+ I've followed the [Star Wars API](http://swapi.co/documentation#intro) as closely as I can. It's probably best illustrated with some examples from the [specs](https://github.com/pikesley/red5/tree/master/spec):
25
+
26
+ ###Get a thing
27
+
28
+ it 'finds a particular film', :vcr do
29
+ film = Films.find 1
30
+ expect(film['title']).to eq 'A New Hope'
31
+ expect(film['episode_id']).to eq 4
32
+ end
33
+
34
+ ###Fetch the thing linked to by an attribute
35
+
36
+ it 'fetches the species for a person', :vcr do
37
+ person = People.find 15 # Greedo
38
+ expect(person.fetch_species[0]['name']).to eq 'Rodian'
39
+ end
40
+
41
+ ##Exceptions
42
+
43
+ ###Raise an exception on a 404
44
+
45
+ it 'raises an exception on a non-existent thing', :vcr do
46
+ expect{ Starships.find 999 }.to raise_exception do |exception|
47
+ expect(exception).to be_a Red5Exception
48
+ expect(exception.status).to eq 'Starship #999 does not exist'
49
+ end
50
+ end
51
+
52
+ ###Raise an exception on a bogus lookup
53
+
54
+ it 'raises an exception on a bogus lookup', :vcr do
55
+ person = People.find 19
56
+ expect{ person['favourite_food'] }. to raise_exception do |exception|
57
+ expect(exception).to be_a Red5Exception
58
+ expect(exception.status).to eq 'No such attribute favourite_food'
59
+ end
60
+ end
61
+
62
+ ##Further work
63
+
64
+ This seems to cover the basics, what else should it do?
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
+ require 'coveralls/rake/task'
3
4
 
4
5
  RSpec::Core::RakeTask.new
6
+ Coveralls::RakeTask.new
5
7
 
6
- task :default => [:spec]
8
+ task :default => [:spec, 'coveralls:push']
@@ -0,0 +1,19 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+ require 'active_support/core_ext/string'
4
+
5
+ require 'red_5/version'
6
+
7
+ require 'red_5/helpers'
8
+
9
+ require 'red_5/root'
10
+ require 'red_5/models/entities'
11
+
12
+ require 'red_5/models/films'
13
+ require 'red_5/models/people'
14
+ require 'red_5/models/planets'
15
+ require 'red_5/models/species'
16
+ require 'red_5/models/starships'
17
+ require 'red_5/models/vehicles'
18
+
19
+ require 'red_5/exceptions/red_5_exception'
@@ -0,0 +1,9 @@
1
+ module Red5
2
+ class Red5Exception < Exception
3
+ attr_reader :status
4
+
5
+ def initialize status
6
+ @status = status
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Red5
2
+ def self.fetch_results url
3
+ r = RestClient::Resource.new url
4
+ b = r.get.body
5
+ j = JSON.parse b
6
+ j
7
+ end
8
+ end
@@ -0,0 +1,54 @@
1
+ module Red5
2
+ class Entities
3
+ def self.find id
4
+ basename = self.name.split('::').last
5
+ slug = basename.downcase
6
+ resource = RestClient::Resource.new "http://swapi.co/api/#{slug}/#{id}"
7
+
8
+ begin
9
+ self.new JSON.parse resource.get.body
10
+ rescue RestClient::ResourceNotFound
11
+ id = "##{id}" if id.is_a? Numeric
12
+ raise Red5Exception.new "#{basename.singularize} #{id} does not exist"
13
+ end
14
+ end
15
+
16
+ def self.first
17
+ self.find 1
18
+ end
19
+
20
+ def initialize data
21
+ @data = data
22
+ @data['id'] = @data['url'].split('/').last.to_i
23
+ end
24
+
25
+ def [] key
26
+ @data[key] || raise(Red5Exception.new "No such attribute #{key}")
27
+ end
28
+
29
+ def method_missing method_name, *args
30
+ mname = method_name.to_s
31
+ parts = mname.split('_')
32
+
33
+ case parts[0]
34
+ when 'fetch'
35
+ key = parts[1]
36
+ url = self[key]
37
+
38
+ unless url.is_a? Array
39
+ expects_single_result = true
40
+ url = [url]
41
+ end
42
+
43
+ results = []
44
+ url.each do |u|
45
+ results.push Red5.fetch_results u
46
+ end
47
+
48
+ return results.first if expects_single_result
49
+ results
50
+
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,4 +1,4 @@
1
- module Red_5
1
+ module Red5
2
2
  class Films < Entities
3
3
  end
4
4
  end
@@ -1,4 +1,4 @@
1
- module Red_5
1
+ module Red5
2
2
  class People < Entities
3
3
  end
4
4
  end
@@ -1,4 +1,4 @@
1
- module Red_5
1
+ module Red5
2
2
  class Planets < Entities
3
3
  end
4
4
  end
@@ -1,4 +1,4 @@
1
- module Red_5
1
+ module Red5
2
2
  class Species < Entities
3
3
  end
4
4
  end
@@ -1,4 +1,4 @@
1
- module Red_5
1
+ module Red5
2
2
  class Starships < Entities
3
3
  end
4
4
  end
@@ -1,4 +1,4 @@
1
- module Red_5
1
+ module Red5
2
2
  class Vehicles < Entities
3
3
  end
4
4
  end
@@ -1,4 +1,4 @@
1
- module Red_5
1
+ module Red5
2
2
  class Root
3
3
  def self.all
4
4
  resource = RestClient::Resource.new "http://swapi.co/api/"
@@ -0,0 +1,3 @@
1
+ module Red5
2
+ VERSION = "0.0.3"
3
+ end
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'red5/version'
4
+ require 'red_5/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'red5'
@@ -19,12 +19,15 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'rest_client', '~> 1.8'
22
+ spec.add_dependency 'activesupport', '~> 4.2'
22
23
 
23
24
  spec.add_development_dependency 'bundler', '~> 1.7'
24
25
  spec.add_development_dependency 'rake', '~> 10.0'
25
26
  spec.add_development_dependency 'rspec', '~> 3.1'
26
27
  spec.add_development_dependency 'guard', '~> 2.10'
27
28
  spec.add_development_dependency 'guard-rspec', '~> 4.5'
29
+ spec.add_development_dependency 'terminal-notifier-guard', '~> 1.6'
28
30
  spec.add_development_dependency 'vcr', '~> 2.9'
29
31
  spec.add_development_dependency 'webmock', '~> 1.18'
32
+ spec.add_development_dependency 'coveralls', '~> 0.7'
30
33
  end
@@ -0,0 +1,96 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://swapi.co/api/starships/lolwut
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - "*/*; q=0.5, application/xml"
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 301
19
+ message: MOVED PERMANENTLY
20
+ headers:
21
+ Date:
22
+ - Tue, 23 Dec 2014 14:22:11 GMT
23
+ Content-Type:
24
+ - text/html; charset=utf-8
25
+ Transfer-Encoding:
26
+ - chunked
27
+ Connection:
28
+ - keep-alive
29
+ Set-Cookie:
30
+ - __cfduid=df0a1ed34ef460f4111eea44ea9cff8401419344531; expires=Wed, 23-Dec-15
31
+ 14:22:11 GMT; path=/; domain=.swapi.co; HttpOnly
32
+ Location:
33
+ - http://swapi.co/api/starships/lolwut/
34
+ X-Frame-Options:
35
+ - SAMEORIGIN
36
+ Via:
37
+ - 1.1 vegur
38
+ Server:
39
+ - cloudflare-nginx
40
+ Cf-Ray:
41
+ - 19d535b8a00a0a78-LHR
42
+ body:
43
+ encoding: UTF-8
44
+ string: ''
45
+ http_version:
46
+ recorded_at: Tue, 23 Dec 2014 14:22:11 GMT
47
+ - request:
48
+ method: get
49
+ uri: http://swapi.co/api/starships/lolwut/
50
+ body:
51
+ encoding: US-ASCII
52
+ string: ''
53
+ headers:
54
+ Accept:
55
+ - "*/*; q=0.5, application/xml"
56
+ Accept-Encoding:
57
+ - gzip, deflate
58
+ Cookie:
59
+ - __cfduid=df0a1ed34ef460f4111eea44ea9cff8401419344531; domain=.swapi.co
60
+ User-Agent:
61
+ - Ruby
62
+ response:
63
+ status:
64
+ code: 404
65
+ message: NOT FOUND
66
+ headers:
67
+ Date:
68
+ - Tue, 23 Dec 2014 14:22:11 GMT
69
+ Content-Type:
70
+ - application/json
71
+ Transfer-Encoding:
72
+ - chunked
73
+ Connection:
74
+ - keep-alive
75
+ X-Frame-Options:
76
+ - SAMEORIGIN
77
+ Allow:
78
+ - GET, HEAD, OPTIONS
79
+ Vary:
80
+ - Accept, Cookie
81
+ Via:
82
+ - 1.1 vegur
83
+ Server:
84
+ - cloudflare-nginx
85
+ Cf-Ray:
86
+ - 19d535b95fb30a78-LHR
87
+ Content-Encoding:
88
+ - gzip
89
+ body:
90
+ encoding: ASCII-8BIT
91
+ string: !binary |-
92
+ H4sIAAAAAAAAA6pWSkktSczMUbJS8ssvUUjLL81LUaoFAAAA//8DAN21v3kW
93
+ AAAA
94
+ http_version:
95
+ recorded_at: Tue, 23 Dec 2014 14:22:11 GMT
96
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,96 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://swapi.co/api/starships/999
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - "*/*; q=0.5, application/xml"
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 301
19
+ message: MOVED PERMANENTLY
20
+ headers:
21
+ Date:
22
+ - Tue, 23 Dec 2014 14:22:10 GMT
23
+ Content-Type:
24
+ - text/html; charset=utf-8
25
+ Transfer-Encoding:
26
+ - chunked
27
+ Connection:
28
+ - keep-alive
29
+ Set-Cookie:
30
+ - __cfduid=d746f256c535edd7334a2751938b9a4a71419344530; expires=Wed, 23-Dec-15
31
+ 14:22:10 GMT; path=/; domain=.swapi.co; HttpOnly
32
+ Location:
33
+ - http://swapi.co/api/starships/999/
34
+ X-Frame-Options:
35
+ - SAMEORIGIN
36
+ Via:
37
+ - 1.1 vegur
38
+ Server:
39
+ - cloudflare-nginx
40
+ Cf-Ray:
41
+ - 19d535b39fe90a78-LHR
42
+ body:
43
+ encoding: UTF-8
44
+ string: ''
45
+ http_version:
46
+ recorded_at: Tue, 23 Dec 2014 14:22:10 GMT
47
+ - request:
48
+ method: get
49
+ uri: http://swapi.co/api/starships/999/
50
+ body:
51
+ encoding: US-ASCII
52
+ string: ''
53
+ headers:
54
+ Accept:
55
+ - "*/*; q=0.5, application/xml"
56
+ Accept-Encoding:
57
+ - gzip, deflate
58
+ Cookie:
59
+ - __cfduid=d746f256c535edd7334a2751938b9a4a71419344530; domain=.swapi.co
60
+ User-Agent:
61
+ - Ruby
62
+ response:
63
+ status:
64
+ code: 404
65
+ message: NOT FOUND
66
+ headers:
67
+ Date:
68
+ - Tue, 23 Dec 2014 14:22:11 GMT
69
+ Content-Type:
70
+ - application/json
71
+ Transfer-Encoding:
72
+ - chunked
73
+ Connection:
74
+ - keep-alive
75
+ X-Frame-Options:
76
+ - SAMEORIGIN
77
+ Allow:
78
+ - GET, HEAD, OPTIONS
79
+ Vary:
80
+ - Accept, Cookie
81
+ Via:
82
+ - 1.1 vegur
83
+ Server:
84
+ - cloudflare-nginx
85
+ Cf-Ray:
86
+ - 19d535b58f9b0a78-LHR
87
+ Content-Encoding:
88
+ - gzip
89
+ body:
90
+ encoding: ASCII-8BIT
91
+ string: !binary |-
92
+ H4sIAAAAAAAAA6pWSkktSczMUbJS8ssvUUjLL81LUaoFAAAA//8DAN21v3kW
93
+ AAAA
94
+ http_version:
95
+ recorded_at: Tue, 23 Dec 2014 14:22:11 GMT
96
+ recorded_with: VCR 2.9.3