Skip to content

Instantly share code, notes, and snippets.

@nwadams
Created January 14, 2022 20:44
Show Gist options
  • Save nwadams/00a0ed1e09d052ca4890a8c32b684553 to your computer and use it in GitHub Desktop.
Save nwadams/00a0ed1e09d052ca4890a8c32b684553 to your computer and use it in GitHub Desktop.
api only upload
require 'berbix'
require 'net/http'
require 'json'
require 'base64'
def upload_image(access_token, is_front, encoded_image)
uri = URI('https://nick.dev.berbix.com:8443/v0/images/upload')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
req['Authorization'] = "Bearer #{access_token}"
payload = {}
image_data = {}
image_data['image_subject'] = is_front ? 'document_front' : 'document_back'
image_data['format'] = 'image/jpeg'
image_data['data'] = encoded_image
payload['image'] = image_data
req.body = payload.to_json
res = http.request(req)
json_body = JSON.parse(res.body)
return json_body
end
# Construct the client, providing your API secret
client = Berbix::Client.new(
api_secret: 'secret',
api_host: 'https://nick.dev.berbix.com:8443'
)
uri = URI('https://nick.dev.berbix.com:8443/v0/transactions')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
req.basic_auth('secret', '')
payload = {}
payload['customer_uid'] = (0...8).map { (65 + rand(26)).chr }.join # ID for the user in client database
payload['template_key'] = 'template'
payload['api_only_options'] = {"id_type" => "DL", "id_country" =>"US"}
puts payload.to_json
req.body = payload.to_json
res = http.request(req)
json_body = JSON.parse(res.body)
puts "response #{json_body}"
access_token = json_body['access_token']
puts access_token
# Open the file you wish to encode
front = File.open('/Users/nickadams/Documents/id_photos/20220113_151530.jpg').read
front_encoded = Base64.encode64(front)
back = File.open('/Users/nickadams/Documents/id_photos/20220113_151535.jpg').read
back_encoded = Base64.encode64(back)
front_response = front_response = upload_image(access_token, true, front_encoded)
puts front_response
back_response = upload_image(access_token, false, back_encoded)
puts back_response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment