ultron 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d47f1ba2fff84cc647f4ea1111338e1ce5f20cf8
4
- data.tar.gz: 8e1eeb250086219dd878d2186b36663d9ef9ccbd
3
+ metadata.gz: 69b914d9f16335035655a79ba254c6dddb28fdea
4
+ data.tar.gz: 99eaf8d276b7036e57376c2b9539032417c73600
5
5
  SHA512:
6
- metadata.gz: d025340593662595b0838030cefc9cd3deb49887b8b4602a826e470b0cf65417d2f9b6e14021234b75d27946009b7ace33dd0f45bbd3f3e73c9d670a57ff8cae
7
- data.tar.gz: be743c8d85ad0ffcea274f806618ace605fa9bf6e25d40154406ee7f8df1a38b09bff84dc4d626438b4cc88e8e871148bd3f81f21de5ab3c9445312229f9cd09
6
+ metadata.gz: 237d10bf3c136b2dd6433eb0b0703b328ae09f9b6c9811232f5e848b6f1e9dd8b31a00f8a20ea11d1c17da6ade53503e12be7fb1dd70ef63e60d2960ffcd1986
7
+ data.tar.gz: d6b43d18a54ae12ab359055153445f1b828a0ab760963e3cf1f52b575a6383631130dd6c7c7177ff4b7cdc1c43e26a3e7053283238b4172df7ba847c37821a5e
data/README.md CHANGED
@@ -65,7 +65,7 @@ I've tried to follow the [Marvel API](http://developer.marvel.com/docs#!/public/
65
65
  comics.first.resourceURI.should == 'http://gateway.marvel.com/v1/public/comics/8268'
66
66
  end
67
67
 
68
- ### Get a random item from a search
68
+ ### Get a random thing from a search
69
69
 
70
70
  it 'should give us a random comic', :vcr do
71
71
  set = Comics.by_character 1009610
@@ -79,14 +79,14 @@ I've tried to follow the [Marvel API](http://developer.marvel.com/docs#!/public/
79
79
  set.sample.title.should == 'Dazzler (1981) #19'
80
80
  end
81
81
 
82
- There's also some Noddy exception handling:
82
+ ## Exceptions
83
83
 
84
84
  ### Catch and re-raise a 404
85
85
 
86
- it 'should throw a Not Found exception', :vcr do
86
+ it 'should throw a 404 (wrapped in a Marvel exception) on a 404', :vcr do
87
87
  begin
88
88
  comic = Comics.find 1000000 # there are not a million comics
89
- rescue NotFoundException => e
89
+ rescue MarvelException => e
90
90
  e.code.should == 404
91
91
  e.status.should == "We couldn't find that comic_issue"
92
92
  end
@@ -94,10 +94,10 @@ There's also some Noddy exception handling:
94
94
 
95
95
  ### Raise a custom exception when no results are found
96
96
 
97
- it 'should throw a No Results exception', :vcr do
97
+ it 'should throw an Ultron exception for something internal to Ultron', :vcr do
98
98
  begin
99
99
  comics = Comics.where offset: 1000000
100
- rescue NoResultsException => e
101
- e.status.should == 'That search returned no results'
100
+ rescue UltronException => e
101
+ e.status.should == 'The search returned no results'
102
102
  end
103
103
  end
@@ -1,2 +1,3 @@
1
1
  host: http://gateway.marvel.com
2
- path: /v1/public/
2
+ path: /v1/public/
3
+ api_docs: http://developer.marvel.com/docs
@@ -12,16 +12,15 @@ require 'ultron/auth'
12
12
  require 'ultron/config'
13
13
  require 'ultron/connection'
14
14
 
15
- require 'ultron/entities'
15
+ require 'ultron/models/entities'
16
+ require 'ultron/models/characters'
17
+ require 'ultron/models/comics'
18
+ require 'ultron/models/creators'
19
+ require 'ultron/models/events'
20
+ require 'ultron/models/series'
21
+ require 'ultron/models/stories'
16
22
 
17
- require 'ultron/characters'
18
- require 'ultron/comics'
19
- require 'ultron/creators'
20
- require 'ultron/events'
21
- require 'ultron/series'
22
- require 'ultron/stories'
23
-
24
- require 'ultron/exceptions/not_found_exception'
25
- require 'ultron/exceptions/no_results_exception'
23
+ require 'ultron/exceptions/marvel_exception'
24
+ require 'ultron/exceptions/ultron_exception'
26
25
 
27
26
  require 'pry'
@@ -9,10 +9,6 @@ module Ultron
9
9
  'User-agent' => 'Ultron v%s: https://rubygems.org/gems/ultron' % Ultron::VERSION
10
10
  }
11
11
 
12
- if Config.instance.config['debug']
13
- puts ">>> Hitting\n\t%s" % url
14
- end
15
-
16
12
  c.perform
17
13
  JSON.parse c.body_str
18
14
  end
@@ -1,5 +1,5 @@
1
1
  module Ultron
2
- class NotFoundException < Exception
2
+ class MarvelException < Exception
3
3
  attr_reader :code, :status
4
4
 
5
5
  def initialize params
@@ -1,5 +1,5 @@
1
1
  module Ultron
2
- class NoResultsException < Exception
2
+ class UltronException < Exception
3
3
  attr_reader :status
4
4
 
5
5
  def initialize status
@@ -1,49 +1,45 @@
1
1
  module Ultron
2
2
  class Entities
3
- attr_reader :metadata
4
-
5
3
  include Enumerable
6
4
 
7
- def self.name_for_path
8
- self.name.split('::')[-1].downcase
9
- end
10
-
11
- def self.find id
12
- path = self.name_for_path
13
- path = '%s/%s' % [path, id]
14
- url = get_url path
15
- response = Ultron::Connection.perform url
16
- case response['code'].to_i
17
- when 404
18
- raise NotFoundException.new response
19
-
20
- else
21
- set = self.new response['data'], url
22
- set.first
23
- end
24
- end
25
-
26
5
  def self.method_missing method_name, *args
27
6
  mname = method_name.to_s
28
7
  query = nil
29
8
  path = self.name_for_path #if mname == 'get'
30
9
 
31
- parts = mname.split /_and_/
32
- parts.each do |part|
10
+ mname.split(/_and_/).each do |part|
33
11
  case part
12
+ when 'find'
13
+ path = '%s/%s' % [path, args.shift]
14
+
34
15
  when /by_(.*)/
35
16
  path = self.send(:by_something, $1, args.shift)
17
+
36
18
  when 'with', 'where'
37
- query = self.send(:by_params, args[0])
19
+ query = self.send(:by_params, args.shift)
38
20
  end
39
21
  end
40
22
 
41
23
  url = get_url path, query
24
+ response = self.response url
25
+
26
+ set = self.new response['data'], url
27
+ return set.first if mname == 'find'
28
+ set
29
+ end
30
+
31
+ def self.response url
42
32
  response = Ultron::Connection.perform url
43
- unless response['data']['results'].any?
44
- raise NoResultsException.new 'That search returned no results'
33
+ case response['code'].to_s
34
+ when /^4/
35
+ raise MarvelException.new response
36
+ when 'ResourceNotFound'
37
+ raise UltronException.new 'Resource does not exist. Check %s' % Config.instance.config.api_docs
45
38
  end
46
- self.new response['data'], url
39
+
40
+ raise UltronException.new 'The search returned no results' unless response['data']['results'].any?
41
+
42
+ response
47
43
  end
48
44
 
49
45
  def self.by_something something, id
@@ -58,8 +54,14 @@ module Ultron
58
54
  "%s%s?%s%s" % [Ultron::Config.instance.root_url, path, query, Ultron.auth(ENV['PRIVATE_KEY'], ENV['PUBLIC_KEY'])]
59
55
  end
60
56
 
57
+ def self.name_for_path
58
+ self.name.split('::')[-1].downcase
59
+ end
60
+
61
61
  ###
62
62
 
63
+ attr_reader :metadata
64
+
63
65
  def initialize data, url
64
66
  @results_set = data['results']
65
67
  @metadata = OpenStruct.new data
@@ -1,3 +1,3 @@
1
1
  module Ultron
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -0,0 +1,32 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://gateway.marvel.com/v1/public/comics/1000000?apikey=<PUBLIC_KEY>&hash=a304d29c7e65217654f6ddd18daf2b3c&ts=1392317244
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - application/json
12
+ User-Agent:
13
+ - 'Ultron v0.1.3: https://rubygems.org/gems/ultron'
14
+ response:
15
+ status:
16
+ code: 404
17
+ message: Not Found
18
+ headers:
19
+ Content-Type:
20
+ - application/json
21
+ Content-Length:
22
+ - '57'
23
+ Date:
24
+ - Sat, 15 Feb 2014 20:59:00 GMT
25
+ Connection:
26
+ - keep-alive
27
+ body:
28
+ encoding: ASCII-8BIT
29
+ string: "{\"code\":404,\"status\":\"We couldn't find that comic_issue\"}"
30
+ http_version:
31
+ recorded_at: Thu, 13 Feb 2014 18:47:24 GMT
32
+ recorded_with: VCR 2.8.0
@@ -0,0 +1,32 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://gateway.marvel.com/v1/public/comics/1000000?apikey=<PUBLIC_KEY>&hash=a304d29c7e65217654f6ddd18daf2b3c&ts=1392317244
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - application/json
12
+ User-Agent:
13
+ - 'Ultron v0.1.3: https://rubygems.org/gems/ultron'
14
+ response:
15
+ status:
16
+ code: 404
17
+ message: Not Found
18
+ headers:
19
+ Content-Type:
20
+ - application/json
21
+ Content-Length:
22
+ - '57'
23
+ Date:
24
+ - Sat, 15 Feb 2014 20:34:41 GMT
25
+ Connection:
26
+ - keep-alive
27
+ body:
28
+ encoding: ASCII-8BIT
29
+ string: "{\"code\":404,\"status\":\"We couldn't find that comic_issue\"}"
30
+ http_version:
31
+ recorded_at: Thu, 13 Feb 2014 18:47:24 GMT
32
+ recorded_with: VCR 2.8.0
@@ -0,0 +1,32 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://gateway.marvel.com/v1/public/characters?Thor=Mighty&apikey=<PUBLIC_KEY>&hash=a304d29c7e65217654f6ddd18daf2b3c&ts=1392317244
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - application/json
12
+ User-Agent:
13
+ - 'Ultron v0.1.3: https://rubygems.org/gems/ultron'
14
+ response:
15
+ status:
16
+ code: 409
17
+ message: Conflict
18
+ headers:
19
+ Content-Type:
20
+ - application/json
21
+ Content-Length:
22
+ - '61'
23
+ Date:
24
+ - Sat, 15 Feb 2014 20:57:47 GMT
25
+ Connection:
26
+ - keep-alive
27
+ body:
28
+ encoding: ASCII-8BIT
29
+ string: "{\"code\":409,\"status\":\"We don't recognize the parameter Thor\"}"
30
+ http_version:
31
+ recorded_at: Thu, 13 Feb 2014 18:47:24 GMT
32
+ recorded_with: VCR 2.8.0
@@ -0,0 +1,33 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://gateway.marvel.com/v1/public/creators/186/characters?apikey=<PUBLIC_KEY>&hash=a304d29c7e65217654f6ddd18daf2b3c&ts=1392317244
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - application/json
12
+ User-Agent:
13
+ - 'Ultron v0.1.3: https://rubygems.org/gems/ultron'
14
+ response:
15
+ status:
16
+ code: 404
17
+ message: Not Found
18
+ headers:
19
+ Content-Type:
20
+ - application/json
21
+ Content-Length:
22
+ - '181'
23
+ Date:
24
+ - Sat, 15 Feb 2014 21:03:27 GMT
25
+ Connection:
26
+ - keep-alive
27
+ body:
28
+ encoding: ASCII-8BIT
29
+ string: "{\"code\":\"ResourceNotFound\",\"message\":\"/v1/public/creators/186/characters?ts=1392317244&apikey=<PUBLIC_KEY>&hash=a304d29c7e65217654f6ddd18daf2b3c
30
+ does not exist\"}"
31
+ http_version:
32
+ recorded_at: Thu, 13 Feb 2014 18:47:24 GMT
33
+ recorded_with: VCR 2.8.0
@@ -0,0 +1,34 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://gateway.marvel.com/v1/public/comics?apikey=<PUBLIC_KEY>&hash=a304d29c7e65217654f6ddd18daf2b3c&offset=1000000&ts=1392317244
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - application/json
12
+ User-Agent:
13
+ - 'Ultron v0.1.3: https://rubygems.org/gems/ultron'
14
+ response:
15
+ status:
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ Etag:
20
+ - c6221500e62a7317e759c6bd37130ee5c62c0cba
21
+ Content-Type:
22
+ - application/json
23
+ Content-Length:
24
+ - '150'
25
+ Date:
26
+ - Sat, 15 Feb 2014 20:34:41 GMT
27
+ Connection:
28
+ - keep-alive
29
+ body:
30
+ encoding: ASCII-8BIT
31
+ string: "{\"code\":200,\"status\":\"Ok\",\"etag\":\"c6221500e62a7317e759c6bd37130ee5c62c0cba\",\"data\":{\"offset\":1000000,\"limit\":20,\"total\":31917,\"count\":0,\"results\":[]}}"
32
+ http_version:
33
+ recorded_at: Thu, 13 Feb 2014 18:47:24 GMT
34
+ recorded_with: VCR 2.8.0
@@ -6,24 +6,39 @@ module Ultron
6
6
  Timecop.freeze '2014-02-13T18:47:24+00:00'
