summaryrefslogtreecommitdiff
path: root/app/models/sim_card.rb
blob: 2bf7304630f483954b788bd19ceae7afea1063b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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

  after_initialize :set_defaults

  before_validation :upcase_some_values
  after_create :active_sim_card

  def to_s
    self.sim_number.to_s
  end

  private
  def set_defaults 
    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