From 3d11d0a3a047a12bfd40b61252e269cabac76225 Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Fri, 15 Feb 2013 14:49:16 +0100 Subject: Basic structure for sim_cards and sim_card_providers. --- app/models/sim_card.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/models/sim_card.rb (limited to 'app/models/sim_card.rb') diff --git a/app/models/sim_card.rb b/app/models/sim_card.rb new file mode 100644 index 0000000..2cbf76b --- /dev/null +++ b/app/models/sim_card.rb @@ -0,0 +1,25 @@ +class SimCard < ActiveRecord::Base + attr_accessible :auto_order_card, :sip_account_id, :auth_key, :sim_number + + # Validations + # + validates :sim_card_provider_id, + :presence => true + + belongs_to :sim_card_provider, :touch => true + + validates :sim_card_provider, + :presence => true + + validates :sip_account_id, + :presence => true + + belongs_to :sip_account + + validates :sip_account, + :presence => true + + validates :sim_number, + :presence => true + +end -- cgit v1.2.3 From 14e8e9923666991703f747a4abfd6fbfb96d2dc1 Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Fri, 15 Feb 2013 15:03:37 +0100 Subject: Show sim_cards of a user. Misc. --- app/models/sim_card.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'app/models/sim_card.rb') diff --git a/app/models/sim_card.rb b/app/models/sim_card.rb index 2cbf76b..806beab 100644 --- a/app/models/sim_card.rb +++ b/app/models/sim_card.rb @@ -22,4 +22,15 @@ class SimCard < ActiveRecord::Base validates :sim_number, :presence => true + after_initialize :set_defaults + + def to_s + self.sim_number.to_s + end + + private + def set_defaults + self.state ||= 'not activated' + end + end -- cgit v1.2.3 From b1cbfe7a7bd5e966be14b71be4e201cc9e2b1aa6 Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Fri, 15 Feb 2013 15:56:16 +0100 Subject: Activate a new sim_card. --- app/models/sim_card.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'app/models/sim_card.rb') diff --git a/app/models/sim_card.rb b/app/models/sim_card.rb index 806beab..2bf7304 100644 --- a/app/models/sim_card.rb +++ b/app/models/sim_card.rb @@ -24,6 +24,9 @@ class SimCard < ActiveRecord::Base after_initialize :set_defaults + before_validation :upcase_some_values + after_create :active_sim_card + def to_s self.sim_number.to_s end @@ -33,4 +36,28 @@ class SimCard < ActiveRecord::Base self.state ||= 'not activated' end + def upcase_some_values + self.sim_number = self.sim_number.to_s.upcase + end + + def active_sim_card + require 'open-uri' + + url = "#{self.sim_card_provider.api_server_url}/app/api/main?cmd=order&ref=#{self.sim_number}&s=#{self.sim_card_provider.sip_server}&u=#{self.sip_account.auth_name}&p=#{self.sip_account.password}&ordercard=0&apiuser=#{self.sim_card_provider.api_username}&apipass=#{self.sim_card_provider.api_password}" + + open(url, "User-Agent" => "Ruby/#{RUBY_VERSION}", + "From" => "admin@localhost", + "Referer" => "http://amooma.com/gemeinschaft/gs5") { |f| + # Save the response body + @response = f.read + } + + if @response.class == String && @response.split(/;/).first == 'OK' + self.state = 'activated' + self.auth_key = @response.split(/;/).last.chomp.split(/=/).last + self.save + end + + end + end -- cgit v1.2.3