diff --git a/lib/oscli.rb b/lib/oscli.rb index 48a3343..478ec8e 100644 --- a/lib/oscli.rb +++ b/lib/oscli.rb @@ -2,6 +2,9 @@ require_relative 'osproject' require_relative 'osproject_ios' require_relative 'osproject_android' +require 'net/http' +require 'uri' +require 'resolv-replace' class InstallCommand < Clamp::Command option [ "-t", "--type"], "TYPE", "project type (ios, android)" @@ -69,11 +72,13 @@ def execute class OSCLI < Clamp::Command option ["--version", "-v"], :flag, "Show version" do - puts "0.0.0" + puts OSProject.version + # TODO add tracking exit(0) end option ["--help", "-h"], :flag, "Show Commands" do OSCLI.helptext + # TODO add tracking exit(0) end self.default_subcommand = "available-commands" diff --git a/lib/osnetwork.rb b/lib/osnetwork.rb new file mode 100644 index 0000000..1555a33 --- /dev/null +++ b/lib/osnetwork.rb @@ -0,0 +1,34 @@ +require 'clamp' +require_relative 'osproject' +require_relative 'osproject_ios' +require_relative 'osproject_android' + +class NetworkHandler + @instance = new + + private_class_method :new + + def self.instance + @instance + end + + def send_track_from_message(app_id, platform, lang, success_mesage, append_message) + actions_taken = success_mesage.gsub(" * ", "").gsub("\n",";") + actions_taken += append_message + send_track_actions(app_id, platform, lang, actions_taken) + end + + def send_track_actions(app_id, platform, lang, actions_taken) + uri = URI.parse('https://api.onesignal.com/api/v1/track') + + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + + request = Net::HTTP::Post.new(uri.request_uri) + request.set_form_data({}) + request['app_id'] = app_id + request['OS-Usage-Data'] = 'lib-name=' + OSProject.tool_name + ',lib-version=' + OSProject.version + ',lib-os=' + OSProject.os + ',lib-type=' + platform + ',lib-lang=' + lang + ',lib-actions=' + actions_taken + + response = http.request(request) + end +end \ No newline at end of file diff --git a/lib/osproject.rb b/lib/osproject.rb index 048db88..094e191 100644 --- a/lib/osproject.rb +++ b/lib/osproject.rb @@ -32,7 +32,7 @@ def has_sdk? end def self.version - '1.0' + '0.0.0' end def self.os diff --git a/lib/osproject_android.rb b/lib/osproject_android.rb index 24e8963..d4af0a3 100644 --- a/lib/osproject_android.rb +++ b/lib/osproject_android.rb @@ -1,21 +1,20 @@ require_relative 'osproject' require_relative 'osproject_helpers' -require 'net/http' -require 'uri' -require 'resolv-replace' +require_relative 'osnetwork' class OSProject::GoogleAndroid < OSProject attr_accessor :app_class_location def initialize(app_class_location, os_app_id) @app_class_location = app_class_location - + directory_split = app_class_location.split('/', -1) lang_array = directory_split[-1].split(".") + if lang_array.length() == 1 puts 'Missing Application class file extension (.kt or .java)' error_track_message = "error=user missed Application class file extension;" - send_track_actions(os_app_id, 'android', "nil", error_track_message) + NetworkHandler.instance.send_track_actions(os_app_id, 'android', "nil", error_track_message) exit(1) end @@ -25,7 +24,7 @@ def initialize(app_class_location, os_app_id) unless lang == "java" || lang == "kt" puts 'Invalid language (java or kotlin)' error_track_message = "error=user entered invalid language: " + lang + ";" - send_track_actions(os_app_id, 'android', lang, error_track_message) + NetworkHandler.instance.send_track_actions(os_app_id, 'android', lang, error_track_message) exit(1) end @@ -38,6 +37,8 @@ def add_sdk! raise end + network_handler = NetworkHandler.instance + project_dir = dir app_dir = app_class_location.split('/')[0] build_gradle_dir = project_dir + '/build.gradle' @@ -50,7 +51,7 @@ def add_sdk! puts "File not found: " + build_gradle_dir puts "Call CLI tool from base project directory" error_track_message = "error=user called CLI from invalid directory file: " + build_gradle_dir + " not found;" - send_track_from_message(os_app_id, 'android', lang, success_mesage, error_track_message) + network_handler.send_track_from_message(os_app_id, 'android', lang, success_mesage, error_track_message) return end @@ -61,7 +62,7 @@ def add_sdk! puts "Provide --entrypoint param as Application file path directory. If no Appplication class available, OneSignal will create it at the directory provided." puts "Example: app/src/main/java/com/onesignal/testapplication/OneSignalApplication.java" error_track_message = "error=user entered invalid Application file path: " + project_dir + '/' + app_dir + " directory not found;" - send_track_from_message(os_app_id, 'android', lang, success_mesage, error_track_message) + network_handler.send_track_from_message(os_app_id, 'android', lang, success_mesage, error_track_message) return end @@ -86,7 +87,7 @@ def add_sdk! unless user_response == "y" || user_response == "yes" || user_response == "n" || user_response == "no" puts 'Invalid response (Y/N)' error_track_message = "error=user entered invalid response for Application class creation command used: " + user_response + ";" - send_track_from_message(os_app_id, 'android', lang, success_mesage, error_track_message) + NetworkHandler.instance.send_track_from_message(os_app_id, 'android', lang, success_mesage, error_track_message) exit(1) end @@ -168,10 +169,10 @@ def add_sdk! actions_taken = success_mesage.gsub(" * ", "").gsub("\n",";") success_mesage = "*** OneSignal integration completed successfully! ***\n\n" + success_mesage if success_mesage == "*** OneSignal integration completed successfully! ***\n\n" - send_track_actions(os_app_id, 'android', lang, "no actions, sdk already integrated;") + network_handler.send_track_actions(os_app_id, 'android', lang, "no actions, sdk already integrated;") puts "*** OneSignal already integrated, no changes needed ***\n\n" else - send_track_actions(os_app_id, 'android', lang, actions_taken) + network_handler.send_track_actions(os_app_id, 'android', lang, actions_taken) puts success_mesage end end @@ -265,26 +266,6 @@ def add_application_init_code(project_dir, app_class_location, lang) return success_mesage end - def send_track_from_message(app_id, platform, lang, success_mesage, append_message) - actions_taken = success_mesage.gsub(" * ", "").gsub("\n",";") - actions_taken += append_message - send_track_actions(os_app_id, 'android', lang, actions_taken) - end - - def send_track_actions(app_id, platform, lang, actions_taken) - uri = URI.parse('https://api.onesignal.com/api/v1/track') - - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - - request = Net::HTTP::Post.new(uri.request_uri) - request.set_form_data({}) - request['app_id'] = app_id - request['OS-Usage-Data'] = 'lib-name=' + OSProject.tool_name + ',lib-version=' + OSProject.version + ',lib-os=' + OSProject.os + ',lib-type=' + platform + ',lib-lang=' + lang + ',lib-actions=' + actions_taken - - response = http.request(request) - end - def has_sdk? # TODO: more robust testing return File.readlines(dir + '/app/build.gradle').grep(/OneSignal/).any?