module Square
Constants
- VERSION
Attributes
access_token[RW]
api_host[RW]
api_version[RW]
merchant_id[RW]
Public Class Methods
batch(requests = [{}], options = {}, &block)
click to toggle source
{
method: "POST", path: "categories", body: { "name": "Beverages" }
}
# File lib/square.rb, line 165 def self.batch(requests = [{}], options = {}, &block) merchant = options[:merchant] || 'me' requests = requests.map {|request| path = request.delete(:path) request[:relative_path] = File.join('/', 'v1', merchant, path) request[:access_token] = access_token request[:request_id] ||= SecureRandom.uuid request } request_params = { headers: request_headers(access_token), method: :post, url: File.join(api_host, 'v1', 'batch'), payload: { requests: requests }.to_json } begin response = RestClient::Request.execute(request_params, &block) JSON.parse(response) rescue RestClient::Exception => e Logger.new(STDOUT).error(e.http_body) raise e end end
make_request(options = {}, &block)
click to toggle source
Make an API call to Square
.
@param options [Hash] Hash of options. Expects something like this:
{
method: 'POST', endpoint: 'items', request: {...}
}
@return [RestClient::Response]
# File lib/square.rb, line 86 def self.make_request(options = {}, &block) if access_token.nil? raise StandardError.new('No access token set.') end # Default to a GET request. method = (options[:method] || :get).downcase.to_sym # Allow passing in a fully formed URL. if !options[:url].nil? url = options[:url] else merchant = options[:merchant] || merchant_id # Special handling of the merchant param. if !options[:params].nil? && !options[:params][:merchant].nil? merchant = options[:params].delete(:merchant) end if !options[:payload].nil? && !options[:payload][:merchant].nil? merchant = options[:payload].delete(:merchant) end path_args = [api_host, api_version, merchant, options[:endpoint]].compact url = File.join(path_args) end # Build up the RestClient request object. request_params = { headers: request_headers(access_token).merge(options[:headers] || {}), method: method, url: url } # Merge in a payload hash. payload = options[:payload] || nil if !payload.nil? && payload.respond_to?(:to_json) && !payload.empty? payload = payload.to_json request_params.merge!(payload: payload) end # Merge in a params hash. params = options[:params] || nil if !params.nil? && !params.empty? request_params[:headers].merge!(params: params) end # Perform the request. self.request(request_params, &block) end
parse_response(response)
click to toggle source
Parse a response.
@param response [RestClient::Response]
@return [#to_json]
# File lib/square.rb, line 149 def self.parse_response(response) begin JSON.parse(response, symbolize_names: true) rescue RestClient::Exception => e Logger.new(STDOUT).error(e.http_body) raise e end end
request(request, &block)
click to toggle source
Request helper. Makes testing and switching out the http client easy.
# File lib/square.rb, line 140 def self.request(request, &block) RestClient::Request.execute(request, &block) end
request_headers(access_token)
click to toggle source
Get the request headers.
@param [String] Square
access token.
@return [Hash] Hash of headers.
# File lib/square.rb, line 199 def self.request_headers(access_token) { authorization: "Bearer #{access_token}", accept: 'application/json', content_type: 'application/json' } end