7
7
  end
8
8
 
9
- it 'should throw a Not Found exception', :vcr do
9
+ it 'should throw a 404 (wrapped in a Marvel exception) on a 404', :vcr do
10
10
  begin
11
11
  comic = Comics.find 1000000 # there are not a million comics
12
- rescue NotFoundException => e
12
+ rescue MarvelException => e
13
13
  e.code.should == 404
14
14
  e.status.should == "We couldn't find that comic_issue"
15
15
  end
16
16
  end
17
17
 
18
- it 'should throw a No Results exception', :vcr do
18
+ it 'should throw a No Idea What This Param Is exception', :vcr do
19
+ begin
20
+ character = Characters.where Thor: 'Mighty'
21
+ rescue MarvelException => e
22
+ e.code.should == 409
23
+ e.status.should == "We don't recognize the parameter Thor"
24
+ end
25
+ end
26
+
27
+ it 'should throw an Ultron exception for something internal to Ultron', :vcr do
19
28
  begin
20
29
  comics = Comics.where offset: 1000000
21
- rescue NoResultsException => e
22
- e.status.should == 'That search returned no results'
30
+ rescue UltronException => e
31
+ e.status.should == 'The search returned no results'
23
32
  end
24
33
  end
25
34
 
26
- it 'should throw a No Idea What This Param Is exception'
35
+ it 'should throw a Resource Not Found exception when we search for something nonsensical', :vcr do
36
+ begin
37
+ characters = Characters.by_creator 186 # characters by creator is a nonsense concept in the Marvel API
38
+ rescue UltronException => e
39
+ e.status.should == 'Resource does not exist. Check http://developer.marvel.com/docs'
40
+ end
41
+ end
27
42
 
28
43
  after :each do
29
44
  Timecop.return
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ultron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - pikesley
@@ -263,22 +263,21 @@ files:
263
263
  - LICENSE.md
264
264
  - README.md
265
265
  - Rakefile
266
- - config/params.yaml
267
266
  - config/ultron.yaml
268
267
  - examples/links.rb
269
268
  - lib/ultron.rb
270
269
  - lib/ultron/auth.rb
271
- - lib/ultron/characters.rb
272
- - lib/ultron/comics.rb
273
270
  - lib/ultron/config.rb
274
271
  - lib/ultron/connection.rb
