Skip to content

Instantly share code, notes, and snippets.

@kavyasukumar
Created August 9, 2018 19:46
Show Gist options
  • Save kavyasukumar/7a25b5443cf266dadd4037e1cf969335 to your computer and use it in GitHub Desktop.
Save kavyasukumar/7a25b5443cf266dadd4037e1cf969335 to your computer and use it in GitHub Desktop.

Sample 1

Assumptions

The request parameter passed to the method is a standard Rails request object.

1. What does this method do?

This method checks if the request came from a list of known bots. It returns true if the user agent -- the part of request that describes the kind of browser or "agent" that was used to make the request -- contains any of the bot identifier words defined in code. In this case it specifically looks for SEOkicks or Semrush bots.

From the name of the method, I assume that the method is used in context with blocking a request from a bot. I had to look up the specific bots mentioned here. They look like web crawlers that index backlink information.

There are many reasons one would want to block crawlers including preventing unnecessary traffic and spamming on the site, preventing indexing of duplicate content, and prevent scraping data from your site.

Improvements

I would change the name of the method. This method only checks the user agent. It does not explicitly check if the request is blocked or block the request. The name implies otherwise.

I would change it to known_bot?. This is more inline with the recommended RuboCop style guide.

As an added improvement, it might be worth moving the list of fragments to a configuration option. This again depends on the use case. If this list changes frequently, it might be worth moving it out of the code into config or a Rails environment variable.

Sample 2

2. What does this method do?

This method is used for validating data consistency. It checks if the encrypted version of a field, specified by name parameter, matches its clear text version.

This is useful in scenarios where the encrypted data could accidentally differ from clear text. This can happen when the encryption key changes or during data migrations.

Depending on the program and cost of real-time encryption, encryption could be a scheduled service. And in rare cases, they could be parts of the program modifies one version and does not modify the other. (This can be avoided with imposing constraints.)

Improvements

  1. It would be good to check if the field exists before calling it. Return false or raise an error as appropriate if self.has_attribute? encrypted_name returns false. This will prevent accidental calling of methods. eg: check_field(name: 'delete!')
  2. Depending on the nature of data, you may not want to log the clear text version of the field in the console.
  3. Rename the function to ruby naming conventions to end with ? since it is a predicate function - i.e. returns true or false.

Sample 3

  1. What is the purpose of calling .with_indifferent_access? .with_indifferent_access allows a hash to be accessed using either symbols or string keys. i.e. For a hash with indifferent access h, h['key'] and h[:key] references the same object in the hash.

The incoming hash can have either string keys or symbol keys.

  1. What assumptions are made about the incoming values? What happens if these assumptions are incorrect?
  • The first assumption is that the incoming parameter is a hash. If that is not true, the method will throw an error.
  • The hash contents are assumed to be valid. The name could be an empty string if the value is missing or is a different type such as a hash. type may not be a valid type. type and value may not match each other.

What happens depends on the validation and initializer for CustomField and the storage backend. If there are validations on the filed itself, this will throw invalid field errors. If the customField is stored as a serialized hash with no checks, this may propagate errors downstream and cause the object's views to be unusable or cause runtime errors.

  1. What is the purpose of calling .present? on the :required member of the hash? This avoids the dreaded three-value boolean problem and makes sure the value is always either true or false.

When data comes in from a check box on a form, for instance, :required is missing if it is unchecked. It should be considered equivalent to false. .

The problem with storing a null in a boolean field is it confuses logic operations. Eg: true && nil is nil not false.

If the field was missing from the hash and the code directly assigned required: h[:required], the value stored will be a null. Adding a .present? sets a false value if the value is missing.

Sample 4

  1. 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?

This one really stumped me. I had to google quite a bit. And the most likely explanation I can come up with is that the configuration is stored on a cloud backend or on a server and it uploads something like a profile image perhaps. I am guessing this because the code carries on despite the error. So the upload must not be mission critical.

For sake of simplicity, I am going to assume that the upload is to AWS. This can easily be replaced by any other backend and could cause analogous errors.

I remember there being a point of time where the latest openSSL version on MacOS was incompatible with AWS S3 uploads. (I have also experienced this while writing to google docs from Rails backend.) It could also have been from when AWS suffered an outage. And the last option I can think of is a permission issue. Especially if the upload uses a user's permission.

Sample 5

7 . Describe the behavior of the above method, given that Organization is an ActiveRecord model.

I don't quite understand what format selector needs to takes to makes this valid map(&selector.symbolize_keys).

Roughly what this code does is, it allows passing in Procs as field selectors. It then maps those to organization ids. The procs themselves can be passed in as a include condition or an exclude condition. This then returns a list of ids of ActiveRecord Organization that satisfy the criteria and are enabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment