Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
174 lines (120 sloc) 4.19 KB

Square logo

Square Connect Ruby SDK - RETIRED


Build Status Gem Version Apache-2 license

NOTICE: Square Connect Ruby SDK retired

The Square Connect Ruby SDK is retired (EOL) as of 2019-08-15 and will no longer receive bug fixes or product updates. To continue receiving API and SDK improvements, please follow the instructions below to migrate to the new Square Ruby SDK gem.

The old Connect SDK documentation is available under the /docs folder.





Migrate to the Square Ruby SDK

Follow the instructions below to migrate your apps from the deprecated square_connect gem to the new square.rb gem.

Update your bundle

  1. Find the line in your Gemfile starting with gem: 'square_connect' and change the entire line to gem: 'square.rb'.
  2. Run bundle to update your Gemfile.lock.

Update your code

  1. Change all instances of require 'square_connect' to require 'square'.
  2. Replace models with plain Ruby Hash equivalents.
  3. Update client instantiation to follow the method outlined below.
  4. Update code for accessing response data to follow the method outlined below.
  5. Check response.success? or response.error? rather than rescuing exceptions for flow control.

To simplify your code, we also recommend that you use method chaining to access APIs instead of explicitly instantiating multiple clients.

Client instantiation

require 'square'

square = Square::Client.new(access_token: 'YOUR ACCESS TOKEN')

response = square.API.ENDPOINT(body: BODY)

Accessing response data

if response.success?
  p response.data
else
  warn response.errors
end



Example code migration

As a specific example, consider the following code for creating a new customer from the following Hash:

new_customer = {
 given_name: 'Ava',
 address: {
   address_line_1: '555 Electric Ave',
   locality: 'Los Angeles',
   country: 'US'
 }
}

With the deprecated square_connect gem, this is how you instantiate a client for the Customers API, format the request, and call the endpoint:

require 'square_connect'

# Instantiate the client
SquareConnect.configure do |config|
  config.access_token = 'YOUR ACCESS TOKEN'
end

api_instance = SquareConnect::CustomersApi.new

# Create the models
address = SquareConnect::Address.new(new_customer[:address])

body = SquareConnect::CreateCustomerRequest.new(
  given_name: new_customer[:given_name],
  address: address
)

begin
  # Call the endpoint
  response = api_instance.create_customer(body)

  # Handle the response and warn on errors
  p response.customer.to_hash
rescue SquareConnect::ApiError
  warn response.errors
end

Now consider equivalent code using the new square.rb gem:

require 'square'

# Instantiate the client
square = Square::Client.new(access_token: 'YOUR ACCESS TOKEN')

# Call the endpoint
response = square.customers.create_customer(body: new_customer)

# Handle the response and warn on errors
if response.success?
  p response.data
else
  warn response.errors
end

That's it!

What was once a multi-block process can be handled in 2 lines of code and an if/else block. Migrating to the square.rb gem reduces boilerplate and lets you focus on the parts of your code that really matter.




Ask the community

Please join us in our Square developer community if you have any questions!

You can’t perform that action at this time.