8000 GitHub - button/button-client-ruby at v1.2.0
[go: up one dir, main page]

Skip to content

button/button-client-ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

button-client-ruby Build Status

This module is a thin client for interacting with Button's API.

Please see the full API Docs for more information.

Supported runtimes

  • Ruby (MRI) >=1.9.3

Dependencies

  • None

Usage

gem install button

To create a client capable of making network requests, instantiate a Button::Client with your API key.

require 'button'

client = Button::Client.new('sk-XXX')

The client will always attempt to raise a Button::ButtonClientError in an error condition.

All API requests will return a Button::Response instance, which supports accessing data properties from the API response as methods. To access the raw response hash, use #to_hash. For instance:

require 'button'

client = Button::Client.new('sk-XXX')

begin
  response = client.orders.get('btnorder-XXX')
rescue Button::ButtonClientError => error
  puts error
end

puts response 
# => Button::Response(button_order_id: btnorder-XXX, total: 60, ... )

puts response.button_order_id
# => btnorder-XXX

puts response.to_hash()
# => {:button_order_id=>'btnorder-29de0b1436075ea6', :total=>60, ... }

n.b. the keys of the response hash will always be symbols.

Configuration

You may optionally supply a config argument with your API key:

require 'button'

client = Button::Client.new('sk-XXX', {
  hostname: 'api.testsite.com',
  port: 3000,
  secure: false,
  timeout: 5 # seconds
})

The supported options are as follows:

  • hostname: Defaults to api.usebutton.com.
  • port: Defaults to 443 if config.secure, else defaults to 80.
  • secure: Whether or not to use HTTPS. Defaults to True. N.B: Button's API is only exposed through HTTPS. This option is provided purely as a convenience for testing and development.
  • timeout: The time in seconds that may elapse before network requests abort. Defaults to nil.

Resources

We currently expose only one resource to manage, Orders.

Orders

n.b: all currency values should be reported in the smallest possible unit of that denomination, i.e. $1.00 should be reported as 100 (i.e. 100 pennies)

Create
require 'button'

client = Button::Client.new('sk-XXX')

response = client.orders.create({
  total: 50,
  currency: 'USD',
  order_id: '1994',
  finalization_date: '2017-08-02T19:26:08Z',
  btn_ref: 'srctok-XXX'
})

puts response 
# => Button::Response(button_order_id: btnorder-XXX, total: 50, ... )
Get
require 'button'

client = Button::Client.new('sk-XXX')

response = client.orders.get('btnorder-XXX')

puts response 
# => Button::Response(button_order_id: btnorder-XXX, total: 50, ... )
Update
require 'button'

client = Button::Client.new('sk-XXX')

response = client.orders.update('btnorder-XXX', total: 60)

puts response 
# => Button::Response(button_order_id: btnorder-XXX, total: 60, ... )
Delete
require 'button'

client = Button::Client.new('sk-XXX')

response = client.orders.delete('btnorder-XXX')

puts response
# => Button::Response()

Utils

Utils houses generic helpers useful in a Button Integration.

#webhook_authentic?

Used to verify that requests sent to a webhook endpoint are from Button and that their payload can be trusted. Returns true if a webhook request body matches the sent signature and false otherwise. See Webhook Security for more details.

require 'button'

Button::Utils::webhook_authentic?(
  ENV['WEBHOOK_SECRET'],
  request_body,
  request_headers.fetch('X-Button-Signature')
)

Contributing

  • Building the gem: gem build button.gemspec
  • Installing locally: gem install ./button-X.Y.Z.gem
  • Installing development dependencies: bundle install
  • Running linter: rake lint
  • Running tests: bundle exec rake

About

ruby client for the Button Order API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 6

Languages

0