Class: ComicVine::API
- Inherits:
-
Object
- Object
- ComicVine::API
- Defined in:
- lib/comicvine.rb
Overview
Class to interact and query the ComicVine API
Defined Under Namespace
Classes: ComicVineAPIError, ResourceNotSupportedError
Constant Summary collapse
- @@types =
Hash containing ComicVine resource types and corresponding id values
nil
- @@last_type_check =
Last time we pulled types from the API
nil
- @@api_key =
ComicVine API Key. Set to the environmental variable CV_API_KEY by default if present
ENV['CV_API_KEY'] || nil
- @@api_timeout =
Api response timeout in seconds
10
Class Method Summary collapse
-
.api_key ⇒ String
Returns ComicVine API Key.
-
.api_key=(key) ⇒ Object
Sets the ComicVine API Key.
-
.api_timeout ⇒ Integer
Returns ComicVine API request timeout value.
-
.api_timeout=(seconds) ⇒ Object
Sets the ComicVine API request timeout value in seconds.
-
.find_detail(resource) ⇒ Hash
Cycles through type hash to return the
resource
hash of the matching the detail_resource_name. -
.find_list(resource) ⇒ Hash
Cycles through type hash to return the
resource
hash of the matching the list_resource_name. -
.get_api_version ⇒ String
Makes request for the current api version.
-
.get_details(resource, id, **params) ⇒ ComicVine::Resource
Fetches provided
resource
with associatedid
. -
.get_details_by_url(url) ⇒ ComicVine::Resource
Will fetch the provided
url
as a Resource. -
.get_list(resource, **params) ⇒ ComicVine::ResourceList
Fetches provided.
-
.method_missing(method_sym, *arguments, &block) ⇒ Object
Checks missing method against the resource types and passes it to API.find_list or API.get_details.
- .new(key) ⇒ ComicVine::API
-
.search(resource, query, **params) ⇒ ComicVine::SearchResults
Search ComicVine with the provided information.
-
.types ⇒ Hash
Returns comicvine type information.
Class Method Details
.api_key ⇒ String
Returns ComicVine API Key. Set to the environmental variable CV_API_KEY by default if present
79 80 81 |
# File 'lib/comicvine.rb', line 79 def api_key @@api_key || ENV['CV_API_KEY'] end |
.api_key=(key) ⇒ Object
Sets the ComicVine API Key. Overrides the environmental variable CV_API_KEY
87 88 89 |
# File 'lib/comicvine.rb', line 87 def api_key=(key) @@api_key = key end |
.api_timeout ⇒ Integer
Returns ComicVine API request timeout value
95 96 97 |
# File 'lib/comicvine.rb', line 95 def api_timeout @@api_timeout end |
.api_timeout=(seconds) ⇒ Object
Sets the ComicVine API request timeout value in seconds
103 104 105 |
# File 'lib/comicvine.rb', line 103 def api_timeout=(seconds) @@api_timeout = seconds.to_i end |
.find_detail(resource) ⇒ Hash
Cycles through type hash to return the resource
hash of the matching the detail_resource_name
157 158 159 |
# File 'lib/comicvine.rb', line 157 def find_detail(resource) types.find { |t| t['detail_resource_name'] == resource.to_s } end |
.find_list(resource) ⇒ Hash
Cycles through type hash to return the resource
hash of the matching the list_resource_name
168 169 170 |
# File 'lib/comicvine.rb', line 168 def find_list(resource) types.find { |t| t['list_resource_name'] == resource.to_s } end |
.get_api_version ⇒ String
Makes request for the current api version
112 113 114 |
# File 'lib/comicvine.rb', line 112 def get_api_version _make_request(:types)['version'].to_s end |
.get_details(resource, id, **params) ⇒ ComicVine::Resource
Fetches provided resource
with associated id
194 195 196 197 198 199 200 201 |
# File 'lib/comicvine.rb', line 194 def get_details(resource, id, **params) ops_hash = { id: id } ops_hash.merge! params resp = _make_request(resource, ops_hash) ComicVine::Resource.create_resource(resp['results']) end |
.get_details_by_url(url) ⇒ ComicVine::Resource
Will fetch the provided url
as a Resource
209 210 211 212 |
# File 'lib/comicvine.rb', line 209 def get_details_by_url(url) resp = _make_url_request(url) ComicVine::Resource::create_resource(resp['results']) end |
.get_list(resource, **params) ⇒ ComicVine::ResourceList
Fetches provided
180 181 182 183 |
# File 'lib/comicvine.rb', line 180 def get_list(resource, **params) resp = _make_request(resource, params) ComicVine::ResourceList.new(resp, resource) end |
.method_missing(method_sym, *arguments, &block) ⇒ Object
Checks missing method against the resource types and passes it to find_list or get_details
216 217 218 219 220 221 222 223 |
# File 'lib/comicvine.rb', line 216 def method_missing(method_sym, *arguments, &block) if find_list(method_sym) get_list method_sym, arguments.first elsif find_detail(method_sym) get_details method_sym, *arguments elsif super end end |
.new(key) ⇒ ComicVine::API
70 71 72 73 |
# File 'lib/comicvine.rb', line 70 def new(key) @@api_key=key self end |
.search(resource, query, **params) ⇒ ComicVine::SearchResults
Search ComicVine with the provided information
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/comicvine.rb', line 126 def search(resource, query, **params) = { resources: resource.to_s, query: CGI::escape(query) } .merge! params ComicVine::SearchResults.new(_make_request(:search, ), resource, query) end |
.types ⇒ Hash
Returns comicvine type information
142 143 144 145 146 147 148 |
# File 'lib/comicvine.rb', line 142 def types if @@types.nil? || (@@last_type_check + (4 * 60 * 60)) < Time.now @@last_type_check = Time.now @@types = _make_request(:types)['results'] end @@types end |