Class: SmsBroker::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/sms_broker/setup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSetup

Returns a new instance of Setup.



28
29
30
31
32
33
34
35
# File 'lib/sms_broker/setup.rb', line 28

def initialize
  @errors = {}
  @options = {
    services: ['nexmo'],
    default_service: 'nexmo',
    services_setups: {}
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, args, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/sms_broker/setup.rb', line 71

def method_missing(method, args, &block)
  service = method.to_s.split('_setup')[0].dup

  if @options[:services].include?(service)
    @options[:services_setups][service.to_sym] = args
    @options[:services_setups]
  else
    super
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/sms_broker/setup.rb', line 3

def errors
  @errors
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/sms_broker/setup.rb', line 3

def options
  @options
end

Class Method Details

.service_validation_schemasObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sms_broker/setup.rb', line 6

def self.service_validation_schemas
  {
    nexmo: Compel.hash.keys(
      key: Compel.string.required,
      secret: Compel.string.required,
      sender_id: Compel.string,
      phone_number: Compel.string.required
    ),
    twilio: Compel.hash.keys(
      sender_id: Compel.string,
      auth_token: Compel.string.required,
      account_sid: Compel.string.required,
      phone_number: Compel.string.required
    ),
    open_market: Compel.hash.keys(
      account_id: Compel.string.required,
      account_password: Compel.string.required,
      sender_id: Compel.string
    )
  }
end

Instance Method Details

#compel_validation_schema(services_list = []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sms_broker/setup.rb', line 53

def compel_validation_schema(services_list = [])
  not_all_services_setup = proc do |services_setups|
    services_list.all? do |service|
      services_setups.keys.include?(service.to_sym)
    end
  end

  services_setups_schema = \
    Compel.hash.required
      .keys(Setup.service_validation_schemas)
      .if(not_all_services_setup, message: 'all services must be setup')

  Compel.hash.keys \
    services: Compel.array.required.min_length(1),
    default_service: Compel.string.required.in(services_list),
    services_setups: services_setups_schema
end

#default_service(service) ⇒ Object



41
42
43
# File 'lib/sms_broker/setup.rb', line 41

def default_service(service)
  @options[:default_service] = service
end

#services(services) ⇒ Object



37
38
39
# File 'lib/sms_broker/setup.rb', line 37

def services(services)
  @options[:services] = services
end

#valid?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'lib/sms_broker/setup.rb', line 45

def valid?
  result = compel_validation_schema(@options[:services]).validate(@options)

  @errors = result.errors

  result.valid?
end