Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Ruby Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Ruby) UPS Tracking API

Demonstrates making a call to the UPS tracking REST API. Parses the tracking response and extracts the base64 signature image to a gif file.

Chilkat Ruby Downloads

Ruby Library for Linux and MAC OS X

require 'chilkat'

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

http = Chilkat::CkHttp.new()

# This is the testing endpoint for the tracking API:
url = "https://wwwcie.ups.com/rest/Track"

# Send an HTTP request with the following JSON body:

# {
#   "UPSSecurity": {
#     "UsernameToken": {
#       "Username": "Your Username",
#       "Password": "Your Password"
#     },
#     "ServiceAccessToken": {
#       "AccessLicenseNumber": "Your Access License Number"
#     }
#   },
#   "TrackRequest": {
#     "Request": {
#       "RequestOption": "1",
#       "TransactionReference": {
#         "CustomerContext": "Your Test Case Summary Description"
#       }
#     },
#     "InquiryNumber": "YourTrackingNumber"
#   }
# }
# 

# Build the above JSON.
json = Chilkat::CkJsonObject.new()
json.UpdateString("UPSSecurity.UsernameToken.Username","UPS_USERNAME")
json.UpdateString("UPSSecurity.UsernameToken.Password","UPS_PASSWORD")
json.UpdateString("UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY")
# Request all activity...
json.UpdateString("TrackRequest.Request.RequestOption","activity")
json.UpdateString("TrackRequest.Request.TransactionReference.CustomerContext","Your Test Case Summary Description")
json.UpdateString("TrackRequest.InquiryNumber","1Z12345E0205271688")

sb = Chilkat::CkStringBuilder.new()
# resp is a CkHttpResponse
resp = http.PostJson3(url,"application/json",json)
if (http.get_LastMethodSuccess() != true)
    print http.lastErrorText() + "\n";
    exit
end

print "status = " + resp.get_StatusCode().to_s() + "\n";

# A 200 response status indicate success.
if (resp.get_StatusCode() != 200)
    print resp.bodyStr() + "\n";
    print "Failed." + "\n";

    exit
end

json.Load(resp.bodyStr())
json.put_EmitCompact(false)
print json.emit() + "\n";

# {
#   "TrackResponse": {
#     "Response": {
#       "ResponseStatus": {
#         "Code": "1",
#         "Description": "Success"
#       },
#       "TransactionReference": {
#         "CustomerContext": "Your Test Case Summary Description"
#       }
#     },
#     "Shipment": {
#       "InquiryNumber": {
#         "Code": "01",
#         "Description": "ShipmentIdentificationNumber",
#         "Value": "1Z12345E0205271688"
#       },
#       "ShipmentType": {
#         "Code": "01",
#         "Description": "Small Package"
#       },
#       "ShipperNumber": "12345E",
#       "Service": {
#         "Code": "002",
#         "Description": "2ND DAY AIR"
#       },
#       "ReferenceNumber": {
#         "Code": "01",
#         "Value": "LINE4AND115"
#       },
#       "PickupDate": "19990608",
#       "Package": {
#         "TrackingNumber": "1Z12345E0205271688",
#         "Activity": [
#           {
#             "ActivityLocation": {
#               "Address": {
#                 "City": "ANYTOWN",
#                 "StateProvinceCode": "GA",
# 
#                 "PostalCode": "30340",
#                 "CountryCode": "US"
#               },
#               "Code": "ML",
#               "Description": "BACK DOOR",
#               "SignedForByName": "JOHN DOE"
#             },
#             "Status": {
#               "Type": "D",
#               "Description": "DELIVERED",
#               "Code": "KM"
#             },
#             "Date": "19990610",
#             "Time": "120000",
#             "Document": {
#               "Type": {
#                 "Code": "01",
#                 "Description": "Signature Image"
#               },
#               "Content": "R0lGODdhoA ... JU9Y8RdHsRKLMVJ4MVDMREAAADs=",
#               "Format": {
#                 "Code": "01",
#                 "Description": "GIF"
#               }
#             }
#           },
#           {
#             "Status": {
#               "Type": "M",
#               "Description": "BILLING INFORMATION RECEIVED. SHIPMENT DATE PENDING.",
#               "Code": "MP"
#             },
#             "Date": "19990608",
#             "Time": "120000"
#           }
#         ],
#         "PackageWeight": {
#           "UnitOfMeasurement": {
#             "Code": "LBS"
#           },
#           "Weight": "5.00"
#         },
#         "ReferenceNumber": [
#           {
#             "Code": "01",
#             "Value": "LINE4AND115"
#           },
#           {
#             "Code": "08",
#             "Value": "LJ67Y5"
#           }
#         ]
#       }
#     },
#     "Disclaimer": "You are using UPS tracking service on customer integration test environment, please switch to UPS production environment once you finish the test. The URL is https://onlinetools.ups.com/webservices/Track"
#   }
# }

# Use the online tool at Generate JSON Parsing Code
# to generate JSON parsing code.

statusCode = json.stringOf("TrackResponse.Response.ResponseStatus.Code")
statusDescription = json.stringOf("TrackResponse.Response.ResponseStatus.Description")
print "statusCode: " + statusCode + "\n";
print "statusDescription" + statusDescription + "\n";
customerContext = json.stringOf("TrackResponse.Response.TransactionReference.CustomerContext")
inquiryNumberCode = json.stringOf("TrackResponse.Shipment.InquiryNumber.Code")
inquiryNumberDescription = json.stringOf("TrackResponse.Shipment.InquiryNumber.Description")
inquiryNumberValue = json.stringOf("TrackResponse.Shipment.InquiryNumber.Value")
shipmentTypeCode = json.stringOf("TrackResponse.Shipment.ShipmentType.Code")
shipmentTypeDescription = json.stringOf("TrackResponse.Shipment.ShipmentType.Description")
shipperNumber = json.stringOf("TrackResponse.Shipment.ShipperNumber")
serviceCode = json.stringOf("TrackResponse.Shipment.Service.Code")
serviceDescription = json.stringOf("TrackResponse.Shipment.Service.Description")
referenceNumberCode = json.stringOf("TrackResponse.Shipment.ReferenceNumber.Code")
referenceNumberValue = json.stringOf("TrackResponse.Shipment.ReferenceNumber.Value")
pickupDate = json.stringOf("TrackResponse.Shipment.PickupDate")
trackingNumber = json.stringOf("TrackResponse.Shipment.Package.TrackingNumber")
unitOfMeasurementCode = json.stringOf("TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code")
weight = json.stringOf("TrackResponse.Shipment.Package.PackageWeight.Weight")
disclaimer = json.stringOf("TrackResponse.Disclaimer")

i = 0
activityCount = json.SizeOfArray("TrackResponse.Shipment.Package.Activity")
print "activityCount: " + activityCount.to_s() + "\n";

while i < activityCount
    print "-- activity " + i.to_s() + "\n";
    json.put_I(i)
    if (json.HasMember("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") == true)
        city = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City")
        print "city: " + city + "\n";
        provinceCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode")
        postalCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode")
        countryCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode")
    end

    locationCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code")
    locationDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description")
    locationSignedForByName = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName")

    activityStatusType = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Status.Type")
    print "activityStatusType: " + activityStatusType + "\n";
    activityStatusDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Status.Description")
    print "activityStatusDescription: " + activityStatusDescription + "\n";
    activityStatusCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Status.Code")
    print "activityStatusCode: " + activityStatusCode + "\n";

    activityDate = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Date")
    activityTime = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Time")

    if (json.HasMember("TrackResponse.Shipment.Package.Activity[i].Document") == true)
        typeCode = json.IntOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Code")
        typeDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Description")
        documentContent = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Content")
        documentFormatCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Code")
        # Format description would be something like "GIF" for a signature image.
        documentFormatDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Description")

        # 01 - Signature Image
        # 02 - Delivery Receipt
        # 03 - Free Astray
        # 04 - POD
        if (typeCode == 1)
            # We have a signature image.  Get the image data and save to a file.
            sbImagePath = Chilkat::CkStringBuilder.new()
            sbImagePath.Append("qa_output/sig_")
            sbImagePath.Append(trackingNumber)
            sbImagePath.Append(".")
            sbImagePath.Append(documentFormatDescription)
            imageData = Chilkat::CkBinData.new()
            success = imageData.AppendEncoded(documentContent,"base64")
            # Write to "qa_output/sig_1Z12345E0205271688.GIF"
            success = imageData.WriteFile(sbImagePath.getAsString())
        end

    end

    i = i + 1
end

i = 0
refnumCount = json.SizeOfArray("TrackResponse.Shipment.Package.ReferenceNumber")
while i < refnumCount
    json.put_I(i)
    refnumCode = json.stringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Code")
    refnumValue = json.stringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Value")
    i = i + 1
end

print "Success." + "\n";


 

© 2000-2016 Chilkat Software, Inc. All Rights Reserved.