Sha256: 1515886ca363845eb27ec94feb14cbc59a97647fe2aea88bfcaf9bcf22a952c1

Contents?: true

Size: 1.93 KB

Versions: 11

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

# Setup
#
# gem install bundler
# Ensure you have http://localhost:5678 (or PORT) as a Redirect URI in QBO.

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'dotenv'
  # gem 'ledger_sync'
  gem 'ledger_sync-quickbooks_online', path: './'
  gem 'rack'
  gem 'pd_ruby'
end

puts 'Gems installed and loaded!'

require 'pd_ruby'
require 'socket'
require 'dotenv'
require 'rack'
require 'ledger_sync/quickbooks_online'
require 'rack/lobster'

Dotenv.load('.env.local')

port = ENV.fetch('PORT', 5678)
server = TCPServer.new port

base_url = "http://localhost:#{port}"

puts "Listening at #{base_url}"

client_id = ENV.fetch('QUICKBOOKS_ONLINE_CLIENT_ID')

raise 'QUICKBOOKS_ONLINE_CLIENT_ID not set in ../.env' if client_id.blank?

client = LedgerSync::QuickBooksOnline::Client.new_from_env(test: true)

puts 'Go to the following URL:'
puts client.authorization_url(redirect_uri: base_url)

while (session = server.accept) # rubocop:disable Lint/UnreachableLoop
  request = session.gets

  puts request

  # 1
  _method, full_path = request.split

  # 2
  _path, query = full_path.split('?')

  params = query.split('&').map { |e| e.split('=') }.to_h if query.present?

  client.set_credentials_from_oauth_code(
    code: params.fetch('code'),
    realm_id: params.fetch('realmId'),
    redirect_uri: base_url
  )

  puts "\n"

  puts 'access_token:'
  puts client.access_token
  puts "\n"
  puts 'client_id:'
  puts client.client_id
  puts "\n"
  puts 'client_secret:'
  puts client.client_secret
  puts "\n"
  puts 'realm_id:'
  puts client.realm_id
  puts "\n"
  puts 'refresh_token:'
  puts client.refresh_token
  puts "\n"

  puts 'Done!'

  status = 200
  body = 'Done'
  headers = {
    'Content-Length' => body.size
  }

  session.print "HTTP/1.1 #{status}\r\n"

  headers.each do |key, value|
    session.print "#{key}: #{value}\r\n"
  end

  session.print "\r\n"

  session.print body

  session.close

  break
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ledger_sync-quickbooks_online-1.0.1 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-1.0.0 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-0.4.0 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-0.3.1 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-0.3.0 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-0.2.6 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-0.2.5 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-0.2.4 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-0.2.2 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-0.2.1 bin/quickbooks_online_oauth_server.rb
ledger_sync-quickbooks_online-0.2.0 bin/quickbooks_online_oauth_server.rb