marvelite 0.0.1
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.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +93 -0
- data/Rakefile +1 -0
- data/lib/marvelite/api/client.rb +52 -0
- data/lib/marvelite/api/response.rb +10 -0
- data/lib/marvelite/api/router.rb +25 -0
- data/lib/marvelite/version.rb +3 -0
- data/lib/marvelite.rb +15 -0
- data/spec/marvelite/api/client_spec.rb +69 -0
- data/spec/marvelite/api/router_spec.rb +5 -0
- data/spec/spec_helper.rb +2 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 79f53a7757f023b10894b8e6ed848fb12391cd92
|
4
|
+
data.tar.gz: b040915a1836fc80ab2264f1e0a7d382e4b83285
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ad69674271a377ca1e63f9cd4ccba4f8dfcbad3c3c354a2487392cf2841fc2bce905f0c90b66a0250e2014cec5fcf42908c2dda2d537b2bdcb7db58855dc2a4
|
7
|
+
data.tar.gz: 78a1f01d3a93eb825cd0977e193f063883b94d2201878dae979c9e905fae3ac5eba577b54719ddbd5705b5d26a19cd3004c2256d124518ff10f81ea6d40c5a53
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Antonio Antillon
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# Marvelite
|
2
|
+
|
3
|
+
Simple wrapper to communicate with the Marvel Comics API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'marvelite'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install marvelite
|
18
|
+
|
19
|
+
## Contents
|
20
|
+
|
21
|
+
* [Usage](#usage)
|
22
|
+
* [Methods](#methods)
|
23
|
+
* [Responses](#responses)
|
24
|
+
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Please register first in the [Marvel Comics Developer Portal](http://developer.marvel.com/) to get your API credentials (a public key and a private key, you'll need them both to configure and instantiate a client).
|
29
|
+
|
30
|
+
### Instantiate a client
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
client = Marvelite::API::Client.new(:public_key => 'abcd1234', :private_key => '5678efgh')
|
34
|
+
```
|
35
|
+
|
36
|
+
### Methods
|
37
|
+
|
38
|
+
| Method | Description |
|
39
|
+
| ------ | ----------- |
|
40
|
+
| [#characters](#characters) | Fetches a list of comic characters. |
|
41
|
+
| [#character](#character) | Fetches a single character resource. |
|
42
|
+
|
43
|
+
|
44
|
+
#### characters
|
45
|
+
Fetches a list of comic characters. Can receive optional params.
|
46
|
+
```ruby
|
47
|
+
client.characters
|
48
|
+
client.characters(:name => 'Spider-Man')
|
49
|
+
client.characters(:limit => 10, :offset => 400, :orderBy => 'name')
|
50
|
+
```
|
51
|
+
|
52
|
+
|
53
|
+
See the [Marvel Comics Interactive API Tester](http://developer.marvel.com/docs#!/public/getCreatorCollection_get_0) for a complete list of params that you can pass to the `#characters` method.
|
54
|
+
|
55
|
+
#### character
|
56
|
+
|
57
|
+
Fetches a single character resource. Accepts an integer or string value.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
client.character(1009368)
|
61
|
+
client.character('Iron Man')
|
62
|
+
```
|
63
|
+
|
64
|
+
## Responses
|
65
|
+
|
66
|
+
All requests to the API, return a `Marvelite::API::Response` object, that is nothing more than the raw API response enhanced with Hashie methods.
|
67
|
+
|
68
|
+
This way you gain several adavantages to manipulate the response, like:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
# HashWithIndifferentAccess like functionality
|
72
|
+
response[:data][:results][0][:name]
|
73
|
+
response["data"]["results"][0]["name"]
|
74
|
+
|
75
|
+
# Top level methods
|
76
|
+
response.data
|
77
|
+
response.data[:results]
|
78
|
+
```
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
## Todo
|
83
|
+
|
84
|
+
* Add more endpoints
|
85
|
+
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
1. Fork it
|
90
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
91
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
92
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
93
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Marvelite
|
2
|
+
module API
|
3
|
+
class Client
|
4
|
+
include ActiveModel::Model
|
5
|
+
include HTTParty
|
6
|
+
base_uri 'http://gateway.marvel.com'
|
7
|
+
|
8
|
+
attr_accessor :public_key, :private_key, :router, :api_version
|
9
|
+
|
10
|
+
validates_presence_of :public_key
|
11
|
+
validates_presence_of :private_key
|
12
|
+
|
13
|
+
def initialize(attrs)
|
14
|
+
super
|
15
|
+
@router = attrs.fetch(:router) { Marvelite.router }
|
16
|
+
@api_version = attrs.fetch(:api_version) { 'v1' }
|
17
|
+
end
|
18
|
+
|
19
|
+
def characters(query_params = {})
|
20
|
+
response = self.class.get("/#{api_version}/#{router.characters_path}", :query => params(query_params))
|
21
|
+
Response.new(response)
|
22
|
+
end
|
23
|
+
|
24
|
+
def character(value)
|
25
|
+
by_name = value.is_a?(String) ? true : false
|
26
|
+
response = if by_name
|
27
|
+
self.characters(:name => value)
|
28
|
+
else
|
29
|
+
self.class.get("/#{api_version}/#{router.character_path(value)}", :query => params)
|
30
|
+
end
|
31
|
+
|
32
|
+
Response.new(response)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def params(additional_params = {})
|
37
|
+
base_hash = { :apikey => public_key, :ts => ts, :hash => request_hash }
|
38
|
+
additional_params.merge(base_hash)
|
39
|
+
end
|
40
|
+
|
41
|
+
def request_hash
|
42
|
+
Digest::MD5.hexdigest("#{ts}#{private_key}#{public_key}")
|
43
|
+
end
|
44
|
+
|
45
|
+
def ts
|
46
|
+
begin
|
47
|
+
Time.now.to_i
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Marvelite
|
2
|
+
module API
|
3
|
+
class Router
|
4
|
+
def characters_path
|
5
|
+
'public/characters'
|
6
|
+
end
|
7
|
+
|
8
|
+
def character_path(id)
|
9
|
+
"public/characters/#{id}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def character_comics_path(id)
|
13
|
+
"public/characters/#{id}/comics"
|
14
|
+
end
|
15
|
+
|
16
|
+
def character_events_path(id)
|
17
|
+
"public/characters/#{id}/events"
|
18
|
+
end
|
19
|
+
|
20
|
+
def character_stories_path(id)
|
21
|
+
"public/characters/#{id}/stories"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/marvelite.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'digest'
|
3
|
+
require 'httparty'
|
4
|
+
require 'hashie'
|
5
|
+
|
6
|
+
require "marvelite/version"
|
7
|
+
require "marvelite/api/router"
|
8
|
+
require "marvelite/api/client"
|
9
|
+
require "marvelite/api/response"
|
10
|
+
|
11
|
+
module Marvelite
|
12
|
+
def self.router
|
13
|
+
Marvelite::API::Router.new
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Marvelite::API::Client do
|
4
|
+
describe 'initialization' do
|
5
|
+
it 'requires a :public_key to be set' do
|
6
|
+
client = Marvelite::API::Client.new(:private_key => '12345')
|
7
|
+
expect(client).to_not be_valid
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'requires a :private_key to be set' do
|
11
|
+
client = Marvelite::API::Client.new(:public_key => '1234')
|
12
|
+
expect(client).to_not be_valid
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'valid' do
|
16
|
+
let(:client) { Marvelite::API::Client.new(:public_key => '1234', :private_key => 'abcd') }
|
17
|
+
|
18
|
+
describe '#api_version' do
|
19
|
+
it 'receives an :api_version optional param' do
|
20
|
+
client = Marvelite::API::Client.new(:public_key => '1234', :private_key => 'abcd', :api_version => 'v2')
|
21
|
+
expect(client.api_version).to eq('v2')
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'defaults to v1' do
|
25
|
+
expect(client.api_version).to eq('v1')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'is an instance of Marvelite::Client' do
|
30
|
+
expect(client).to be_valid
|
31
|
+
expect(client).to be_a(Marvelite::API::Client)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'has an API Router instance in its config' do
|
35
|
+
expect(client.router).to_not be_nil
|
36
|
+
expect(client.router).to be_a(Marvelite::API::Router)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#ts' do
|
42
|
+
let(:client) { Marvelite::API::Client.new(:public_key => '1234', :private_key => 'abcd') }
|
43
|
+
|
44
|
+
it 'calls Time.now' do
|
45
|
+
Time.should_receive(:now)
|
46
|
+
client.ts
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'represents a timestamp converted to integer' do
|
50
|
+
expect(client.ts).to be_a(Integer)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#params' do
|
55
|
+
let(:client) { Marvelite::API::Client.new(:public_key => '1234', :private_key => 'abcd') }
|
56
|
+
|
57
|
+
it 'passes required API params to all requests' do
|
58
|
+
expect(client.params).to eq('?apikey=1234&ts=1&hash=ffd275c5130566a2916217b101f26150')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#characters' do
|
63
|
+
let(:client) { Marvelite::API::Client.new(:public_key => '1234', :private_key => 'abcd') }
|
64
|
+
|
65
|
+
it 'returns a JSON response from the characters endpoint' do
|
66
|
+
expect(client.characters).to eq('http://gateway.marvel.com/v1/public/characters?apikey=1234&ts=1&hash=ffd275c5130566a2916217b101f26150')
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: marvelite
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Antonio Antillon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hashie
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Simple wrapper around the Marvel Comics API
|
112
|
+
email:
|
113
|
+
- antillas21@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- lib/marvelite/api/client.rb
|
119
|
+
- lib/marvelite/api/response.rb
|
120
|
+
- lib/marvelite/api/router.rb
|
121
|
+
- lib/marvelite/version.rb
|
122
|
+
- lib/marvelite.rb
|
123
|
+
- spec/marvelite/api/client_spec.rb
|
124
|
+
- spec/marvelite/api/router_spec.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- Gemfile
|
127
|
+
- LICENSE.txt
|
128
|
+
- Rakefile
|
129
|
+
- README.md
|
130
|
+
homepage: ''
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
metadata: {}
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
requirements: []
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 2.0.6
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: Simple wrapper around the Marvel Comics API
|
154
|
+
test_files:
|
155
|
+
- spec/marvelite/api/client_spec.rb
|
156
|
+
- spec/marvelite/api/router_spec.rb
|
157
|
+
- spec/spec_helper.rb
|