Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit dcb4b09676f165984baecd5aa23b51e1dc6cdc3c 0 parents
@larsburgess larsburgess authored
2  .gitignore
@@ -0,0 +1,2 @@
+coverage
+.DS_Store
19 Rakefile
@@ -0,0 +1,19 @@
+begin
+ require 'vendor/rhosync/lib/rhosync/tasks'
+ require 'vendor/rhosync/lib/rhosync'
+rescue LoadError
+ require 'rhosync/tasks'
+ require 'rhosync'
+end
+
+ROOT_PATH = File.expand_path(File.dirname(__FILE__))
+
+begin
+ require 'resque/tasks'
+ task "resque:setup" do
+ require 'application'
+ end
+rescue LoadError
+ puts "Resque not available. Install it with: "
+ puts "gem install resque\n\n"
+end
27 application.rb
@@ -0,0 +1,27 @@
+class Application < Rhosync::Base
+ class << self
+ def authenticate(username,password,session)
+ true # do some interesting authentication here...
+ end
+
+ # Add hooks for application startup here
+ # Don't forget to call super at the end!
+ def initializer(path)
+ super
+ end
+
+ # Calling super here returns rack tempfile path:
+ # i.e. /var/folders/J4/J4wGJ-r6H7S313GEZ-Xx5E+++TI
+ # Note: This tempfile is removed when server stops or crashes...
+ # See http://rack.rubyforge.org/doc/Multipart.html for more info
+ #
+ # Override this by creating a copy of the file somewhere
+ # and returning the path to that file (then don't call super!):
+ # i.e. /mnt/myimages/soccer.png
+ def store_blob(blob)
+ super #=> returns blob[:tempfile]
+ end
+ end
+end
+
+Application.initializer(ROOT_PATH)
33 config.ru
@@ -0,0 +1,33 @@
+#!/usr/bin/env ruby
+
+# Try to load vendor-ed rhosync, otherwise load the gem
+begin
+ require 'vendor/rhosync/lib/rhosync/server'
+ require 'vendor/rhosync/lib/rhosync/console/server'
+rescue LoadError
+ require 'rhosync/server'
+ require 'rhosync/console/server'
+end
+
+# By default, turn on the resque web console
+require 'resque/server'
+
+ROOT_PATH = File.expand_path(File.dirname(__FILE__))
+
+# Rhosync server flags
+Rhosync::Server.disable :run
+Rhosync::Server.disable :clean_trace
+Rhosync::Server.enable :raise_errors
+Rhosync::Server.set :environment, :development
+Rhosync::Server.set :secret, '<changeme>'
+Rhosync::Server.set :root, ROOT_PATH
+Rhosync::Server.use Rack::Static, :urls => ["/data"], :root => Rhosync::Server.root
+
+# Load our rhosync application
+require 'application'
+
+# Setup the url map
+run Rack::URLMap.new \
+ "/" => Rhosync::Server.new,
+ "/resque" => Resque::Server.new, # If you don't want resque frontend, disable it here
+ "/console" => RhosyncConsole::Server.new # If you don't want rhosync frontend, disable it here
1  settings/license.key
@@ -0,0 +1 @@
+858fc60fadfde40273d0ac505906969318aa4931d1a2c4aeb24d98393c74379f60e226651601969874257f7f1fbda9b099ecd551a641519aa46819947fda0191
17 settings/settings.yml
@@ -0,0 +1,17 @@
+#Sources
+:sources:
+ Site:
+ :poll_interval: 300
+
+:test:
+ :syncserver: http://localhost:9292/application/
+ :redis: localhost:6379
+ :licensefile: settings/license.key
+:production:
+ :syncserver: http://localhost:9292/application/
+ :redis: localhost:6379
+ :licensefile: settings/license.key
+:development:
+ :syncserver: http://localhost:9292/application/
+ :redis: localhost:6379
+ :licensefile: settings/license.key
81 sources/site.rb
@@ -0,0 +1,81 @@
+require 'open-uri'
+require 'xmlsimple'
+
+class Site < SourceAdapter
+
+ # this is a handy function to extract key-value pairs from a complex nested hash (hash of arrays and hashes such as is returned by
+ # many web services)
+ def extract_keyvalues(v)
+
+ result={}
+ if v.is_a?(Hash)
+
+ v.each do |key,value|
+ if value.is_a?(String)
+ result[key]=value
+ elsif value.is_a?(Array) and value.size==1 and value[0].is_a?(String)
+ result[key]=value[0]
+ else
+ temp=extract_keyvalues(value)
+ temp.keys.each do |x|
+ result[x]=temp[x]
+ end
+ end
+ end
+ elsif v.is_a?(Array)
+ v.each do |item|
+ if item.is_a?(Hash) or item.is_a?(Array)
+ temp=extract_keyvalues(item)
+ temp.keys.each do |x|
+ result[x]=temp[x]
+ end
+ end
+ end
+ end
+ p "Returning result: #{result.inspect.to_s}"
+
+ result
+ end
+
+ def query(conditions=nil)
+ # use web service at http://qwwebservices.usgs.gov/technical-documentation.html#DOMAIN
+ # for example: http://qwwebservices.usgs.gov/Station/search?bBox=-122.1,36.9,-121.9,37.1
+ @radius=conditions[:radius] if conditions
+ @radius||=0.1
+ @lat=conditions[:lat] if conditions
+ @lat||=37.33
+ @long=conditions[:long] if conditions
+ @long||=-122.04
+
+ base_url="http://qwwebservices.usgs.gov/Station/search"
+ p "Base URL #{base_url}"
+ url=base_url+"?bBox=#{@long-@radius},#{@lat-@radius},#{@long+@radius},#{@lat+@radius}"
+ puts "Opening #{url}"
+ begin
+ response=open(url)
+ rescue Exception=>e
+ puts "Error opening: e.inspect.to_s"
+ end
+ begin
+ xmlresult=XmlSimple.xml_in(response.read)
+ rescue Exception=>e
+ puts "Error parsing: #{e.inspect.to_s}"
+ end
+
+ @result={}
+ org=xmlresult["Organization"]
+ p "Org: #{org[0].inspect.to_s}"
+ org[0]["MonitoringLocation"].each do |loc|
+ begin
+ puts "Site: #{loc.inspect.to_s}"
+ puts "Site name: #{loc['MonitoringLocationIdentity'][0]['MonitoringLocationIdentifier']}"
+ @result[loc['MonitoringLocationIdentity'][0]['MonitoringLocationIdentifier'][0]]=extract_keyvalues(loc)
+ rescue
+ puts "Failure to access site"
+ end
+ end
+ p "Final result: #{@result.inspect.to_s}"
+
+ @result
+ end
+end
25 spec/sources/site_spec.rb
@@ -0,0 +1,25 @@
+require File.join(File.dirname(__FILE__),'..','spec_helper')
+
+describe "Site" do
+ it_should_behave_like "SpecHelper"
+
+ before(:each) do
+ setup_test_for Site,'testuser'
+ end
+
+ it "should process Site query" do
+ test_query.size.should > 0
+ end
+
+ it "should process Site create" do
+ pending
+ end
+
+ it "should process Site update" do
+ pending
+ end
+
+ it "should process Site delete" do
+ pending
+ end
+end
27 spec/spec_helper.rb
@@ -0,0 +1,27 @@
+require 'rubygems'
+
+# Set environment to test
+ENV['RHO_ENV'] = 'test'
+ROOT_PATH = ENV['ROOT_PATH']
+
+# Try to load vendor-ed rhosync, otherwise load the gem
+begin
+ require 'vendor/rhosync/lib/rhosync'
+rescue LoadError
+ require 'rhosync'
+end
+
+# Load our rhosync application
+require 'application'
+include Rhosync
+
+require 'rhosync/test_methods'
+
+describe "SpecHelper", :shared => true do
+ include Rhosync::TestMethods
+
+ before(:each) do
+ Store.db.flushdb
+ Application.initializer(ROOT_PATH)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.