1+ #!/usr/bin/env ruby
2+ # frozen_string_literal: true
3+
4+ # Example demonstrating file upload functionality in the Nylas Ruby SDK
5+ # Tests both small (<3MB) and large (>3MB) file handling with the new HTTParty implementation
6+ #
7+ # This example shows how to:
8+ # 1. Send messages with small attachments (<3MB) - handled as JSON with base64 encoding
9+ # 2. Send messages with large attachments (>3MB) - handled as multipart form data
10+ # 3. Create test files of appropriate sizes for demonstration
11+ # 4. Handle file upload errors and responses
12+ #
13+ # Prerequisites:
14+ # - Ruby 3.0 or later
15+ # - A Nylas API key
16+ # - A grant ID (connected email account)
17+ # - A test email address to send to
18+ #
19+ # Environment variables needed:
20+ # export NYLAS_API_KEY="your_api_key"
21+ # export NYLAS_GRANT_ID="your_grant_id"
22+ # export NYLAS_TEST_EMAIL="test@example.com" # Email address to send test messages to
23+ # export NYLAS_API_URI="https://api.us.nylas.com" # Optional
24+ #
25+ # Alternatively, create a .env file in the examples directory with:
26+ # NYLAS_API_KEY=your_api_key
27+ # NYLAS_GRANT_ID=your_grant_id
28+ # NYLAS_TEST_EMAIL=test@example.com
29+ # NYLAS_API_URI=https://api.us.nylas.com
30+
31+ $LOAD_PATH. unshift File . expand_path ( '../../lib' , __dir__ )
32+ require "nylas"
33+ require "json"
34+ require "tempfile"
35+
36+ # Enhanced error logging helper
37+ def log_detailed_error ( error , context = "" )
38+ puts "\n ❌ ERROR DETAILS #{ context . empty? ? '' : "- #{ context } " } "
39+ puts "=" * 60
40+ puts "Error Class: #{ error . class } "
41+ puts "Error Message: #{ error . message } "
42+
43+ if error . respond_to? ( :response ) && error . response
44+ puts "HTTP Response Code: #{ error . response . code } " if error . response . respond_to? ( :code )
45+ puts "HTTP Response Body: #{ error . response . body } " if error . response . respond_to? ( :body )
46+ puts "HTTP Response Headers: #{ error . response . headers } " if error . response . respond_to? ( :headers )
47+ end
48+
49+ if error . respond_to? ( :request_id ) && error . request_id
50+ puts "Request ID: #{ error . request_id } "
51+ end
52+
53+ puts "Full Stack Trace:"
54+ puts error . backtrace . join ( "\n " )
55+ puts "=" * 60
56+ end
57+
58+ # Simple .env file loader
59+ def load_env_file
60+ env_file = File . expand_path ( '../.env' , __dir__ )
61+ return unless File . exist? ( env_file )
62+
63+ puts "Loading environment variables from .env file..."
64+ File . readlines ( env_file ) . each do |line |
65+ line = line . strip
66+ next if line . empty? || line . start_with? ( '#' )
67+
68+ key , value = line . split ( '=' , 2 )
69+ next unless key && value
70+
71+ # Remove quotes if present
72+ value = value . gsub ( /\A ['"]|['"]\z / , '' )
73+ ENV [ key ] = value
74+ end
75+ rescue => e
76+ log_detailed_error ( e , "loading .env file" )
77+ raise
78+ end
79+
80+ # Def send message with small attachment
81+ def send_message_with_attachment ( nylas , grant_id , recipient_email , test_file_path , content_type )
82+ # load the file and read it's contents
83+ file_contents = File . read ( test_file_path )
84+
85+ # manually build file_attachment
86+ file_attachment = {
87+ filename : File . basename ( test_file_path ) ,
88+ content_type : content_type ,
89+ content : file_contents ,
90+ size : File . size ( test_file_path )
91+ }
92+
93+ request_body = {
94+ subject : "Test Email with Attachment" ,
95+ to : [ { email : recipient_email } ] ,
96+ body : "This is a test email with a attachment.\n \n File size: #{ File . size ( test_file_path ) } bytes\n Sent at: #{ Time . now } " ,
97+ attachments : [ file_attachment ]
98+ }
99+
100+ puts "- Sending message with large attachment..."
101+ puts "- Recipient: #{ recipient_email } "
102+ puts "- Attachment size: #{ File . size ( test_file_path ) } bytes"
103+ puts "- Expected handling: Multipart form data"
104+ puts "- Request body keys: #{ request_body . keys } "
105+
106+ response , request_id = nylas . messages . send (
107+ identifier : grant_id ,
108+ request_body : request_body
109+ )
110+
111+ puts "Response: #{ response } "
112+ puts "Request ID: #{ request_id } "
113+ puts "Grant ID: #{ response [ :grant_id ] } "
114+ puts "Message ID: #{ response [ :id ] } "
115+ puts "Message Subject: #{ response [ :subject ] } "
116+ puts "Message Body: #{ response [ :body ] } "
117+
118+ end
119+
120+ def main
121+ puts "=== Nylas File Upload Example - HTTParty Migration Test ==="
122+
123+ begin
124+ # Load .env file if it exists
125+ load_env_file
126+
127+ # Check for required environment variables
128+ api_key = ENV [ "NYLAS_API_KEY" ]
129+ grant_id = ENV [ "NYLAS_GRANT_ID" ]
130+ test_email = ENV [ "NYLAS_TEST_EMAIL" ]
131+
132+ puts "- Checking environment variables..."
133+ raise "NYLAS_API_KEY environment variable is not set" unless api_key
134+ raise "NYLAS_GRANT_ID environment variable is not set" unless grant_id
135+ raise "NYLAS_TEST_EMAIL environment variable is not set" unless test_email
136+
137+ puts "Using API key: #{ api_key [ 0 ..4 ] } ..."
138+ puts "Using grant ID: #{ grant_id [ 0 ..8 ] } ..."
139+ puts "Test email recipient: #{ test_email } "
140+ puts "API URI: #{ ENV [ "NYLAS_API_URI" ] || "https://api.us.nylas.com" } "
141+
142+ # Initialize the Nylas client
143+ puts "- Initializing Nylas client..."
144+ nylas = Nylas ::Client . new (
145+ api_key : api_key ,
146+ api_uri : ENV [ "NYLAS_API_URI" ] || "https://api.us.nylas.com"
147+ )
148+ puts "- Nylas client initialized successfully"
149+
150+ # Demonstrate file handling logic
151+ jpg_file_path = File . expand_path ( "large_jpg_test_file.jpg" , __dir__ )
152+ unless File . exist? ( jpg_file_path )
153+ raise "JPG test file not found at #{ jpg_file_path } . Please create a JPG file for testing."
154+ end
155+ send_message_with_attachment ( nylas , grant_id , test_email , jpg_file_path , "image/jpeg" )
156+
157+ puts "\n === File Upload Example Completed Successfully ==="
158+ rescue => e
159+ log_detailed_error ( e , "main method" )
160+ raise
161+ end
162+ end
163+
164+ main
0 commit comments