Created
August 30, 2023 16:57
-
-
Save Streek/48936bd5d73ac29aef2b411bc6621509 to your computer and use it in GitHub Desktop.
zinc_api.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "net/http" | |
require "json" | |
require "base64" | |
class ZincAPI | |
API_ENDPOINT = "https://api.zinc.io/v1" | |
def initialize(client_token) | |
@client_token = client_token | |
end | |
# Create an order | |
def create_order(order_details) | |
post_request("/orders", order_details) | |
end | |
# Retrieve an order by request_id | |
def get_order(request_id) | |
get_request("/orders/#{request_id}") | |
end | |
# Retrieve all orders based on provided filters | |
def get_all_orders(filters = {}) | |
params = URI.encode_www_form(filters) | |
get_request("/orders?#{params}") | |
end | |
# Abort an order | |
def abort_order(request_id) | |
post_request("/orders/#{request_id}/abort") | |
end | |
# Retry an order | |
def retry_order(request_id) | |
post_request("/orders/#{request_id}/retry") | |
end | |
# Amazon email verification (assuming you provide the verification code) | |
def verify_email(request_id, verification_code) | |
details = { | |
request_id: request_id, | |
verification_code: verification_code | |
} | |
post_request("/orders/#{request_id}/verify", details) | |
end | |
private | |
def get_request(path) | |
uri = URI("#{API_ENDPOINT}#{path}") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
request = Net::HTTP::Get.new(uri, { | |
"Authorization" => "Basic #{Base64.encode64(@client_token + ":")}" | |
}) | |
response = http.request(request) | |
JSON.parse(response.body) | |
end | |
def post_request(path, body = {}) | |
uri = URI("#{API_ENDPOINT}#{path}") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
request = Net::HTTP::Post.new(uri, { | |
"Content-Type" => "application/json", | |
"Authorization" => "Basic #{Base64.encode64(@client_token + ":")}" | |
}) | |
request.body = body.to_json | |
response = http.request(request) | |
JSON.parse(response.body) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment