Contract Work

Monday, February 3, 2014

Getting the Information

In order to actually get the information on the action campaigns that I needed, I needed to use nokogiri to parse the information, create new actions and clients, and then map that information. I started with this code:
# def self.fetch
   #Nokogiri::XML(SalsaLabs::Client.new.request_data('/api/getObjects.sjs', object: 'Action')).css('item').map do |attributes|
   #SalsaLabs::Action.new({title: atributes.css('Title').text)})
   #end
#end
And then used that code to break it into smaller, more transparent methods. I broke the code into five separate methods. First, I created a new class for it called ActionFetch. Then I initialized (or set up) that class setting an instance variable for api_client
def initialize(api_client)
  @api_client = api_client
end
Then, starting from the meat of the class, I created this method:
def data_request
  return @data if @data 
  body = @api_client.fetch('/api/getObjects.sjs', object: 'Action') 
  doc = Nokogiri::XML(body) 
  @data = doc.css('item') 
  @data
end
The data request method starts by saying if there is anything already in the @data instance variable, then just return, if not, then go through the rest of the method’s steps. This is also called Memoizing which basically means storing a value for future use so that you don’t have to run the call over again which saves you time.

The next line says that take the api client (which is the actual http request, set up with faraday, using credentials, etc., set up in a different api client class) and get the stuff that is located at that api endpoint with the params of the object which is named Action. Then, use nokogiri to parse the information that is in the body string (set above). Finally, look at the doc (which is the result of the nokogiri parsing and is now an xml doc) and use the css selector to find all the items. Finally, return the value.

Then, I took those results and fed them into the get_results method.
def get_results
  data_request.map do |attribute_set| 
    new_action(extract_attributes(attribute_set)) 
  end
end
This method takes the results from data_request and for each one (each attribute set), it extracts them into a hash and then makes each of them a new action (it wraps each of the attributes sets in an action class). It does this by using the extract_attributes method which looks like this:
def extract_attributes(action_data)
  action_data.children.inject({}) do |memo, node|
    memo[node.name.downcase] = node.text 
    memo
  end
end
and takes each of the item nodes (action_data is the Nokogiri item tag) and makes them into a hash. And finally, wrapping it in an action class is done by using the new_action method which looks like this:
def new_action(params)
  SalsaLabs::Action.new(params)
end
It just stick each of the params into an object called action which gives the attributes pretty names.
Phew!

Okay, so then, what do I mean by pretty names? Well, in order to see that, I look at the Action class which I’ll cover in the next post.

No comments:

Post a Comment