comic_vine 0.0.4 → 0.0.5

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
@@ -18,13 +18,11 @@ Or install it yourself as:
18
18
 
19
19
  You will also need to have a ComicVine API key.
20
20
 
21
- After installing gem run the generator
22
-
23
- rails g comic_vine:install
21
+ ## Usage
24
22
 
25
- This will install a keyfile at config/cv_key.yml. Update this file with your own API key.
23
+ Requires that API key be set manually. This is a breaking change from 0.0.4.
26
24
 
27
- ## Usage
25
+ ComicVine::API.key = xxxxxx
28
26
 
29
27
  works on a subset of the api actions
30
28
 
data/changelog CHANGED
@@ -1,3 +1,5 @@
1
+ 0.0.5 - Behind the scenes cleanup. Change gem to work as a pure ruby implementation. API.key is now manually set as opposed to reading from a config file. Removed generator and railties.
2
+
1
3
  0.0.4 - Add CVObjectList to carry count vars from result. Add simple pagination. Include enumerable in the list classes
2
4
 
3
5
  0.0.3 - Add simple associations, remove initializer and do that work in the railtie class, error check on CV response, allow options
@@ -0,0 +1,77 @@
1
+ module ComicVine
2
+ class CVList
3
+ include Enumerable
4
+
5
+ attr_reader :total_count
6
+ attr_reader :offset
7
+ attr_reader :limit
8
+ attr_reader :cvos
9
+
10
+ def initialize(resp)
11
+ @total_count = resp['number_of_total_results']
12
+ @offset = resp['offset']
13
+ @limit = resp['limit']
14
+ end
15
+
16
+ def each
17
+ @cvos.each { |c| yield c }
18
+ end
19
+
20
+ def last
21
+ @cvos.last
22
+ end
23
+
24
+ protected
25
+ def update_ivals(new_cvol)
26
+ @total_count = new_cvol.total_count
27
+ @offset = new_cvol.offset
28
+ @limit = new_cvol.limit
29
+
30
+ @cvos = new_cvol.cvos
31
+ end
32
+ end
33
+
34
+ class CVObjectList < CVList
35
+ attr_reader :resource
36
+
37
+ def initialize(resp, resc)
38
+ super(resp)
39
+
40
+ @resource = resc
41
+ @cvos = resp['results'].map{ |r| ComicVine::CVObject.new(r)}
42
+ end
43
+
44
+ def next_page
45
+ return nil if (@offset + count) == @total_count
46
+ update_ivals(ComicVine::API.send(@resource, {:limit => @limit, :offset => (@offset + count)}))
47
+ end
48
+
49
+ def prev_page
50
+ return nil if @offset == 0
51
+ update_ivals(ComicVine::API.send(@resource, {:limit => @limit, :offset => (@offset - count)}))
52
+ end
53
+ end
54
+
55
+ class CVSearchList < CVList
56
+ attr_reader :resource
57
+ attr_reader :query
58
+
59
+ def initialize(resp, resc, query)
60
+ super(resp)
61
+
62
+ @resource = resc
63
+ @query = query
64
+ @cvos = resp['results'].map{ |r| ComicVine::CVSearchObject.new(r)}
65
+ end
66
+
67
+ def next_page
68
+ return nil if (@offset + count) == @total_count
69
+ update_ivals(ComicVine::API.search(@resource, @query, {:limit => @limit, :offset => (@offset + count)}))
70
+ end
71
+
72
+ def prev_page
73
+ return nil if @offset == 0
74
+ update_ivals(ComicVine::API.search(@resource, @query, {:limit => @limit, :offset => (@offset - count)}))
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,38 @@
1
+ module ComicVine
2
+ class CVObject
3
+ def initialize(args)
4
+ args.each do |k,v|
5
+ self.class.class_eval { attr_accessor k }
6
+ instance_variable_set "@#{k}", v
7
+ end
8
+ end
9
+
10
+ def method_missing(method_sym, *arguments, &block)
11
+ if method_sym.to_s =~ /^get_(.*)$/
12
+ key = method_sym.to_s.sub "get_", ""
13
+ if instance_variable_defined?("@#{key}")
14
+ if type = ComicVine::API.find_list(key)
15
+ res = []
16
+ send(key).each do |i|
17
+ res << ComicVine::API.send(type['detail_resource_name'], i['id'])
18
+ end
19
+ return res
20
+ end
21
+ if ComicVine::API.find_detail(key)
22
+ return ComicVine::API.send(key, send(key)['id'])
23
+ end
24
+ else
25
+ super
26
+ end
27
+ elsif
28
+ super
29
+ end
30
+ end
31
+ end
32
+
33
+ class CVSearchObject < CVObject
34
+ def fetch
35
+ ComicVine::API.send(@resource_type, @id)
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module ComicVine
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/comic_vine.rb CHANGED
@@ -1,195 +1,94 @@
1
1
  require "comic_vine/version"
