Created
March 31, 2019 15:26
-
-
Save ediathome/0c0300923c8e29ce5627fa69ae743162 to your computer and use it in GitHub Desktop.
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
# | |
# A controller for all Paddle related stuff | |
# | |
# For use of .env file see Gist by dblandin | |
# https://gist.github.com/dblandin/8509457 | |
# | |
class PaddleController | |
def initialize | |
add_license if should_add_license? | |
# Ask the Product to get it's latest state and info from the Paddle Platform: | |
paddle_product.refresh nil | |
show_licensing unless activated? | |
end | |
# | |
# show a dialogue with the licensing information | |
# | |
def show_licensing | |
if activated? | |
show_activation_dialogue | |
else | |
paddle.showProductAccessDialogWithProduct paddle_product | |
end | |
end | |
def show_activation_dialogue | |
paddle.showLicenseActivationDialogForProduct(paddle_product, email:paddle_product.activationEmail, | |
licenseCode:paddle_product.licenseCode, activationStatusCompletion:show_license_completion_block | |
) | |
end | |
def activated? | |
paddle_product.activated | |
end | |
def deactivate_license | |
paddle_product.deactivateWithCompletion(lambda do |stat| | |
puts "successfully deactivated the license" | |
end) | |
end | |
def activate_license(email, license_code, from_cli=false) | |
Dispatch::Queue.main.async do | |
cblock = from_cli ? cli_activation_completion_block : activation_completion_block | |
paddle_product.activateEmail(email, license:license_code, completion:cblock) | |
end | |
end | |
# | |
# START Paddle Delegate methods | |
# see example at: https://github.com/PaddleHQ/sdk-mac-v4-sample/blob/master/macOS%20SDK%20v4%20Sample/AppDelegate.m | |
# | |
# def willShowPaddleUIType(uiType, product:product) | |
# # We'll unconditionally display all configurable Paddle dialogs as sheets attached to the main window. | |
# return PADDisplayConfiguration.configuration(PADDisplayTypeSheet, hideNavigationButtons:false, parentWindow:NSApp.mainWindow) | |
# end | |
def paddleDidError(error) | |
puts "Paddle error occurred: #{error}" | |
end | |
def canAutoActivate(product) | |
true | |
end | |
# END Paddle Delegate methods | |
private | |
def paddle | |
@paddle ||= proc do | |
Paddle.sharedInstanceWithVendorID(my_vendor_id, apiKey:my_api_key, productID:my_product_id, | |
configuration:productConfiguration, delegate:self) | |
end.call | |
end | |
def paddle_product | |
@paddle_product ||= proc do | |
p = PADProduct.alloc.initWithProductID(my_product_id, | |
productType:PADProductTypeSDKProduct, | |
configuration:nil | |
) | |
p | |
end.call | |
end | |
def my_product_id | |
App::ENV['PADDLE_PRODUCT_ID'] | |
end | |
def my_api_key | |
App::ENV['PADDLE_PRODUCT_API_KEY'] | |
end | |
def my_vendor_id | |
App::ENV['PADDLE_PRODUCT_VENDOR_ID'] | |
end | |
def show_license_completion_block | |
lambda do |stat| | |
case stat | |
when PADActivationActivated | |
puts "license sucessfully activated" | |
when PADActivationDeactivated | |
puts "license sucessfully deactivated" | |
when PADActivationAbandoned | |
puts "did leave dialogue without doing anything" | |
when PADActivationFailed | |
puts "Ooops something went wrong when activating the license" | |
end | |
end | |
end | |
def activation_completion_block | |
lambda do |activated, error| | |
if activated | |
puts 'Product successfully activated. Thank you for supporting Docxtor.' | |
else | |
puts 'UNABLE to activate product' | |
end | |
true | |
end | |
end | |
def cli_activation_completion_block | |
lambda do |activated, error| | |
puts "Activating from CLI" | |
if activated | |
stdout.log('Product successfully activated. Thank you for supporting Docxtor.') | |
else | |
stdout.log('Could not activate Docxtor: ' + error.localizedDescription) | |
end | |
stdout.log "\nDocxtor says: good-bye\n\n" | |
puts "Docxtor says bye-bye" | |
NSApplication.sharedApplication.terminate self | |
end | |
end | |
def productConfiguration | |
@productConfiguration ||= proc do | |
pc = PADProductConfiguration.alloc.init | |
pc.productName = "Docxtor"; | |
pc.vendorName = "Martin Kolb" | |
pc.trialLength = 0 | |
pc.trialType = PADProductTrialTimeLimited | |
pc | |
end.call | |
end | |
def support_string | |
'Please supply valid licensing data or contact [email protected] for further info' | |
end | |
# | |
# Methods for licensing via CLI | |
# e.g. by running the following command from Terminal: | |
# /Applications/Docxtor.app/Contents/MacOS/Docxtor addlicense [email protected] 12345678-12345678-12345678-12345678-12345678 | |
# | |
def should_add_license? | |
args.include? 'addlicense' | |
end | |
def add_license | |
error_status = false | |
# write this to stdout | |
if args_email.nil? | |
error_status = true | |
stdout.log "\nERROR: No valid email found. #{support_string}" | |
end | |
if args_license_code.nil? | |
error_status = true | |
stdout.log "\nERROR: No valid license code found. #{support_string}" | |
end | |
activate_license(args_email, args_license_code) unless error_status | |
end | |
def stdout | |
@stdout ||= proc { StdoutView.new }.call | |
end | |
def args_email | |
args_email_index.nil? ? nil : args.fetch(args_email_index, nil) | |
end | |
def args_license_code | |
args_email_index.nil? ? nil : args.fetch(args_email_index + 1, nil) | |
end | |
def args_email_index | |
@args_email_index ||= args.index { |x| x.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/i) } | |
end | |
def args | |
NSProcessInfo.processInfo.arguments | |
end | |
end |
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
# A class for logging to the stdout | |
class StdoutView | |
def log(out_str) | |
puts "StdoutView::puts: #{out_str}" | |
out_str.writeToFile('/dev/stdout', atomically: false, encoding: NSUTF8StringEncoding, error: nil) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment