Skip to content

Commit

Permalink
added location fields to oa, added wiser earth plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Mott committed Aug 26, 2009
1 parent f7d232e commit 86760e2
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/views/actions/index.atom.builder
Expand Up @@ -40,6 +40,14 @@ xml.feed(:xmlns => 'http://www.w3.org/2005/Atom',
action.tags.each do |tag|
xml.category :term => tag
end
unless action.location_city.blank? and action.location_country.blank? and action.location_state.blank? and action.location_postalcode.blank?
xml.oa :location do
xml.oa :city, action.location_city unless action.location_city.blank?
xml.oa :country, action.location_country unless action.location_country.blank?
xml.oa :state, action.location_state unless action.location_state.blank?
xml.oa :postalcode, action.location_postalcode unless action.location_postalcode.blank?
end
end
unless action.platform_name.blank? and action.platform_url.blank? and action.platform_email.blank?
xml.oa :platform do
xml.oa :name, action.platform_name unless action.platform_name.blank?
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/033_add_location_fields_to_actions.rb
@@ -0,0 +1,15 @@
class AddLocationFieldsToActions < ActiveRecord::Migration
def self.up
add_column :actions, :location_city, :string
add_column :actions, :location_country, :string
add_column :actions, :location_state, :string
add_column :actions, :location_postalcode, :string
end

def self.down
remove_column :actions, :location_city
remove_column :actions, :location_country
remove_column :actions, :location_state
remove_column :actions, :location_postalcode
end
end
6 changes: 5 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 32) do
ActiveRecord::Schema.define(:version => 33) do

create_table "action_sources", :force => true do |t|
t.string "name"
Expand Down Expand Up @@ -68,6 +68,10 @@
t.text "tags"
t.integer "redirect_id"
t.integer "hit_count", :default => 0
t.string "location_city"
t.string "location_country"
t.string "location_state"
t.string "location_postalcode"
end

add_index "actions", ["id"], :name => "index_actions_on_id", :unique => true
Expand Down
7 changes: 7 additions & 0 deletions lib/action_source_plugins/feed.rb
Expand Up @@ -59,6 +59,13 @@ def populate_action(entry)
t.scheme == 'http://socialactions.com/action_types'
}.map{|t| t.term}
end

if entry.oa_location
action.location_city = entry.oa_location.oa_city
action.location_country = entry.oa_location.oa_country
action.location_state = entry.oa_location.oa_state
action.location_postalcode = entry.oa_location.oa_postalcode
end

if entry.oa_platform
action.platform_name = entry.oa_platform.oa_name
Expand Down
146 changes: 146 additions & 0 deletions lib/action_source_plugins/wiser_earth_feed.rb
@@ -0,0 +1,146 @@
require 'xml'
require 'digest/md5'
require 'open-uri'

module WiserEarthFeed

MAIN_URL = "http://www.wiserearth.org"

def parse
case api_data_type
when "events"
parse_items doc_for_xml(feed), 'event'
when "jobs"
parse_items doc_for_xml(feed), 'job'
when "groups"
parse_items doc_for_xml(feed), 'group'
end
#update_attribute(:needs_updating, false)
end

def doc_for_xml(xml)
parser, parser.string = XML::Parser.new, xml
doc, statuses = parser.parse, []
doc
end

def parse_items(doc,node)
doc.find("//rsp/results/#{node}").each do |entry|
node = entry
if api_data_type != 'groups'
entry_doc = doc_for_xml feed_entry(entry.attributes.to_h['id'])
node = entry_doc.find("//rsp//#{node}").first
end
populate_action(node)
end
end

def feed
open("#{self.url}&sig=#{api_signature}&key=#{api_key}").read # http://www.wiserearth.org/events/api_search?r=1&language=EN&sig=<signature>&key=<apiKey>
end

def feed_entry(masterid)
open("#{MAIN_URL}/#{api_data_type}/#{masterid}?sig=#{api_signature_for_id(masterid)}&key=#{api_key}").read
end

def api_key
json_additional_data['key'] || raise("WiserEarth requires an api key!")
end

def api_secret
json_additional_data['secret'] || raise("WiserEarth requires a secret to access data!")
end

def api_data_type
json_additional_data['data_type'] || raise("WiserEarth requires a data type!")
end

def api_signature_for_id(id)
Digest::MD5.hexdigest("masterid#{id}#{api_secret}")
end

def api_signature
Digest::MD5.hexdigest("#{param_key_for_url(self.url)}#{api_secret}")
end

