Skip to content

Instantly share code, notes, and snippets.

@michaeltwofish
Created November 10, 2011 01:00
Show Gist options
  • Save michaeltwofish/1353744 to your computer and use it in GitHub Desktop.
Save michaeltwofish/1353744 to your computer and use it in GitHub Desktop.
module Lithium
%w[rubygems net/http uri json].each {|lib| require lib}
def self.call(params)
endpoint = "http://local-oars/connection/rpc"
query = URI.escape(params.map {|k,v| "#{k}=#{v}"}.join('&'))
uri = URI.parse(endpoint + '?' + query)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header({"User-Agent" => "cucumber-lithium"})
response = http.request(request)
response.body == 'null' ? nil : JSON.parse(response.body)
end
def self.instantiate_models
models = call({:method => '\lithium\core\Libraries::locate', :params => 'models'})
models.each do |name|
# Prefix with 'Lithium' to avoid collisions.
# This should be possible through module namespaces
#Object.const_set(model.split('\\').last, Module.new).extend(LithiumModel)
# @todo Hold the namespace too
model = Lithium.const_set("{name.split('\\').last}", Class.new).extend(LithiumModel)
model.model = name
end
end
module LithiumModel
attr_accessor :model
def method_missing(sym, *args, &block)
puts "calling #{sym}"
Lithium::call({:method => "#{self.model}::#{sym}", :params => args})
end
end
instantiate_models
# Ask lithium for all the models
# Create all the models < LithiumModel
end
# Can now call something like Lithium.first
Lithium::Client.first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment