Class: Gophish::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Validations, ActiveRecord::Callbacks, ActiveSupport::Inflector, HTTParty
Defined in:
lib/gophish/base.rb

Direct Known Subclasses

Campaign, Group, Page, Smtp, Template

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
# File 'lib/gophish/base.rb', line 19

def initialize(attributes = {})
  @persisted = false
  super(attributes)
  clear_changes_information
end

Class Method Details

.allObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/gophish/base.rb', line 66

def self.all
  response = get "#{resource_path}/"
  if response.response.code == '200'
    response.parsed_response.map do |data|
      instance = new filter_attributes(data)
      instance.instance_variable_set :@persisted, true
      instance
    end
  end
end

.configurationObject



25
26
27
28
29
30
31
32
# File 'lib/gophish/base.rb', line 25

def self.configuration
  @configuration ||= {
    base_uri: "#{Gophish.configuration.url}/api",
    headers: { 'Authorization' => Gophish.configuration.api_key },
    verify: Gophish.configuration.verify_ssl,
    debug_output: Gophish.configuration.debug_output ? STDOUT : nil
  }
end

.delete(path, options = {}) ⇒ Object



52
53
54
55
56
# File 'lib/gophish/base.rb', line 52

def self.delete(path, options = {})
  options = configuration.merge options
  options[:headers] = (options[:headers] || {}).merge(configuration[:headers])
  super(path, options)
end

.find(id) ⇒ Object

Raises:

  • (StandardError)


77
78
79
80
81
82
83
84
85
# File 'lib/gophish/base.rb', line 77

def self.find(id)
  response = get "#{resource_path}/#{id}"
  raise StandardError, "Resource not found with id: #{id}" if response.response.code != '200'

  data = JSON.parse response.body
  instance = new filter_attributes(data)
  instance.instance_variable_set :@persisted, true
  instance
end

.get(path, options = {}) ⇒ Object



34
35
36
37
38
# File 'lib/gophish/base.rb', line 34

def self.get(path, options = {})
  options = configuration.merge options
  options[:headers] = (options[:headers] || {}).merge(configuration[:headers])
  super(path, options)
end

.post(path, options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/gophish/base.rb', line 40

def self.post(path, options = {})
  options = configuration.merge options
  options[:headers] = (options[:headers] || {}).merge(configuration[:headers])
  super(path, options)
end

.put(path, options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/gophish/base.rb', line 46

def self.put(path, options = {})
  options = configuration.merge options
  options[:headers] = (options[:headers] || {}).merge(configuration[:headers])
  super(path, options)
end

.resource_nameObject



58
59
60
# File 'lib/gophish/base.rb', line 58

def self.resource_name
name.split('::').last.underscore.dasherize
end

.resource_pathObject



62
63
64
# File 'lib/gophish/base.rb', line 62

def self.resource_path
  "/#{resource_name.pluralize}"
end

Instance Method Details

#destroyObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gophish/base.rb', line 100

def destroy
  return false unless persisted?
  return false if id.nil?

  response = self.class.delete "#{self.class.resource_path}/#{id}"
  return handle_error_response response unless response.success?

  @persisted = false
  freeze
  true
end

#new_record?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/gophish/base.rb', line 116

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/gophish/base.rb', line 112

def persisted?
  @persisted && !id.nil?
end

#saveObject



87
88
89
90
91
92
93
# File 'lib/gophish/base.rb', line 87

def save
  return false unless valid?

  return update_record if persisted?

  create_record
end

#update_attributes(attributes) ⇒ Object



95
96
97
98
# File 'lib/gophish/base.rb', line 95

def update_attributes(attributes)
  attributes.each { |key, value| send "#{key}=", value if respond_to? "#{key}=" }
  save
end