comic_vine 0.0.3 → 0.0.4

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.
data/README.md CHANGED
@@ -63,26 +63,35 @@ video_types
63
63
  volumes
64
64
 
65
65
 
66
- Calls to plurals return arrays of CVObjects:
66
+ Calls to plurals return a CVObjectList:
67
67
 
68
- ComicVine::API.characters
68
+ chars = ComicVine::API.characters
69
+
70
+ Pagination will return nil if you are at either end of the list, otherwise it will update the object allowing for looping
71
+
72
+ chars.next_page
73
+ chars.prev_page
69
74
 
70
75
  Calls to singulars require an id and return a CVObject:
71
76
 
72
- ComicVine::API.volume 766
77
+ ComicVine::API.volume 766
73
78
 
74
- Search takes a resource type and a query string and returns an array of CVObjects
79
+ Search takes a resource type or types, separated by a comma(ex. "volume,issue"), and a query string and returns a CVSearchList (also with pagination)
75
80
 
76
- ComicVine::API.search 'volume', 'batman'
81
+ results = ComicVine::API.search 'volume', 'batman'
82
+
83
+ Call fetch to retrieve the full object
84
+
85
+ results.first.fetch
77
86
 
78
87
  Pass in options as a hash
79
88
 
80
- ComicVine::API.characters :limit=>5, :offset=>10
89
+ ComicVine::API.characters {:limit=>5, :offset=>10}
81
90
 
82
91
  There are limited associations. If the name of the association matches the resource name, then you can get the array of CVObjects by calling get_[resource]
83
92
 
84
- volume = ComicVine::API.volume 766
85
- issues = volume.get_issues
93
+ volume = ComicVine::API.volume 766
94
+ issues = volume.get_issues
86
95
 
87
96
  Error responses from the API will raise a CVError with the error message
88
97
 
data/changelog CHANGED
@@ -1,3 +1,5 @@
1
+ 0.0.4 - Add CVObjectList to carry count vars from result. Add simple pagination. Include enumerable in the list classes
2
+
1
3
  0.0.3 - Add simple associations, remove initializer and do that work in the railtie class, error check on CV response, allow options
2
4
 
3
5
  0.0.2 - Create CVObjects from resource returns
data/lib/comic_vine.rb CHANGED
@@ -4,14 +4,94 @@ require "net/http"
4
4
  module ComicVine
5
5
  class Railtie < Rails::Railtie
6
6
  config.after_initialize do
7
- keyfile = YAML::load(File.open(Rails.root.join('config', 'cv_key.yml')))
8
- ComicVine::API.key = keyfile['cvkey']
7
+ if File.exists? Rails.root.join('config', 'cv_key.yml')
8
+ keyfile = YAML::load(File.open(Rails.root.join('config', 'cv_key.yml')))
9
+ ComicVine::API.key = keyfile['cvkey']
10
+ else
11
+ ComicVine::API.key = 'no_keyfile_found'
12
+ end
9
13
  end
10
14
  end
11
15
 
12
16
  class CVError < StandardError
13
17
  end
14
18
 
