Class: Square::Item

Overview

Class Method Summary collapse

Methods included from APIOperations::Delete

delete

Methods included from APIOperations::Update

update

Methods included from APIOperations::Create

create

Methods included from APIOperations::Retrieve

retrieve

Methods included from APIOperations::List

list

Methods inherited from APIResource

data_type, endpoint_base, nested_under

Class Method Details

.upload_image(image_url, item_id) ⇒ Net::HTTPResponse

Parameters:

  • image_url (String)

    Image URL.

  • item_id (String)

    Square item id.

Returns:

  • (Net::HTTPResponse)

    API response.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/square/item.rb', line 20

def self.upload_image(image_url, item_id)
  image = open(image_url)
  filename = File.basename(image_url)

  uri = URI.parse("#{Square.api_host}/v1/me/#{instance_variable_get('@endpoint_base')}/#{item_id}/image")
  connection = Net::HTTP.new(uri.host, uri.port)
  connection.use_ssl = (uri.scheme == 'https')

  # Making this request here because RestClient doesn't do multipart
  # requests the way Square wants it.
  request = Net::HTTP::Post::Multipart.new(
    uri.path,
    image_data: UploadIO.new(image, 'image/jpeg', filename)
  )

  # Copy the auth header.
  request['Authorization'] = Square.request_headers(Square.access_token)[:authorization]
  connection.request(request)
end