Hi there! We've prepared the following code samples to get an idea of how you think about writing and maintaining code. Please take a look at the following code, and answer the corresponding questions. If you have suggestions about how the code could be improved, feel free to share those as well.
We're interested in how you communicate with team members of varying technical background, so stick to plain English whenever possible, but if you feel it's necessary to get technical, please do. Do your best, but don't spend more than one hour on this. All code samples here are written in Rails-idiomatic Ruby.
Good luck! Email your responses to tech at wearehearken dot com
.
def is_blocked?(request)
user_agent = request.user_agent
fragments = [
'SEOkicks-Robot',
'SemrushBot',
]
fragments.any? { |fragment| user_agent.include?(fragment) }
end
- What does the above method do and why would you need it?
def check_field(name:)
encrypted_name = "encrypted_#{name}".to_sym
decrypted = decrypt_string self.send(encrypted_name)
clear_text = self.send(name)
if decrypted.to_s == clear_text.to_s
true
else
puts "#{self.id}##{name}:"
puts " crypt: [#{decrypted}]"
puts " clear: [#{clear_text}]"
false
end
end
- What does the above method do and under what circumstances would you need it?
def self.from_hash(hash)
h = hash.with_indifferent_access
CustomField.new(
name: h[:name].to_s,
type: h[:type]||DEFAULT_TYPE,
value: h[:value],
required: h[:required].present?,
)
end
- What is the purpose of calling
.with_indifferent_access
? - What assumptions are made about the incoming values? What happens if these assumptions are incorrect?
- What is the purpose of calling
.present?
on the:required
member of the hash?
def update_configuration(options)
options.each_pair do |key, value|
begin
configuration.set key, value
rescue Excon::Errors::SocketError => e
self.errors[:base] << "error uploading an image for #{key}"
end
end
end
- Can you propose an explanation for the error handling code in the middle of the above method? How do you think it came to be?
def self.organizations_selector_to_ids(selector)
included, excluded = [:include, :exclude].map(&selector.symbolize_keys).map { |a| a.to_a.map(&:to_i) }
enabled_org_ids = Organization.enabled.pluck(:id)
if included.present?
enabled_org_ids & included
else
enabled_org_ids - excluded
end
end
- Describe the behavior of the above method, given that
Organization
is an ActiveRecord model.