def param_key_for_url(url)
sigStr = url.split("?").last
sigStr.gsub!(/&/,'')
sigStr.gsub!(/=/,'')
end

def populate_action(entry)
attributes = entry.attributes.to_h
action = actions.find_or_create_by_url(attributes['href'])
action.title = attributes['Event'] || attributes['Position'] || attributes['Name']# TODO: handle text vs. html here
action.url = attributes['href']
if api_data_type == 'groups'
action.description = entry.find('About').first.content
else
action.description = entry.find('SpecialTextList/About').first.content
end
#if entry.published_time or action.created_at.blank?
action.created_at = attributes['Created'] if attributes['Created']
#end
action.updated_at = attributes['Updated'] if attributes['Updated']
#figure_out_address_from(entry,action)
#action.organization_ein = entry.cb_ein # "legacy" support for 6deg pre-OA EIN

#unless entry.author_detail.blank?
action.initiator_name = attributes['Contact_name']
action.initiator_email = attributes['Contact_email']
#action.initiator_url = entry.author_detail.url
#end

#action.subtitle = entry.dcterms_alternative
#action.embed_widget = entry.oa_embedwidget

#if entry.oa_goal
#action.goal_completed = entry.oa_goal.oa_completed
#action.goal_amount = entry.oa_goal.oa_amount
#action.goal_type = entry.oa_goal.oa_type
#action.goal_number_of_contributors = entry.oa_goal.oa_numberofcontributors
#end

#action.dcterms_valid = entry.dcterms_valid
#if entry.dcterms_valid and entry.dcterms_valid.match(/(^|;)\s*end=([^;]+)/)
#action.expires_at = $2
#end

#unless entry.tags.blank?
# action_type_category = entry.tags.detect{ |t|
# t.scheme == 'http://socialactions.com/action_types'
# }
# if action_type_category
# action.action_type = ActionType.find_by_name(action_type_category.term)
# end

# action.tags = entry.tags.reject{ |t|
# t.scheme == 'http://socialactions.com/action_types'
# }.map{|t| t.term}
#end

#if entry.oa_location
action.location_city = attributes['City']
action.location_country = attributes['Country']
action.location_state = attributes['State']
action.location_postalcode = attributes['Postal_code'] || attributes['Postal_Code']
#end

#if entry.oa_platform
# action.platform_name = entry.oa_platform.oa_name
# action.platform_url = entry.oa_platform.oa_url
# action.platform_email = entry.oa_platform.oa_email
#end

#if entry.oa_organization
# action.organization_name = entry.oa_organization.oa_name
# action.organization_url = entry.oa_organization.oa_url
# action.organization_email = entry.oa_organization.oa_email
# action.organization_ein = entry.oa_organization.oa_ein
#end

action.save!
end # populate_action

end
60 changes: 60 additions & 0 deletions lib/feedparser_rssa_patch.rb
Expand Up @@ -82,6 +82,16 @@ def _end_oa_platform
@inplatform = false
end

def _start_oa_location(attrsD)
@inlocation = true
push('oa:location', true)
end

def _end_oa_location
pop('oa:location')
@inlocation = false
end

def _start_oa_name(attrsD)
push('oa:name', true)
end
Expand Down Expand Up @@ -133,6 +143,50 @@ def _end_oa_ein
_save_platform('oa_ein', value)
end
end

def _start_oa_city(attrsD)
push('oa:city',true)
end

def _end_oa_city
value = pop('oa:city')
if @inlocation
_save_location('oa_city',value)
end
end

def _start_oa_country(attrsD)
push('oa:country',true)
end

def _end_oa_country
value = pop('oa:country')
if @inlocation
_save_location('oa_country',value)
end
end

def _start_oa_state(attrsD)
push('oa:state',true)
end

def _end_oa_state
value = pop('oa:state')
if @inlocation
_save_location('oa_state',value)
end
end

def _start_oa_postalcode(attrsD)
push('oa:postalcode',true)
end

def _end_oa_postalcode
value = pop('oa:postalcode')
if @inlocation
_save_location('oa_postalcode',value)
end
end

def _save_organization(key, value)
context = getContext()
Expand All @@ -145,5 +199,11 @@ def _save_platform(key, value)
context['oa_platform'] ||= FeedParserDict.new
context['oa_platform'][key] = value
end

def _save_location(key, value)
context = getContext()
context['oa_location'] ||= FeedParserDict.new
context['oa_location'][key] = value
end
end
end

0 comments on commit 86760e2

Please sign in to comment.