Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Dobrzhansky
committed
Sep 17, 2018
1 parent
60b04d0
commit e2aa524
Showing
37 changed files
with
1,855 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
version: 2 | ||
general: | ||
branches: | ||
only: | ||
- master | ||
|
||
.build_template: &build_steps | ||
steps: | ||
- checkout | ||
- run: bundle install --jobs=4 --retry=4 --path vendor/bundle | ||
- run: bundle exec rspec | ||
|
||
jobs: | ||
build_ruby2_3: | ||
<<: *build_steps | ||
docker: | ||
- image: ruby:2.3 | ||
build_ruby2_4: | ||
<<: *build_steps | ||
docker: | ||
- image: ruby:2.4 | ||
build_ruby2_5: | ||
<<: *build_steps | ||
docker: | ||
- image: ruby:2.5 | ||
build_ruby2_6rc: | ||
<<: *build_steps | ||
docker: | ||
- image: ruby:2.6-rc | ||
|
||
workflows: | ||
version: 2 | ||
build_ruby_versions: | ||
jobs: | ||
- build_ruby2_3 | ||
- build_ruby2_4 | ||
- build_ruby2_5 | ||
- build_ruby2_6rc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
*.gem | ||
*.rbc | ||
.idea/ | ||
/.config | ||
/coverage/ | ||
/InstalledFiles | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in coinbase_commerce.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,268 @@ | ||
[![CircleCI](https://circleci.com/gh/coinbase/coinbase-commerce-ruby/tree/master.svg?style=svg)](https://circleci.com/gh/coinbase/coinbase-commerce-ruby/tree/master) | ||
|
||
# coinbase-commerce-ruby | ||
Coinbase Commerce Ruby Gem | ||
|
||
# Table of contents | ||
|
||
<!--ts--> | ||
* [Ruby Versions](#ruby-version) | ||
* [Third Party Libraries and Dependencies](#third-party-libraries-and-dependencies) | ||
* [Documentation](#documentation) | ||
* [Installation](#installation) | ||
* [Usage](#usage) | ||
* [Checkouts](#checkouts) | ||
* [Charges](#charges) | ||
* [Events](#events) | ||
* [Validating webhook signatures](#validating-webhook-signatures) | ||
* [Testing and Contributing](#testing-and-contributing) | ||
<!--te--> | ||
|
||
## Ruby Version | ||
Ruby 2.3, 2.4, 2.5, 2.6RC are supported and tested. | ||
|
||
## Third Party Libraries and Dependencies | ||
|
||
The following libraries will be installed when you install the client library: | ||
* [faraday](https://github.com/lostisland/faraday) | ||
|
||
## Documentation | ||
|
||
For more details visit [Coinbase API docs](https://commerce.coinbase.com/docs/api/). | ||
|
||
To start using library, you'll need to [create a Coinbase Commmerce account](https://commerce.coinbase.com/signup). | ||
Once you've created your Coinbase Commerce account, create an ``API_KEY`` in Settings. | ||
|
||
Next create a ``Client`` object for interacting with the API: | ||
```ruby | ||
require 'coinbase_commerce' | ||
|
||
API_KEY = "API KEY" | ||
client = CoinbaseCommerce::Client.new(api_key: API_KEY) | ||
|
||
``` | ||
|
||
``Client`` contains links to every Ruby Class representations of the API resources | ||
``Checkout, Charge, Event`` | ||
|
||
You can call ``list, auto_paging, create, retrieve, modify`` methods from API resource classes | ||
|
||
```ruby | ||
client.charge.create | ||
client.checkout.auto_paging | ||
client.event.list | ||
client.charge.retrieve | ||
client.checkout.modify | ||
``` | ||
as well as ``save, delete, refresh`` methods from API resource class instances. | ||
```python | ||
checkout = client.checkout.retrieve <id> | ||
checkout.refresh | ||
checkout.save | ||
checkout.delete | ||
``` | ||
|
||
Each API method returns an ``APIObject`` representing the JSON response from the API, all of the models support hash and JSON representation.\ | ||
Also when the response data is parsed into Ruby objects, the appropriate ``APIObject`` subclasses will be used automatically. | ||
All subclasses of ``APIResource`` class support ``refresh`` method. This will update their attributes and all nested data by making a fresh ``GET`` request to the relevant API endpoint. | ||
|
||
The client supports handling of common API errors and warnings. | ||
All errors occuring during the interaction with the API will be raised as exceptions. | ||
|
||
|
||
| Error | Status Code | | ||
|--------------------------|-------------| | ||
| APIError | * | | ||
| InvalidRequestError | 400 | | ||
| ParamRequiredError | 400 | | ||
| ValidationError | 400 | | ||
| AuthenticationError | 401 | | ||
| ResourceNotFoundError | 404 | | ||
| RateLimitExceededError | 429 | | ||
| InternalServerError | 500 | | ||
| ServiceUnavailableError | 503 | | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
```ruby | ||
gem 'coinbase_commerce' | ||
``` | ||
Then execute: | ||
|
||
```sh | ||
bundle install | ||
``` | ||
|
||
Or install it yourself as: | ||
|
||
```sh | ||
gem install coinbase_commerce | ||
``` | ||
|
||
## Usage | ||
```ruby | ||
require 'coinbase_commerce' | ||
|
||
client = CoinbaseCommerce::Client.new(api_key: 'your_api_key') | ||
``` | ||
## Checkouts | ||
[Checkouts API docs](https://commerce.coinbase.com/docs/api/#checkouts) | ||
### Retrieve | ||
```ruby | ||
checkout = client.checkout.retrieve <checkout_id> | ||
``` | ||
### Create | ||
```ruby | ||
checkout_info = { | ||
"name": "The Sovereign Individual", | ||
"description": "Mastering the Transition to the Information Age", | ||
"pricing_type": "fixed_price", | ||
"local_price": { | ||
"amount": "1.00", | ||
"currency": "USD" | ||
}, | ||
"requested_info": ["name", "email"] | ||
} | ||
checkout = client.checkout.create(checkout_info) | ||
|
||
# or | ||
|
||
checkout = client.checkout.create(:name=>'The Sovereign Individual', | ||
:description=>'Mastering the Transition to the Information Age', | ||
:pricing_type=>'fixed_price', | ||
:local_price=>{ | ||
"amount": "100.00", | ||
"currency": "USD" | ||
}, | ||
:requested_info=>["name", "email"]) | ||
``` | ||
### Update | ||
```ruby | ||
checkout = client.checkout.retrieve <checkout_id> | ||
checkout.name = 'new name' | ||
checkout.save | ||
|
||
# or | ||
|
||
client.checkout.modify(<checkout_id>, "local_price": { | ||
"amount": "10000.00", | ||
"currency": "USD" | ||
}) | ||
``` | ||
### Delete | ||
```ruby | ||
checkout.delete | ||
``` | ||
### List | ||
```ruby | ||
checkouts = client.checkout.list | ||
``` | ||
### Paging list iterations | ||
```ruby | ||
client.checkout.auto_paging do |ch| | ||
puts ch.id | ||
end | ||
``` | ||
## Charges | ||
[Charges API docs](https://commerce.coinbase.com/docs/api/#charges) | ||
### Retrieve | ||
```ruby | ||
charge = client.charge.retrieve <charge_id> | ||
``` | ||
### Create | ||
```ruby | ||
charge_info = { | ||
"name": "The Sovereign Individual", | ||
"description": "Mastering the Transition to the Information Age", | ||
"pricing_type": "fixed_price", | ||
"local_price": { | ||
"amount": "1.00", | ||
"currency": "USD" | ||
}, | ||
"requested_info": ["name", "email"] | ||
} | ||
charge = client.charge.create(charge_info) | ||
|
||
# or | ||
|
||
charge = client.charge.create(:name=>'The Sovereign Individual', | ||
:description=>'Mastering the Transition to the Information Age', | ||
:pricing_type=>'fixed_price', | ||
:local_price=>{ | ||
"amount": "100.00", | ||
"currency": "USD" | ||
}) | ||
``` | ||
### List | ||
```ruby | ||
charges_list = client.charge.list | ||
``` | ||
### Paging list iterations | ||
```ruby | ||
client.charge.auto_paging do |charge| | ||
puts charge.id | ||
end | ||
``` | ||
## Events | ||
[Events API Docs](https://commerce.coinbase.com/docs/api/#events) | ||
### Retrieve | ||
```ruby | ||
event = client.event.retrieve <event_id> | ||
``` | ||
### List | ||
```ruby | ||
events = client.event.list | ||
``` | ||
### Paging list iterations | ||
```ruby | ||
client.event.auto_paging do |event| | ||
puts event.id | ||
end | ||
``` | ||
|
||
## Validating webhook signatures | ||
You should verify the webhook signatures using our library. | ||
To perform the verification you'll need to provide the event data, a webhook signature from the request header, and the endpoint’s secret. | ||
In case of an invalid request signature or request payload, you will receive an appropriate error message. | ||
```ruby | ||
WEBHOOK_SECRET = 'your_webhook_secret' | ||
|
||
# Using Sinatra | ||
post '/webhooks' do | ||
request_payload = request.body.read | ||
sig_header = request.env['HTTP_X_CC_WEBHOOK_SIGNATURE'] | ||
|
||
begin | ||
event = CoinbaseCommerce::Webhook.construct_event(request_payload, sig_header, WEBHOOK_SECRET) | ||
# event handle | ||
puts "Received event id=#{event.id}, type=#{event.type}" | ||
status 200 | ||
# errors handle | ||
rescue JSON::ParserError => e | ||
puts "json parse error" | ||
status 400 | ||
return | ||
rescue CoinbaseCommerce::Errors::SignatureVerificationError => e | ||
puts "signature verification error" | ||
status 400 | ||
return | ||
rescue CoinbaseCommerce::Errors::WebhookInvalidPayload => e | ||
puts "missing request or headers data" | ||
status 400 | ||
return | ||
end | ||
end | ||
``` | ||
|
||
### Testing and Contributing | ||
Any and all contributions are welcome! The process is simple: fork this repo, make your changes, add tests, run the test suite, and submit a pull request. Tests are run via rspec. To run the tests, clone the repository and then: | ||
|
||
# Install the requirements | ||
gem install coinbase_commerce | ||
rspec spec | ||
|
||
# or via Bundle | ||
bundle install | ||
bundle exec rspec spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new | ||
|
||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib")) | ||
require 'coinbase_commerce/version' | ||
|
||
Gem::Specification.new do |gem| | ||
gem.name = "coinbase_commerce" | ||
gem.version = CoinbaseCommerce::VERSION | ||
gem.license = "MIT" | ||
gem.required_ruby_version = ">= 2.0.0" | ||
gem.authors = ["Alexander Dobrzhansky",] | ||
|
||
gem.description = "Client library for Coinbase Commerce API" | ||
gem.summary = "Client library for Coinbase Commerce API" | ||
gem.homepage = "https://commerce.coinbase.com/docs/api/" | ||
|
||
gem.files = `git ls-files`.split($/) | ||
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } | ||
gem.test_files = gem.files.grep(%r{^(spec|gem|features)/}) | ||
gem.require_paths = ["lib"] | ||
|
||
gem.add_dependency("faraday", "~> 0.10") | ||
|
||
gem.add_development_dependency "rake" | ||
gem.add_development_dependency "rspec" | ||
gem.add_development_dependency "webmock" | ||
gem.add_development_dependency "pry-byebug" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'coinbase_commerce' | ||
|
||
client = CoinbaseCommerce::Client.new(api_key: 'your_api_key') | ||
|
||
# create charge | ||
data = { | ||
"name": "The Sovereign Individual", | ||
"description": "Mastering the Transition to the Information Age", | ||
"local_price": { | ||
"amount": "100.00", | ||
"currency": "USD" | ||
}, | ||
"pricing_type": "fixed_price" | ||
|
||
} | ||
charge = client.charge.create(data) | ||
|
||
# or retrieve it if you know charge id | ||
charge = client.charge.retrieve charge.id | ||
|
||
# get charges list | ||
charges_list = client.charge.list | ||
|
||
# in case you need provide additional params | ||
charges_list = client.charge.list(limit: 10) | ||
|
||
# or get results from another page | ||
charges_list = client.charge.list(starting_after: charge.id, limit: 3) | ||
|
||
# charges list could be iterated like | ||
charges_list.data.each do |charge| | ||
puts charge.id | ||
end | ||
|
||
# iterate over all charges with per-page limitation | ||
client.charge.auto_paging limit: 20 do |charge| | ||
puts charge.id | ||
end |
Oops, something went wrong.