Skip to content

Commit 2382247

Browse files
author
Juan Rossi
committed
Initial commit.
0 parents  commit 2382247

File tree

12 files changed

+413
-0
lines changed

12 files changed

+413
-0
lines changed

.gems

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rest-client -v 1.7.2
2+
cutest -v 1.2.2
3+
mocha -v 1.1.0

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.env
2+
/.gs
3+
mango-*.gem

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Mango Payments Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Mango
2+
=====
3+
4+
Ruby wrapper for Mango API
5+
6+
7+
## Description
8+
9+
API to interact with Mango
10+
https://getmango.com/
11+
12+
13+
## Installation
14+
15+
As usual, you can install it using rubygems.
16+
17+
```
18+
$ gem build mango-ruby.gemspec
19+
```
20+
21+
```
22+
$ gem install ./mango-ruby-0.0.1.gem
23+
```
24+
25+
Note: We'll be uploading the gem to [RubyGems](https://rubygems.org) in the next couple of days.
26+
27+
28+
## Usage
29+
30+
```
31+
require 'mango-ruby'
32+
33+
Mango.api_key = ENV['MANGO_SECRET_KEY']
34+
35+
params = {
36+
37+
name: "John Doe"
38+
}
39+
40+
begin
41+
customer = Mango::Customer.create params
42+
rescue Mango::Error => e
43+
e.each {|code, message| ... }
44+
end
45+
```
46+
47+
You can set a global API_KEY (`Mango.api_key`) or override it on each request
48+
49+
50+
## API
51+
52+
All the requests go through:
53+
```
54+
Mango.request(method, url, api_key=nil, params={}, headers={})
55+
```
56+
57+
But you can use CRUD methods on `Mango::Card`, `Mango::Customer`...
58+
```
59+
def create params={}, api_key=nil
60+
def list params={}, api_key=nil
61+
def retrieve uid, params={}, api_key=nil
62+
def delete uid, params={}, api_key=nil
63+
def update uid, params={}, api_key=nil
64+
```
65+
66+
So this two are equivalent:
67+
```
68+
customer = Mango::Customer.create params
69+
customer = Mango.request :post, '/customers/', api_key, params
70+
```
71+
72+
This are the operations available for each resource:
73+
74+
|Resource|list|create|retrieve|update|delete|delete_all|
75+
|-|:-:|:-:|:-:|:-:|:-:|:-:|
76+
|Card | x | x | x | x | x | |
77+
|Charge | x | x | x | | | |
78+
|Customer | x | x | x | x | x | |
79+
|Installment | | | x | | | |
80+
|Queue | x | x | x | | x | x |
81+
|Refund | x | x | x | | | |

bin/mango

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#!/usr/bin/env ruby

lib/mango-ruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require File.dirname(__FILE__) + '/mango'

lib/mango.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'rest_client'
2+
require 'json'
3+
require_relative 'mango/resources'
4+
5+
module Mango
6+
7+
class Error < StandardError
8+
include Enumerable
9+
attr_accessor :errors
10+
11+
def initialize message, errors={}
12+
super message
13+
@errors = errors
14+
end
15+
16+
# Yield [error_code, message] pairs
17+
def each
18+
@errors.each { |e| yield *e.first }
19+
end
20+
end
21+
22+
@api_base = 'https://api.getmango.com/v1'
23+
24+
class << self
25+
attr_accessor :api_key, :api_base
26+
end
27+
28+
def self.request(method, url, api_key=nil, params={}, headers={})
29+
method = method.to_sym
30+
31+
url = @api_base + url
32+
33+
unless api_key ||= @api_key
34+
raise Error.new('No API key provided')
35+
end
36+
37+
payload = JSON.generate(params) if method == :post || method == :patch
38+
39+
headers = {
40+
:content_type => 'application/json'
41+
}.merge(headers)
42+
43+
options = {
44+
:headers => headers,
45+
:user => api_key,
46+
:method => method,
47+
:url => url,
48+
:payload => payload
49+
}
50+
51+
begin
52+
response = execute_request(options)
53+
return {} if response.code == 204 and method == :delete
54+
JSON.parse(response.body, :symbolize_names => true)
55+
rescue RestClient::Exception => e
56+
handle_errors e
57+
end
58+
end
59+
60+
private
61+
62+
def self.execute_request(options)
63+
RestClient::Request.execute(options)
64+
end
65+
66+
def self.handle_errors exception
67+
body = JSON.parse exception.http_body
68+
raise Error.new(exception.to_s, body['errors'])
69+
end
70+
71+
end

lib/mango/operations.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module Mango
2+
module Operations
3+
module ClassMethods
4+
5+
private
6+
7+
def create params={}, api_key=nil
8+
Mango.request :post, url, api_key, params
9+
end
10+
11+
def list params={}, api_key=nil
12+
Mango.request :get, url, api_key, params
13+
end
14+
15+
def retrieve uid, params={}, api_key=nil
16+
require_uid uid
17+
Mango.request :get, url(uid), api_key, params
18+
end
19+
20+
def update uid, params={}, api_key=nil
21+
require_uid uid
22+
Mango.request :patch, url(uid), api_key, params
23+
end
24+
25+
def delete uid, params={}, api_key=nil
26+
require_uid uid
27+
Mango.request :delete, url(uid), api_key, params
28+
end
29+
30+
def delete_all params={}, api_key=nil
31+
Mango.request :delete, url, api_key, params
32+
end
33+
34+
def require_uid uid
35+
raise Error.new('UID is required') if uid.nil? || uid == ''
36+
end
37+
38+
end
39+
40+
def self.included(base)
41+
base.extend(ClassMethods)
42+
end
43+
end
44+
end

lib/mango/resources.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require_relative 'operations'
2+
3+
module Mango
4+
class Resource
5+
include Operations
6+
7+
def self.url id=nil
8+
resource_name = self.name.split('::').last.downcase
9+
id ? "/#{resource_name}/#{id}/" : "/#{resource_name}/"
10+
end
11+
end
12+
13+
class Customers < Resource
14+
public_class_method :create, :retrieve, :list, :update, :delete
15+
end
16+
17+
class Charges < Resource
18+
public_class_method :create, :retrieve, :list
19+
end
20+
21+
class Refunds < Resource
22+
public_class_method :create, :retrieve, :list
23+
end
24+
25+
class Cards < Resource
26+
public_class_method :create, :retrieve, :list, :update, :delete
27+
end
28+
29+
class Queue < Resource
30+
public_class_method :create, :retrieve, :list, :delete, :delete_all
31+
end
32+
33+
class Installments < Resource
34+
public_class_method :retrieve
35+
end
36+
37+
end

mango-ruby.gemspec

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# encoding: utf-8
2+
3+
Gem::Specification.new do |s|
4+
s.name = "mango-ruby"
5+
s.version = "0.0.1"
6+
s.summary = "Ruby wrapper for Mango API"
7+
s.description = "API to interact with Mango\nhttps://getmango.com/"
8+
s.authors = ["Joaquín Vicente"]
9+
s.email = ["[email protected]"]
10+
s.homepage = "https://getmango.com/"
11+
s.files = []
12+
13+
s.license = "MIT"
14+
s.executables.push("mango")
15+
s.add_dependency('rest-client', '~> 1.7')
16+
s.add_development_dependency('cutest', '~> 1.2')
17+
s.add_development_dependency('mocha', '~> 1.1')
18+
19+
s.files = %w{
20+
bin/mango
21+
lib/mango.rb
22+
lib/mango-ruby.rb
23+
lib/mango/resources.rb
24+
lib/mango/operations.rb
25+
}
26+
27+
s.test_files = %w{
28+
test/mango_test.rb
29+
test/test_helper.rb
30+
}
31+
32+
end

0 commit comments

Comments
 (0)