Created
August 13, 2013 21:33
-
-
Save scalone/6225910 to your computer and use it in GitHub Desktop.
This file contains 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
module PosxmlParse | |
class Terminal | |
CONFIG_FILE_NAME = "posxml_terminal_config.dat" | |
include PosxmlParse::Threadable | |
attr_accessor :root_path, :token, :email, :password, :app_id_list | |
def initialize(root_path, use_thread, email, password) | |
@root_path = root_path | |
@email = email | |
@password = password | |
@thread = use_thread | |
setup | |
end | |
def create_token_uri | |
URI.parse("http://cloudwalk-manager-staging.herokuapp.com/api/users/token.json?email=#{email}&password=#{password}") | |
end | |
def create_logical_number_uri | |
URI.parse( "http://cloudwalk-manager-staging.herokuapp.com/api/logical_numbers/#{logical_number}.json?access_token=#{token}") | |
end | |
def create_app_uri(application_id) | |
uri.parse("http://cloudwalk-manager-staging.herokuapp.com/api/apps/#{application_id}.json?access_token=#{token}") | |
end | |
def setup | |
posxml_execute_thread do | |
request = Net::HTTP.post_form(create_token_uri, {}) | |
self.token = extract_token(request) | |
end | |
posxml_wait_thread | |
posxml_execute_thread do | |
request = Net::HTTP.get_response(create_logical_number_uri) | |
self.app_id_list = extract_app_id_list(request) | |
end | |
posxml_wait_thread | |
self.app_id_list do |application_id, application| | |
posxml_execute_thread do | |
request = Net::HTTP.get_response(create_app_uri(application_id)) | |
self.app_id_list[application_id] = extract_app_config(request)) | |
end | |
end | |
posxml_wait_thread | |
file = File.new(file_path, "w+") | |
file.write(list.to_json) | |
file.close | |
#self.content_view = | |
#linear_layout :orientation => :vertical do | |
#button :text => 'Executar App', :width => :match_parent, :id => 59, :on_click_listener => check_if_exist_posxml | |
#end | |
end | |
private | |
def extract_token(request) | |
JSON.parse(request.body)[0]["api_token"].chomp | |
end | |
def extract_app_id_list(request) | |
JSON.parse(request.body)["logical_number"]["app_ids"].inject({}) do |hash, app_id| | |
hash[app_id] = nil; hash | |
end | |
end | |
def extract_app_config(request) | |
app_json = JSON.parse(request.body)["app"] | |
app_json["posxml"] = Base64.decode64(app_json["posxml"]) | |
app_json | |
end | |
def file_path | |
"#{path}/#{CONFIG_FILE_NAME}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment