Skip to content

Commit

Permalink
Merge branch 'dev'. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
glorieux committed Aug 29, 2013
2 parents 30b59c8 + b11819d commit 02d4faf
Show file tree
Hide file tree
Showing 34 changed files with 971 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ spec/reports
test/tmp
test/version_tmp
tmp
tags

# YARD artifacts
.yardoc
_yardoc
doc/

.DS_Store
.rvmrc
/Gemfile.lock
/mangopay-*.gem
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--profile
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source "https://rubygems.org"
gemspec
70 changes: 66 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
mangopay2-ruby-sdk
==================
# MangoPay2 Ruby SDK

Ruby Gem for Mangopay apiV2
Tested with the following versions of Ruby: 1.9.2, 1.9.3, 2.0.0
The gem for interacting with the version 2 of the MangoPay API.
See the [API documentation]() for more details on the API.

Tested on the following versions of Ruby: 1.9.2, 1.9.3, 2.0.0

## Code Status
[![Build Status]()]()
[![Code Climate]()]()
[![Dependency Status]()]()

## NEWS

### Version 3.0.0
** BREAKING CHANGES **
This version of the gem is targeting the MangoPay API Version 2.
It has a brand new structure to make the api calls easier to use

## Usage

### Install
* Install the gem by either running ```gem install mangopay``` or by adding it to your Gemfile ```gem 'mangopay'```

* The Rails users will be happy to know that there is a new generator script that will help you configure your access to the MangoPay API version 2.
Simply run ``rails generate mangopay:install CLIENT_ID CLIENT_NAME`` where CLIENT_ID is the id you will use to connect to the api and CLIENT_NAME is a full name that will be use to identify all communications between you and the MangoPay Team.

### Examples


### Tests
Make sure that you have run: ```bundle install```
Then you just have to run rspec ```rspec``` to run all the test suite.
Feel free to report any test failure by creating an issue on the [Gem's Github](https://github.com/MangoPay/mangopay2-ruby-sdk/issues)

## Contributing

1. Fork the repo.

2. Run the tests. We only take pull requests with passing tests, and it's great
to know that you have a clean slate: `bundle && bundle exec rake`

3. Add a test for your change. Only refactoring and documentation changes
require no new tests. If you are adding functionality or fixing a bug, we need
a test!

4. Make the test pass.

5. Push to your fork and submit a pull request.

At this point you're waiting on us. We like to at least comment on, if not
accept, pull requests within three business days (and, typically, one business
day). We may suggest some changes or improvements or alternatives.

Syntax:

* Two spaces, no tabs.
* No trailing whitespace. Blank lines should not have any space.
* Prefer &&/|| over and/or.
* MyClass.my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
* a = b and not a=b.
* Follow the conventions you see used in the source already.

A contribution can also be as simple as a +1 on issues tickets to show us what you would like to see in this gem.

That's it for now. Good Hacking...
>>>>>>> dev
9 changes: 9 additions & 0 deletions bin/mangopay
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby

irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'

libs = " -r irb/completion"
libs << " -r #{File.dirname(__FILE__) + '/../lib/mangopay'}"

puts "Loading MangoPay gem"
exec "#{irb} #{libs}"
60 changes: 60 additions & 0 deletions lib/generators/mangopay/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'rails/generators/base'
require 'mangopay'

module Mangopay
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../../templates', __FILE__)
argument :client_id, type: :string,
desc: 'The id you want to use to query the MangoPay API (must match with the regex ^[a-z0-9_-]{4,20}$)'
argument :client_name, type: :string, desc: "Full name of you're organization"
argument :client_email, type: :string, desc: "An email for future contacts"
class_option :preproduction, type: :boolean, default: true, desc: 'Whether or not use the preproduction environment'

desc 'Installs all the basic configuration of the mangopay gem'
def setup
begin
client = client_id_valid?
remove_file 'config/initializers/mangopay.rb'
@client_id = client_id
@client_passphrase = client['Passphrase']
template 'mangopay.rb', 'config/initializers/mangopay.rb'
rescue => e
puts e.message
end
end

protected

def client_id_valid?
check_client_id_validity
check_client_id_availablility
end

def check_client_id_validity
if (/^[a-z0-9_-]{4,20}$/ =~ client_id).nil?
raise "The client_id must match the regexp ^[a-z0-9_-]{4,20}$"
end
end

def check_client_id_availablility
client = create_client
if client['Type'] == 'ClientID_already_exist'
raise client['Message']
end
client
end

def create_client
MangoPay.configure do |c|
c.preproduction = options[:preproduction]
end
MangoPay::Client.create({
ClientID: client_id,
Name: client_name,
Email: client_email
})
end
end
end
end
5 changes: 5 additions & 0 deletions lib/generators/templates/mangopay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MangoPay.configure do |c|
c.preproduction = <%= options[:preproduction] %>
c.client_id = '<%= @client_id %>'
c.client_passphrase = '<%= @client_passphrase %>'
end
115 changes: 115 additions & 0 deletions lib/mangopay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
require 'net/http'
require 'multi_json'

# Version
require 'mangopay/version'

# JSON
require 'mangopay/json'

# Resources
require 'mangopay/http_calls'
require 'mangopay/resource'
require 'mangopay/client'
require 'mangopay/user'
require 'mangopay/natural_user'
require 'mangopay/legal_user'
require 'mangopay/payin'
require 'mangopay/payout'
require 'mangopay/transfer'
require 'mangopay/transaction'
require 'mangopay/wallet'
require 'mangopay/bank_account'

# Errors
require 'mangopay/errors'

module MangoPay

class Configuration
attr_accessor :root_url, :client_id, :client_passphrase, :preproduction

def preproduction
@preproduction || false
end

def root_url
@root_url || (@preproduction == true ? "https://mangopay-api-inte.leetchi.com" : "https://api.leetchi.com")
end
end

class << self
attr_accessor :configuration
end

def self.configure
self.configuration ||= Configuration.new
yield configuration
end

def self.api_uri(url='')
URI(configuration.root_url + url)
end

def self.request(method, url, params={}, headers={})
uri = api_uri(url)

res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
request = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, request_headers)
request.body = MangoPay::JSON.dump(params)
http.request request
end
MangoPay::JSON.load(res.body)
end

def self.user_agent
@uname ||= get_uname

{
bindings_version: MangoPay::VERSION,
lang: 'ruby',
lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
platform: RUBY_PLATFORM,
uname: @uname
}
end

def self.get_uname
`uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
rescue Errno::ENOMEM
'uname lookup failed'
end

def self.get_oauth_token
if @auth_timestamp.nil? || @auth_timestamp <= Time.now || @auth_token.nil?
uri = api_uri('/api/oauth/token')
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
req = Net::HTTP::Post.new(uri.request_uri)
req.basic_auth configuration.client_id, configuration.client_passphrase
req.body = 'grant_type=client_credentials'
http.request req
end
@auth_token = MangoPay::JSON.load(res.body)
@auth_timestamp = Time.now + @auth_token['expires_in'].to_i
end
@auth_token
end

def self.oauth_token
oauth = get_oauth_token
"#{oauth['token_type']} #{oauth['access_token']}"
end

def self.request_headers
headers = {
'user_agent' => "MangoPay V1 RubyBindings/#{MangoPay::VERSION}",
'Authorization' => oauth_token,
'Content-Type' => 'application/json'
}
begin
headers.update('x_mangopay_client_user_agent' => MangoPay::JSON.dump(user_agent))
rescue => e
headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})")
end
end
end
21 changes: 21 additions & 0 deletions lib/mangopay/bank_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module MangoPay
class BankAccount < Resource
include MangoPay::HTTPCalls::Create
include MangoPay::HTTPCalls::Fetch

def self.fetch(*ids)
url = ids.length == 1 ? url(ids[0]) : url(ids[0], ids[1])
MangoPay.request(:get, url)
end

private

def self.url(*id)
if id.length == 1
"/v2/#{MangoPay.configuration.client_id}/users/#{CGI.escape(id[0])}/bankaccounts"
else
"/v2/#{MangoPay.configuration.client_id}/users/#{CGI.escape(id[0])}/bankaccounts/#{CGI.escape(id[1])}"
end
end
end
end
17 changes: 17 additions & 0 deletions lib/mangopay/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module MangoPay
class Client < Resource

def self.create(params)
uri = URI(MangoPay.configuration.root_url + '/api/clients/')
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
request = Net::HTTP::Post.new(uri.request_uri, {
'user_agent' => "MangoPay V1 RubyBindings/#{MangoPay::VERSION}",
'Content-Type' => 'application/json'
})
request.body = MangoPay::JSON.dump(params)
http.request request
end
MangoPay::JSON.load(res.body)
end
end
end
4 changes: 4 additions & 0 deletions lib/mangopay/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module MangoPay
class Errors
end
end
53 changes: 53 additions & 0 deletions lib/mangopay/http_calls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module MangoPay
module HTTPCalls
module Create
module ClassMethods

def create(*id, params)
id = id.empty? ? nil : id[0]
response = MangoPay.request(:post, url(id), params)
end
end

def self.included(base)
base.extend(ClassMethods)
end
end

module Update
module ClassMethods
def update(id = nil, params = {})
response = MangoPay.request(:put, url(id), params)
end
end

def self.included(base)
base.extend(ClassMethods)
end
end

module Fetch
module ClassMethods
def fetch(id = nil, filters = {})
response = MangoPay.request(:get, url(id), filters)
end
end

def self.included(base)
base.extend(ClassMethods)
end
end

module Refund
module ClassMethods
def refund(id = nil, params = {})
MangoPay.request(:post, url(id) + '/refunds', params)
end
end

def self.included(base)
base.extend(ClassMethods)
end
end
end
end
Loading

0 comments on commit 02d4faf

Please sign in to comment.