comic_vine 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.
data/README.md CHANGED
@@ -24,8 +24,6 @@ rails g comic_vine:install
24
24
 
25
25
  This will install a keyfile at config/cv_key.yml. Update this file with your own API key.
26
26
 
27
- The generator also installs an initializer to capture the api key in a class variable for ComicVine::API
28
-
29
27
  ## Usage
30
28
 
31
29
  works on a subset of the api actions
@@ -77,12 +75,19 @@ Search takes a resource type and a query string and returns an array of CVObject
77
75
 
78
76
  ComicVine::API.search 'volume', 'batman'
79
77
 
80
- ## ToDos
81
- Limits and Offsets
78
+ Pass in options as a hash
79
+
80
+ ComicVine::API.characters :limit=>5, :offset=>10
82
81
 
83
- Filtering
82
+ 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]
84
83
 
85
- Error checking
84
+ volume = ComicVine::API.volume 766
85
+ issues = volume.get_issues
86
+
87
+ Error responses from the API will raise a CVError with the error message
88
+
89
+ ## ToDos
90
+ More Error checking
86
91
 
87
92
  Documentation
88
93
 
data/changelog ADDED
@@ -0,0 +1,5 @@
1
+ 0.0.3 - Add simple associations, remove initializer and do that work in the railtie class, error check on CV response, allow options
2
+
3
+ 0.0.2 - Create CVObjects from resource returns
4
+
5
+ 0.0.1 - First check-in
@@ -1,3 +1,3 @@
1
1
  module ComicVine
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/comic_vine.rb CHANGED
@@ -2,6 +2,16 @@ require "comic_vine/version"
2
2
  require "net/http"
3
3
 
4
4
  module ComicVine
5
+ class Railtie < Rails::Railtie
6
+ config.after_initialize do
7
+ keyfile = YAML::load(File.open(Rails.root.join('config', 'cv_key.yml')))
8
+ ComicVine::API.key = keyfile['cvkey']
9
+ end
10
+ end
11
+
12
+ class CVError < StandardError
13
+ end
14
+
5
15
  class CVObject
6
16
  def initialize(args)
7
17
  args.each do |k,v|
@@ -9,6 +19,28 @@ module ComicVine
9
19
  instance_variable_set "@#{k}", v
10
20
  end
11
21
  end
22
+
23
+ def method_missing(method_sym, *arguments, &block)
24
+ if method_sym.to_s =~ /^get_(.*)$/
25
+ key = method_sym.to_s.sub "get_", ""
26
+ if instance_variable_defined?("@#{key}")
27
+ if ComicVine::API::LIST_ACTIONS.include?(key.to_sym)
28
+ res = []
29
+ send(key).each do |i|
30
+ res << ComicVine::API.send(key.singularize, i['id'])
31
+ end
32
+ return res
33
+ end
34
+ if ComicVine::API::LIST_ACTIONS.include?(key.pluralize.to_sym)
35
+ return ComicVine::API.send(key, send(key)['id'])
36
+ end
37
+ else
38
+ super
39
+ end
40
+ elsif
41
+ super
42
+ end
43
+ end
12
44
  end
13
45
 
14
46
  class API
@@ -34,33 +66,34 @@ module ComicVine
34
66
  :video_types,
35
67
  :volumes ].freeze
36
68
 
37
- def self.search res, query
38
- hit_api(build_url("search")+"&resources=#{res}&query=#{query}")
69
+ def self.search res, query, opts={}
70
+ hit_api(build_url("search", opts)+"&resources=#{res}&query=#{query}")
39
71
  end
40
72
 
41
73
  def self.method_missing(method_sym, *arguments, &block)
42
74
  if LIST_ACTIONS.include?(method_sym)
43
- self.get_list method_sym.to_s
75
+ self.get_list method_sym.to_s, arguments.first
44
76
  elsif LIST_ACTIONS.include?(method_sym.to_s.pluralize.to_sym)
45
- self.get_item method_sym, arguments.first
77
+ self.get_item method_sym, *arguments
46
78
  elsif
47
79
  super
48
80
  end
49
81
  end
50
82
 
51
83
  private
52
- def self.get_list list_type
53
- hit_api(build_url(list_type))
84
+ def self.get_list list_type, opts={}
85
+ hit_api(build_url(list_type, opts))
54
86
  end
55
87
 
56
- def self.get_item item_type, id
57
- hit_api(build_url("#{item_type}/#{id}"))
88
+ def self.get_item item_type, id, opts={}
89
+ hit_api(build_url("#{item_type}/#{id}", opts))
58
90
  end
59
91
 
60
92
  def self.hit_api url
61
93
  url = URI.parse(url)
62
94
  resp = Net::HTTP.get(url)
63
95
  presp = JSON.parse(resp)
96
+ raise CVError, presp['error'] unless presp['status_code'] == 1
64
97
  if presp['results'].kind_of?(Array)
65
98
  presp['results'].map{ |r| ComicVine::CVObject.new(r)}
66
99
  else
@@ -68,8 +101,10 @@ module ComicVine
68
101
  end
69
102
  end
70
103
 
71
- def self.build_url action
72
- API_BASE_URL+action+"/?api_key=#{@@key}&format=json"
104
+ def self.build_url action, opts={}
105
+ query = ''
106
+ query = "&#{opts.to_query}" if !opts.nil?
107
+ API_BASE_URL+action+"/?format=json&api_key=#{@@key}#{query}"
73
108
  end
74
109
  end
75
110
  end
@@ -8,9 +8,9 @@ module ComicVine
8
8
  copy_file "cv_key.yml", "config/cv_key.yml"
9
9
  end
10
10
 
11
- def copy_the_init
12
- copy_file "initializer.rb", "config/initializers/comic_vine.rb"
13
- end
11
+ #def copy_the_init
12
+ # copy_file "initializer.rb", "config/initializers/comic_vine.rb"
13
+ #end
14
14
 
15
15
  end
16
16
  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.2
4
+ version: 0.0.3
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-09 00:00:00.000000000Z
12
+ date: 2012-04-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70362725400000 !ruby/object:Gem::Requirement
16
+ requirement: &70251308181480 !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: *70362725400000
24
+ version_requirements: *70251308181480
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
@@ -34,12 +34,12 @@ files:
34
34
  - LICENSE
35
35
  - README.md
36
36
  - Rakefile
37
+ - changelog
37
38
  - comic_vine.gemspec
38
39
  - lib/comic_vine.rb
39
40
  - lib/comic_vine/version.rb
40
41
  - lib/generators/comic_vine/install/install_generator.rb
41
42
  - lib/generators/comic_vine/install/templates/cv_key.yml
42
- - lib/generators/comic_vine/install/templates/initializer.rb
43
43
  homepage: https://github.com/Jakanapes/ComicVine
44
44
  licenses: []
45
45
  post_install_message:
@@ -1,5 +0,0 @@
1
-
2
-
3
- keyfile = YAML::load(File.open(Rails.root.join('config', 'cv_key.yml')))
4
-
5
- ComicVine::API.key = keyfile['cvkey']