2
2
  require "net/http"
3
+ require "json"
3
4
 
4
5
  module ComicVine
5
- class Railtie < Rails::Railtie
6
- config.after_initialize do
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
13
- end
14
- end
15
6
 
16
7
  class CVError < StandardError
17
8
  end
18
9
 
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
10
+ class API
11
+ @@key = nil
36
12
 
37
- def last
38
- @cvos.last
39
- end
13
+ @@types = nil
14
+ @@last_type_check = nil
40
15
 
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
16
+ @@API_BASE_URL = "http://api.comicvine.com/"
46
17
 
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)}
18
+ def self.search res, query, opts={}
19
+ resp = hit_api(build_url("search", opts)+"&resources=#{res}&query=#{query}")
20
+ ComicVine::CVSearchList.new(resp, res, query)
59
21
  end
60
22
 
61
- def next_page
62
- return nil if (@offset + count) == @total_count
63
- update_ivals(ComicVine::API.send(@resource, {:limit => @limit, :offset => (@offset + count)}))
23
+ def self.find_list type
24
+ types.find { |t| t['list_resource_name'] == type }
64
25
  end
65
26
 
66
- def prev_page
67
- return nil if @offset == 0
68
- update_ivals(ComicVine::API.send(@resource, {:limit => @limit, :offset => (@offset - count)}))
27
+ def self.find_detail type
28
+ types.find { |t| t['detail_resource_name'] == type }
69
29
  end
70
- end
71
-
72
- class CVSearchList < CVList
73
- attr_reader :resource
74
- attr_reader :query
75
30
 
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)}
31
+ def self.method_missing(method_sym, *arguments, &block)
32
+ if find_list(method_sym.to_s)
33
+ get_list method_sym.to_s, arguments.first
34
+ elsif find_detail(method_sym.to_s)
35
+ get_details method_sym.to_s, *arguments
36
+ elsif
37
+ super
38
+ end
82
39
  end
83
40
 
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)}))
41
+ def self.key
42
+ @@key
87
43
  end
88
44
 
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
-
95
- class CVObject
96
- def initialize(args)
97
- args.each do |k,v|
98
- class_eval { attr_accessor k }
99
- instance_variable_set "@#{k}", v
100
- end
45
+ def self.key= key
46
+ @@key = key
101
47
  end
102
48
 
103
- def method_missing(method_sym, *arguments, &block)
104
- if method_sym.to_s =~ /^get_(.*)$/
105
- key = method_sym.to_s.sub "get_", ""
106
- if instance_variable_defined?("@#{key}")
107
- if ComicVine::API::LIST_ACTIONS.include?(key.to_sym)
108
- res = []
109
- send(key).each do |i|
110
- res << ComicVine::API.send(key.singularize, i['id'])
111
- end
112
- return res
113
- end
114
- if ComicVine::API::LIST_ACTIONS.include?(key.pluralize.to_sym)
115
- return ComicVine::API.send(key, send(key)['id'])
116
- end
117
- else
118
- super
119
- end
120
- elsif
121
- super
49
+ def self.types
50
+ if @@types.nil? || (@@last_type_check + (4 *60 *60)) > Time.now
51
+ @@last_type_check = Time.now
52
+ @@types = hit_api(build_url('types'))['results']
122
53
  end
54
+ @@types
123
55
  end
124
- end
125
56
 
126
- class CVSearchObject < CVObject
127
- def fetch
128
- ComicVine::API.send(@resource_type, @id)
57
+ def self.get_list list_type, opts=nil
58
+ resp = hit_api(build_url(list_type, opts))
59
+ ComicVine::CVObjectList.new(resp, list_type)
129
60
  end
130
- end
131
-
132
- class API
133
- cattr_accessor :key
134
-
135
- API_BASE_URL = "http://api.comicvine.com/"
136
61
 