275
- - lib/ultron/creators.rb
276
- - lib/ultron/entities.rb
277
- - lib/ultron/events.rb
278
- - lib/ultron/exceptions/no_results_exception.rb
279
- - lib/ultron/exceptions/not_found_exception.rb
280
- - lib/ultron/series.rb
281
- - lib/ultron/stories.rb
272
+ - lib/ultron/exceptions/marvel_exception.rb
273
+ - lib/ultron/exceptions/ultron_exception.rb
274
+ - lib/ultron/models/characters.rb
275
+ - lib/ultron/models/comics.rb
276
+ - lib/ultron/models/creators.rb
277
+ - lib/ultron/models/entities.rb
278
+ - lib/ultron/models/events.rb
279
+ - lib/ultron/models/series.rb
280
+ - lib/ultron/models/stories.rb
282
281
  - lib/ultron/version.rb
283
282
  - spec/cassettes/Ultron_Characters/find_a_character.yml
284
283
  - spec/cassettes/Ultron_Characters/should_find_a_character.yml
@@ -293,8 +292,13 @@ files:
293
292
  - spec/cassettes/Ultron_Series/should_give_a_default_set_of_series.yml
294
293
  - spec/cassettes/Ultron_Series/should_let_us_reuse_the_connection_safely.yml
295
294
  - spec/cassettes/Ultron_Series/should_let_us_search_with_parameters.yml
295
+ - spec/cassettes/exceptions/should_throw_a_404_wrapped_in_a_Marvel_exception_on_a_404.yml
296
+ - spec/cassettes/exceptions/should_throw_a_Marvel_exception_on_an_API_error.yml
297
+ - spec/cassettes/exceptions/should_throw_a_No_Idea_What_This_Param_Is_exception.yml
296
298
  - spec/cassettes/exceptions/should_throw_a_No_Results_exception.yml
297
299
  - spec/cassettes/exceptions/should_throw_a_Not_Found_exception.yml
300
+ - spec/cassettes/exceptions/should_throw_a_Resource_Not_Found_exception_when_we_search_for_something_non-sensical.yml
301
+ - spec/cassettes/exceptions/should_throw_an_Ultron_exception_for_something_internal_to_Ultron.yml
298
302
  - spec/cassettes/sample/let_us_pick_a_random_item/should_get_a_count_of_items_for_a_lookup.yml
299
303
  - spec/cassettes/sample/let_us_pick_a_random_item/should_give_us_a_random_comic.yml
300
304
  - spec/cassettes/sample/let_us_pick_a_random_item/should_give_us_a_random_comic_for_a_more_complex_search.yml
@@ -368,8 +372,13 @@ test_files:
368
372
  - spec/cassettes/Ultron_Series/should_give_a_default_set_of_series.yml
369
373
  - spec/cassettes/Ultron_Series/should_let_us_reuse_the_connection_safely.yml
370
374
  - spec/cassettes/Ultron_Series/should_let_us_search_with_parameters.yml
375
+ - spec/cassettes/exceptions/should_throw_a_404_wrapped_in_a_Marvel_exception_on_a_404.yml
376
+ - spec/cassettes/exceptions/should_throw_a_Marvel_exception_on_an_API_error.yml
377
+ - spec/cassettes/exceptions/should_throw_a_No_Idea_What_This_Param_Is_exception.yml
371
378
  - spec/cassettes/exceptions/should_throw_a_No_Results_exception.yml
372
379
  - spec/cassettes/exceptions/should_throw_a_Not_Found_exception.yml
380
+ - spec/cassettes/exceptions/should_throw_a_Resource_Not_Found_exception_when_we_search_for_something_non-sensical.yml
381
+ - spec/cassettes/exceptions/should_throw_an_Ultron_exception_for_something_internal_to_Ultron.yml
373
382
  - spec/cassettes/sample/let_us_pick_a_random_item/should_get_a_count_of_items_for_a_lookup.yml
374
383
  - spec/cassettes/sample/let_us_pick_a_random_item/should_give_us_a_random_comic.yml
375
384
  - spec/cassettes/sample/let_us_pick_a_random_item/should_give_us_a_random_comic_for_a_more_complex_search.yml
File without changes