19
+ class CVList
20
+ include Enumerable
21
+
22
+ attr_reader :total_count
23
+ attr_reader :offset
24
+ attr_reader :limit
25
+ attr_reader :cvos
26
+
27
+ def initialize(resp)
28
+ @total_count = resp['number_of_total_results']
29
+ @offset = resp['offset']
30
+ @limit = resp['limit']
31
+ end
32
+
33
+ def each
34
+ @cvos.each { |c| yield c }
35
+ end
36
+
37
+ def last
38
+ @cvos.last
39
+ end
40
+
41
+ protected
42
+ def update_ivals(new_cvol)
43
+ @total_count = new_cvol.total_count
44
+ @offset = new_cvol.offset
45
+ @limit = new_cvol.limit
46
+
47
+ @cvos = new_cvol.cvos
48
+ end
49
+ end
50
+
51
+ class CVObjectList < CVList
52
+ attr_reader :resource
53
+
54
+ def initialize(resp, resc)
55
+ super(resp)
56
+
57
+ @resource = resc
58
+ @cvos = resp['results'].map{ |r| ComicVine::CVObject.new(r)}
59
+ end
60
+
61
+ def next_page
62
+ return nil if (@offset + count) == @total_count
63
+ update_ivals(ComicVine::API.send(@resource, {:limit => @limit, :offset => (@offset + count)}))
64
+ end
65
+
66
+ def prev_page
67
+ return nil if @offset == 0
68
+ update_ivals(ComicVine::API.send(@resource, {:limit => @limit, :offset => (@offset - count)}))
69
+ end
70
+ end
71
+
72
+ class CVSearchList < CVList
73
+ attr_reader :resource
74
+ attr_reader :query
75
+
76
+ def initialize(resp, resc, query)
77
+ super(resp)
78
+
79
+ @resource = resc
80
+ @query = query
81
+ @cvos = resp['results'].map{ |r| ComicVine::CVSearchObject.new(r)}
82
+ end
83
+
84
+ def next_page
85
+ return nil if (@offset + count) == @total_count
86
+ update_ivals(ComicVine::API.search(@resource, @query, {:limit => @limit, :offset => (@offset + count)}))
87
+ end
88
+
89
+ def prev_page
90
+ return nil if @offset == 0
91
+ update_ivals(ComicVine::API.search(@resource, @query, {:limit => @limit, :offset => (@offset - count)}))
92
+ end
93
+ end
94
+
15
95
  class CVObject
16
96
  def initialize(args)
17
97
  args.each do |k,v|
@@ -43,6 +123,12 @@ module ComicVine
43
123
  end
44
124
  end
45
125
 
126
+ class CVSearchObject < CVObject
127
+ def fetch
128
+ ComicVine::API.send(@resource_type, @id)
129
+ end
130
+ end
131
+
46
132
  class API
47
133
  cattr_accessor :key
48
134
 
@@ -67,7 +153,8 @@ module ComicVine
67
153
  :volumes ].freeze
68
154
 
69
155
  def self.search res, query, opts={}
70
- hit_api(build_url("search", opts)+"&resources=#{res}&query=#{query}")
156
+ resp = hit_api(build_url("search", opts)+"&resources=#{res}&query=#{query}")
157
+ ComicVine::CVSearchList.new(resp, res, query)
71
158
  end
72
159
 
73
160
  def self.method_missing(method_sym, *arguments, &block)
@@ -82,11 +169,13 @@ module ComicVine
82
169
 
83
170
  private
84
171
  def self.get_list list_type, opts={}
85
- hit_api(build_url(list_type, opts))
172
+ resp = hit_api(build_url(list_type, opts))
173
+ ComicVine::CVObjectList.new(resp, list_type)
86
174
  end
87
175
 
88
176
  def self.get_item item_type, id, opts={}
89
- hit_api(build_url("#{item_type}/#{id}", opts))
177
+ resp = hit_api(build_url("#{item_type}/#{id}", opts))
178
+ ComicVine::CVObject.new(resp['results'])
90
179
  end
91
180
 
92
181
  def self.hit_api url
@@ -94,11 +183,7 @@ module ComicVine
94
183
  resp = Net::HTTP.get(url)
95
184
  presp = JSON.parse(resp)
96
185
  raise CVError, presp['error'] unless presp['status_code'] == 1
97
- if presp['results'].kind_of?(Array)
98
- presp['results'].map{ |r| ComicVine::CVObject.new(r)}
99
- else
100
- ComicVine::CVObject.new(presp['results'])
101
- end
186
+ presp
102
187
  end
103
188
 
104
189
  def self.build_url action, opts={}
@@ -1,3 +1,3 @@
1
1
  module ComicVine
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comic_vine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-10 00:00:00.000000000Z
12
+ date: 2012-04-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70251308181480 !ruby/object:Gem::Requirement
16
+ requirement: &70341716356020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70251308181480
24
+ version_requirements: *70341716356020
25
25
  description: Simple api interface to Comic Vine. Allows for searches and returning
26
26
  specific information on resources.
27
27
  email: jakanapes@gmail.com