137
- LIST_ACTIONS = [ :characters,
138
- :chats,
139
- :concepts,
140
- :issues,
141
- :locations,
142
- :movies,
143
- :objects,
144
- :origins,
145
- :persons,
146
- :powers,
147
- :promos,
148
- :publishers,
149
- :story_arcs,
150
- :teams,
151
- :videos,
152
- :video_types,
153
- :volumes ].freeze
154
-
155
- def self.search res, query, opts={}
156
- resp = hit_api(build_url("search", opts)+"&resources=#{res}&query=#{query}")
157
- ComicVine::CVSearchList.new(resp, res, query)
62
+ def self.get_details item_type, id, opts=nil
63
+ resp = hit_api(build_url("#{item_type}/#{id}", opts))
64
+ ComicVine::CVObject.new(resp['results'])
158
65
  end
159
66
 
160
- def self.method_missing(method_sym, *arguments, &block)
161
- if LIST_ACTIONS.include?(method_sym)
162
- self.get_list method_sym.to_s, arguments.first
163
- elsif LIST_ACTIONS.include?(method_sym.to_s.pluralize.to_sym)
164
- self.get_item method_sym, *arguments
165
- elsif
166
- super
167
- end
67
+ def self.hit_api url
68
+ uri = URI.parse(url)
69
+ resp = Net::HTTP.get(uri)
70
+ presp = JSON.parse(resp)
71
+ raise CVError, presp['error'] unless presp['status_code'] == 1
72
+ presp
168
73
  end
169
74
 
170
- private
171
- def self.get_list list_type, opts={}
172
- resp = hit_api(build_url(list_type, opts))
173
- ComicVine::CVObjectList.new(resp, list_type)
174
- end
175
-
176
- def self.get_item item_type, id, opts={}
177
- resp = hit_api(build_url("#{item_type}/#{id}", opts))
178
- ComicVine::CVObject.new(resp['results'])
179
- end
180
-
181
- def self.hit_api url
182
- url = URI.parse(url)
183
- resp = Net::HTTP.get(url)
184
- presp = JSON.parse(resp)
185
- raise CVError, presp['error'] unless presp['status_code'] == 1
186
- presp
187
- end
188
-
189
- def self.build_url action, opts={}
190
- query = ''
191
- query = "&#{opts.to_query}" if !opts.nil?
192
- API_BASE_URL+action+"/?format=json&api_key=#{@@key}#{query}"
75
+ def self.build_url action, opts=nil
76
+ query = ''
77
+ if !opts.nil? && !opts.empty?
78
+ opts.each do |k,v|
79
+ query << "&#{k.to_s}=#{v}"
80
+ end
193
81
  end
82
+ @@API_BASE_URL+action+"/?format=json&api_key=#{@@key}#{query}"
83
+ end
84
+
85
+ private_class_method :types
86
+ private_class_method :get_list
87
+ private_class_method :get_details
88
+ private_class_method :hit_api
89
+ private_class_method :build_url
194
90
  end
195
91
  end
92
+
93
+ require 'comic_vine/cv_object'
94
+ require 'comic_vine/cv_list'
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.4
4
+ version: 0.0.5
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-11 00:00:00.000000000Z
12
+ date: 2012-04-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70341716356020 !ruby/object:Gem::Requirement
16
+ requirement: &70119669944600 !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: *70341716356020
24
+ version_requirements: *70119669944600
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
@@ -37,9 +37,9 @@ files:
37
37
  - changelog
38
38
  - comic_vine.gemspec
39
39
  - lib/comic_vine.rb
40
+ - lib/comic_vine/cv_list.rb
41
+ - lib/comic_vine/cv_object.rb
40
42
  - lib/comic_vine/version.rb
41
- - lib/generators/comic_vine/install/install_generator.rb
42
- - lib/generators/comic_vine/install/templates/cv_key.yml
43
43
  homepage: https://github.com/Jakanapes/ComicVine
44
44
  licenses: []
45
45
  post_install_message:
@@ -1,17 +0,0 @@
1
- module ComicVine
2
- module Generators
3
- class InstallGenerator < ::Rails::Generators::Base
4
- source_root File.expand_path('../templates', __FILE__)
5
-
6
- desc "This generator installs the blank keyfile for ComicVine and copies the initializer"
7
- def copy_the_keyfile
8
- copy_file "cv_key.yml", "config/cv_key.yml"
9
- end
10
-
11
- #def copy_the_init
12
- # copy_file "initializer.rb", "config/initializers/comic_vine.rb"
13
- #end
14
-
15
- end
16
- end
17
- end
@@ -1,2 +0,0 @@
1
- # Add your ComicVine API key here
2
- cvkey: XXXXXX