Class: Preact::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/preact/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults = {}) ⇒ Configuration

Returns a new instance of Configuration.



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
# File 'lib/preact/configuration.rb', line 32

def initialize(defaults={})
  @scheme = 'https'
  @host = 'api.preact.io'
  @base_path = '/api/v2'

  @autolog = false
  @autolog_ignored_actions = []
  @disabled = false
  @person_builder = nil

  @logging_mode = nil
  @sidekiq_queue = :default
  @request_timeout = 5

  @inject_javascript = false

  @current_user_getter = :current_user
  @current_account_getter = nil
  
  @user_agent = "ruby-preact:#{Preact::VERSION}"

  if defaults && defaults.is_a?(Hash)
    defaults.each do |k,v|
      instance_variable_set("@#{k}", v) unless v.nil?
    end
  end
end

Instance Attribute Details

#account_builderObject

Returns the value of attribute account_builder.



13
14
15
# File 'lib/preact/configuration.rb', line 13

def 
  @account_builder
end

#autologObject

Returns the value of attribute autolog.



14
15
16
# File 'lib/preact/configuration.rb', line 14

def autolog
  @autolog
end

#autolog_ignored_actionsObject

Returns the value of attribute autolog_ignored_actions.



15
16
17
# File 'lib/preact/configuration.rb', line 15

def autolog_ignored_actions
  @autolog_ignored_actions
end

#base_pathObject

Returns the value of attribute base_path.



30
31
32
# File 'lib/preact/configuration.rb', line 30

def base_path
  @base_path
end

#codeObject

Preact credentials



7
8
9
# File 'lib/preact/configuration.rb', line 7

def code
  @code
end

#current_account_getterObject

Returns the value of attribute current_account_getter.



22
23
24
# File 'lib/preact/configuration.rb', line 22

def 
  @current_account_getter
end

#current_user_getterObject

Returns the value of attribute current_user_getter.



21
22
23
# File 'lib/preact/configuration.rb', line 21

def current_user_getter
  @current_user_getter
end

#disabledObject

Default option settings



11
12
13
# File 'lib/preact/configuration.rb', line 11

def disabled
  @disabled
end

#hostObject

Returns the value of attribute host.



29
30
31
# File 'lib/preact/configuration.rb', line 29

def host
  @host
end

#inject_javascriptObject

Returns the value of attribute inject_javascript.



19
20
21
# File 'lib/preact/configuration.rb', line 19

def inject_javascript
  @inject_javascript
end

#loggerObject

Logger settings



25
26
27
# File 'lib/preact/configuration.rb', line 25

def logger
  @logger
end

#logging_modeObject

Returns the value of attribute logging_mode.



18
19
20
# File 'lib/preact/configuration.rb', line 18

def logging_mode
  @logging_mode
end

#person_builderObject

Returns the value of attribute person_builder.



12
13
14
# File 'lib/preact/configuration.rb', line 12

def person_builder
  @person_builder
end

#request_timeoutObject

Returns the value of attribute request_timeout.



17
18
19
# File 'lib/preact/configuration.rb', line 17

def request_timeout
  @request_timeout
end

#schemeObject

The URL of the API server



28
29
30
# File 'lib/preact/configuration.rb', line 28

def scheme
  @scheme
end

#secretObject

Returns the value of attribute secret.



8
9
10
# File 'lib/preact/configuration.rb', line 8

def secret
  @secret
end

#sidekiq_queueObject

Returns the value of attribute sidekiq_queue.



16
17
18
# File 'lib/preact/configuration.rb', line 16

def sidekiq_queue
  @sidekiq_queue
end

Instance Method Details

#autolog_enabled?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/preact/configuration.rb', line 85

def autolog_enabled?
  autolog == true
end

#autolog_should_log?(controller, action) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/preact/configuration.rb', line 89

def autolog_should_log?(controller, action)
  # check to see if we're ignoring this action
  if autolog_ignored_actions && autolog_ignored_actions.is_a?(Array)

    # check to see if we've ignored this specific action
    return false if autolog_ignored_actions.include?("#{controller}##{action}")

    # check to see if we've ignored all actions from this controller
    return false if autolog_ignored_actions.include?("#{controller}#*")

  end

  true
end

#base_uriObject



77
78
79
# File 'lib/preact/configuration.rb', line 77

def base_uri
  "#{scheme}://#{code}:#{secret}@#{host}#{base_path}"
end

#convert_to_account(account) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/preact/configuration.rb', line 146

def ()
  return nil if .nil?
  
  if 
    if .respond_to?(:call)
      hash = .call()
    else
      raise "account_builder must be callable"
    end
  elsif .respond_to?(:to_preact)
    hash = .to_preact
  elsif .is_a? Hash
    hash = 
  else
    hash = ()
  end

  hash
end

#convert_to_person(user) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/preact/configuration.rb', line 126

def convert_to_person(user)
  return nil if user.nil?

  if person_builder
    if person_builder.respond_to?(:call)
      hash = person_builder.call(user)
    else
      raise "person_builder must be callable"
    end
  elsif user.respond_to?(:to_preact)
    hash = user.to_preact
  elsif user.is_a? Hash
    hash = user
  else
    hash = default_user_to_preact_hash(user)
  end

  hash
end

#disabled?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/preact/configuration.rb', line 69

def disabled?
  disabled == true
end

#enabled?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/preact/configuration.rb', line 65

def enabled?
  !disabled
end

#get_current_account(target) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/preact/configuration.rb', line 115

def (target)
  return nil if current_user_getter.nil?

  if .to_s.starts_with?("@")
    # instance var
    target.instance_variable_get() rescue nil
  else
    target.send() rescue nil
  end
end

#get_current_user(target) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/preact/configuration.rb', line 104

def get_current_user(target)
  return nil if current_user_getter.nil?

  if current_user_getter.to_s.starts_with?("@")
    # instance var
    target.instance_variable_get(current_user_getter) rescue nil
  else
    target.send(current_user_getter) rescue nil
  end
end

#inject_javascript?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/preact/configuration.rb', line 81

def inject_javascript?
  inject_javascript == true
end

#prepare_account_hash(account) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/preact/configuration.rb', line 187

def ()
  # id for account should actually be passed as external_identifier
  # make that correction here before sending (LEGACY SUPPORT)
  external_id = [:external_identifier] || ["external_identifier"]
  if  = [:id] || ["id"]
    if external_id.nil?
      [:external_identifier] = 
      .delete(:id)
      .delete("id")
    end
  end

  
end

#prepare_event_hash(event) ⇒ Object



202
203
204
205
# File 'lib/preact/configuration.rb', line 202

def prepare_event_hash(event)
  event[:source] = Preact.configuration.user_agent
  event
end

#prepare_person_hash(person) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/preact/configuration.rb', line 166

def prepare_person_hash(person)
  return nil if person.nil?

  if external_id = person[:external_identifier] || person["external_identifier"]
    person[:uid] ||= external_id
    person.delete(:external_identifier)
    person.delete("external_identifier")
  end

  if created_at = person[:created_at] || person["created_at"]
    if created_at.respond_to?(:to_i)
      created_at = created_at.to_i
    end

    person[:created_at] = created_at
    person.delete("created_at")
  end

  person
end

#user_agentObject



73
74
75
# File 'lib/preact/configuration.rb', line 73

def user_agent
  @user_agent
end

#valid?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/preact/configuration.rb', line 60

def valid?
  # we require both the API keys
  code && secret
end