Class: ComicVine::API

Inherits:
Object
  • Object
show all
Defined in:
lib/comicvine.rb

Overview

Class to interact and query the ComicVine API

Since:

  • 0.1.0

Defined Under Namespace

Classes: ComicVineAPIError, ResourceNotSupportedError

Constant Summary collapse

@@types =

Hash containing ComicVine resource types and corresponding id values

Since:

  • 0.1.0

nil
@@last_type_check =

Last time we pulled types from the API

Since:

  • 0.1.0

nil
@@api_key =

ComicVine API Key. Set to the environmental variable CV_API_KEY by default if present

Since:

  • 0.1.0

ENV['CV_API_KEY'] || nil
@@api_timeout =

Api response timeout in seconds

Since:

  • 0.1.0

10

Class Method Summary collapse

Class Method Details

.api_keyString

Returns ComicVine API Key. Set to the environmental variable CV_API_KEY by default if present

Returns:

  • (String)

Since:

  • 0.1.0



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

Parameters:

  • key (String)

Since:

  • 0.1.0



87
88
89
# File 'lib/comicvine.rb', line 87

def api_key=(key)
  @@api_key = key
end

.api_timeoutInteger

Returns ComicVine API request timeout value

Returns:

  • (Integer)

Since:

  • 0.1.2



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

Parameters:

  • seconds (Integer)

Since:

  • 0.1.0



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

Examples:

ComicVine::API.find_detail(:issue) #=> { "detail_resource_name": "issue", "list_resource_name": "issues", "id": 4000 }

Parameters:

  • resource (Symbol)

    The symbol of the resource to return

Returns:

  • (Hash)

Since:

  • 0.1.0



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

Examples:

ComicVine::API.find_list(:issues) #=> { "detail_resource_name": "issue", "list_resource_name": "issues", "id": 4000 }

Parameters:

  • resource (Symbol)

    The symbol of the resource to return

Returns:

  • (Hash)

Since:

  • 0.1.0



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_versionString

Makes request for the current api version

Returns:

  • (String)

Since:

  • 0.1.0



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

Examples:

ComicVine::API.get_details(:issue, '371103')

Parameters:

  • resource (Symbol)

    The symbol of the resource to fetch

  • id (String)

    The id of the resource you would like to fetch

  • params (Hash)

    optional parameters to pass to CV API

Returns:

Since:

  • 0.1.0



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

Examples:

ComicVine::API.get_details_by_url('http://comicvine.gamespot.com/api/issue/4000-371103') #=> ComicVine::Resource::Issue

Parameters:

  • url (String)

Returns:

Since:

  • 0.1.0



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

Examples:

ComicVine::API.get_list(:volumes, limit: 50)

Parameters:

  • resource (Symbol)

    The symbol of the resource to fetch (plural)

  • params (Hash)

    optional parameters to pass to CV API

Returns:

Since:

  • 0.1.0



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

Since:

  • 0.1.0



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

Returns:

Since:

  • 0.1.0



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

Examples:

ComicVine::API.search(:volume, 'Avengers ', limit: 5)

Parameters:

  • resource (Symbol)

    The symbol of the resource to query

  • query (String)

    The string to query

  • params (Hash)

    optional parameters to pass to CV API

Returns:

Since:

  • 0.1.0



126
127
128
129
130
131
132
133
134
135
# File 'lib/comicvine.rb', line 126

def search(resource, query, **params)
  options = {
      resources: resource.to_s,
      query: CGI::escape(query)
  }

  options.merge! params

  ComicVine::SearchResults.new(_make_request(:search, options), resource, query)
end

.typesHash

Returns comicvine type information

Returns:

  • (Hash)

Since:

  • 0.1.0



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