Posts

Showing posts with the label module

Using the Python Requests module to POST documents to Solr

Using the Python Requests module to POST documents to Solr Method Definition The first method accepts the Solr host URL and the JSON payload.  The auto-commit feature is turned off.  The second method will not POST any data, but it will commit any pending transactions. def post (host, data): headers = { "content-type" : "application/json" } params = { "commit" : "false" } return requests . post(host, data = data, params = params, headers = headers) def commit (host): headers = { "content-type" : "application/json" } params = { "commit" : "true" } return requests . post(host, params = params, headers = headers) Method Invocation The most important part of the method invocation is construction of the JSON payload: payload = { "add" : { "doc" : str (data) } } The payload has three aspects: The add  command tells Solr that a Create or Update is going to be perf...

Using module eval to Define Instance Methods in a Ruby Gem to Enable Per Model Configuration

Using module eval to Define Instance Methods in a Ruby Gem to Enable Per Model Configuration In the last post, I mentioned that I wrote a small gem to post changes to ActiveRecord models to Twitter. In that version, the configuration was handled by a YAML file. I wanted to evolve the gem so that developers could switch Twitter accounts for each model. Basically, I wanted to able to the use the following: class Place { :username => alastrina_gem, :password => QQQQQQ } end After a bit of tinkering and searching, I decided to use the following code in my gem. ALASTRINA_CONFIGURATION_FILE = config/alastrina.yml module Alastrina def self.included(base) base.extend(ClassMethods) end module ClassMethods def alastrina hash module_eval do def configuration throw "Missing #{ALASTRINA_CONFIGURATION_FILE}" unless File.exists? ALASTRINA_CONFIGURATION_FILE @config ||= YAML::load(File.read(ALASTRINA_CONFIGURATION_FILE)) end if hash[:twitter] def send_to_twitter? true end eval...