Skip to content

Commit

Permalink
Abstract API request to new class
Browse files Browse the repository at this point in the history
* Create Singleton NetworkHandler class
* Make NetworkHandler responsible of tracking request
  • Loading branch information
Jeasmine committed Nov 29, 2021
1 parent a644c0f commit 1f99404
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
7 changes: 6 additions & 1 deletion lib/oscli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down Expand Up @@ -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"
Expand Down
34 changes: 34 additions & 0 deletions lib/osnetwork.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/osproject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def has_sdk?
end

def self.version
'1.0'
'0.0.0'
end

def self.os
Expand Down
43 changes: 12 additions & 31 deletions lib/osproject_android.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand All @@ -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'
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 1f99404

Please sign in to comment.