Module: Square

Defined in:
lib/square.rb,
lib/square/fee.rb,
lib/square/item.rb,
lib/square/refund.rb,
lib/square/webhook.rb,
lib/square/payment.rb,
lib/square/version.rb,
lib/square/merchant.rb,
lib/square/discount.rb,
lib/square/category.rb,
lib/square/inventory.rb,
lib/square/variation.rb,
lib/square/data_type.rb,
lib/square/settlement.rb,
lib/square/api_resource.rb,
lib/square/bank_accounts.rb,
lib/square/list_response.rb,
lib/square/data_types/fee.rb,
lib/square/data_types/item.rb,
lib/square/data_types/money.rb,
lib/square/data_types/refund.rb,
lib/square/data_types/device.rb,
lib/square/data_types/tender.rb,
lib/square/data_types/payment.rb,
lib/square/data_types/discount.rb,
lib/square/data_types/category.rb,
lib/square/api_operations/list.rb,
lib/square/data_types/merchant.rb,
lib/square/data_types/item_image.rb,
lib/square/api_operations/delete.rb,
lib/square/api_operations/update.rb,
lib/square/api_operations/create.rb,
lib/square/data_types/settlement.rb,
lib/square/data_types/payment_tax.rb,
lib/square/data_types/coordinates.rb,
lib/square/data_types/phone_number.rb,
lib/square/data_types/bank_account.rb,
lib/square/api_operations/retrieve.rb,
lib/square/data_types/modifier_list.rb,
lib/square/data_types/global_address.rb,
lib/square/data_types/item_variation.rb,
lib/square/data_types/inventory_entry.rb,
lib/square/data_types/modifier_option.rb,
lib/square/data_types/payment_modifier.rb,
lib/square/data_types/settlement_entry.rb,
lib/square/data_types/payment_discount.rb,
lib/square/data_types/payment_item_detail.rb,
lib/square/data_types/payment_itemization.rb,
lib/square/data_types/merchant_location_details.rb

Defined Under Namespace

Modules: APIOperations, DataTypes Classes: APIResource, BankAccount, Category, DataType, Discount, Fee, Inventory, Item, ListResponse, Merchant, Payment, Refund, Settlement, Variation, Webhook

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.access_tokenObject

Returns the value of attribute access_token



72
73
74
# File 'lib/square.rb', line 72

def access_token
  @access_token
end

.api_hostObject

Returns the value of attribute api_host



72
73
74
# File 'lib/square.rb', line 72

def api_host
  @api_host
end

.api_versionObject

Returns the value of attribute api_version



72
73
74
# File 'lib/square.rb', line 72

def api_version
  @api_version
end

.merchant_idObject

Returns the value of attribute merchant_id



72
73
74
# File 'lib/square.rb', line 72

def merchant_id
  @merchant_id
end

Class Method Details

.batch(requests = [{}], options = {}, &block) ⇒ Object

{

method: "POST",
path: "categories",
body: {
  "name": "Beverages"
}

}



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# 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) ⇒ RestClient::Response

Make an API call to Square.

method: 'POST',
endpoint: 'items',
request: {...

}

Parameters:

  • options (Hash) (defaults to: {})

    Hash of options. Expects something like this:

Returns:

  • (RestClient::Response)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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
    # Special handling of the merchant param.
    if !options[:params].nil? && options[:params].key?(:merchant)
      merchant = options[:params].delete(:merchant)
    end

    if !options[:payload].nil? && options[:payload].key?(:merchant)
      merchant = options[:payload].delete(:merchant)
    end

    merchant = merchant || merchant_id

    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) ⇒ #to_json

Parse a response.

Parameters:

  • response (RestClient::Response)

Returns:

  • (#to_json)


149
150
151
152
153
154
155
156
# 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) ⇒ Object

Request helper. Makes testing and switching out the http client easy.



140
141
142
# File 'lib/square.rb', line 140

def self.request(request, &block)
  RestClient::Request.execute(request, &block)
end

.request_headers(access_token) ⇒ Hash

Get the request headers.

Parameters:

  • Square (String)

    access token.

Returns:

  • (Hash)

    Hash of headers.



199
200
201
202
203
204
205
# File 'lib/square.rb', line 199

def self.request_headers(access_token)
  {
    authorization: "Bearer #{access_token}",
    accept: 'application/json',
    content_type: 'application/json'
  }
end