Module: SevenApi::Hooks::Validator

Defined in:
lib/seven_api/hooks.rb

Class Method Summary collapse

Class Method Details

.event_type?(str) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/seven_api/hooks.rb', line 56

def self.event_type?(str)
  SevenApi::Util::in_module_constants?(str, SevenApi::Hooks::EventType)
end

.is_action?(str) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/seven_api/hooks.rb', line 52

def self.is_action?(str)
  SevenApi::Util::in_module_constants?(str, SevenApi::Hooks::Action)
end

.request_method?(str) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/seven_api/hooks.rb', line 60

def self.request_method?(str)
  SevenApi::Util::in_module_constants?(str, SevenApi::Hooks::RequestMethod)
end

.subscribe(params) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/seven_api/hooks.rb', line 40

def self.subscribe(params)
  { :request_method => SevenApi::Hooks::RequestMethod::POST }.merge!(params)

  self.event_type?(params[:event_type]) &&
    self.request_method?(params[:request_method]) &&
    self.target_url?(params[:target_url])
end

.target_url?(str) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/seven_api/hooks.rb', line 64

def self.target_url?(str)
  SevenApi::Util::is_valid_url?(str)
end

.unsubscribe(params) ⇒ Object



48
49
50
# File 'lib/seven_api/hooks.rb', line 48

def self.unsubscribe(params)
  SevenApi::Util::is_positive_integer?(params[:id])
end

.validate(params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/seven_api/hooks.rb', line 28

def self.validate(params)
  action = params[:action]

  raise "Unknown action #{action}" unless SevenApi::Hooks::Validator::is_action?(action)

  if SevenApi::Hooks::Action::SUBSCRIBE == action
    raise 'Parameter validation failed' unless SevenApi::Hooks::Validator::subscribe(params)
  elsif SevenApi::Hooks::Action::UNSUBSCRIBE == action
    raise 'ID must be a positive integer' unless SevenApi::Hooks::Validator::unsubscribe(params)
  end
end