adamscott adamscott - 7 months ago 104
Ruby Question

Understanding an HTTParty configuration

In "Learn Ruby the Hard Way", the author advises to go through someones code and try to make sense of it. They also advise that this will be difficult, but to write down questions you are having about the code. The only problem is, I do not know how to get the answers to the questions I have after Googling, so I figure I will try to post them here and see if someone can help me out? Let me know If this is not appropriate and I will try to find another option.

require 'httparty'

class BaseWebService
include HTTParty
format :json
base_uri URL_to_mock_services
default_options.update(verify: false)

def self.get_data(extension = {}, url = {}, body = {}, query = {})
url_string = combine_extension_and_url(extension, url)
do_get = -> (req_url, req_body, req_query) { get(req_url, body: req_body.class == String ? JSON.parse(req_body) : req_body, query: req_query) }
return_response(url_string, body.to_json, query, &do_get)
end


In the code above the author is setting up a request to 'Mock Services'. The author is using HTTParty to make the requests to the services. I have some questions on the following:


  • Why is it that if we require HTTParty in our .rb file that we then
    have to include it within the class? Is it not available by default
    in the entire class?


    • format :json
      : Is this going to format any response we get back from the call as a JSON object?

    • base_uri
      : Is this automatically called each time we create a new instance of the BaseWebService class? If not, how is it triggered?

    • default_options
      : I see in the HTTParty documentation that states


      Returns the value of attribute default_options


      but I am not sure what that means. Nor what it is used for in this class?

    • What does the syntax
      do_get = ->
      do?

    • In the class method
      get_data
      the author makes use of
      req_url
      ,
      req_body
      and
      req_query
      , but I cannot locate these as variables or methods anywhere in the code base. Do you know how these are being used?



Answer

You're my favorite golfer so I'm going to help you out here...

Why is it that if we require HTTParty in our .rb file that we then have to include it within the class? Is it not available by default in the entire class?

The require keyword is Ruby's way of looking for a file with that name and loading it.

Once required, you now have the ability to access the classes and modules from that file/files, for example, HTTParty.

include does something very different here. You can see in HTTParty that it is actually a module and when you include 'AModule' on a class (BaseWebService in your code) you're giving instances of your class access to that module's methods. That means a method the HTTParty module implements, such as get, is now available in your BaseWebService class via BaseWebService.new.get.

format :json, is this going to format any response we get back from the call as a JSON object?

He's simply overriding the class method format (see HTTParty line #50).

base_uri - Is this automatically called each time we create a new instance of the BaseWebService class? If not, how is it triggered? default_options - I see in the HTTParty docs this states Returns the value of attribute default_options, but I am not sure what that means. Nor what it is used for in this class?

When you call most HTTParty methods, it will look to see if a base_uri is set up, and since you're overriding it in your class, it will use that value.

What does the syntax do_get = -> do?

do_get = -> is an assignment, where a new variable do_get is assigned a lambda. -> is short hand for a lambda.

In the class method get_data the author makes use of the following (req_url, req_body, req_query), but I cannot locate these as variables or methods anywhere in the code base? Do you know how these are being used?

Those variables are assigned values when the block is yielded to.