Skip to content

Commit

Permalink
many many updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehren Murdick committed Apr 29, 2009
1 parent 3d1b1ff commit 5c78209
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 6 deletions.
7 changes: 5 additions & 2 deletions gnap.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ Gem::Specification.new do |s|
s.files = Dir.glob("lib/**/*.rb")
s.require_paths = ["lib"]
s.summary = "Gnap is a interface to the Gnip api"
if s.respond_to? :specification_version then
if s.respond_to? :specification_version
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 2

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0')
s.add_runtime_dependency(%q<activesupport>, [">= 2.3.2"])
s.add_runtime_dependency(%q<builder>, [">= 2.1.2"])
else
s.add_dependency(%q<activesupport>, [">= 2.3.2"])
s.add_dependency(%q<builder>, [">= 2.1.2"])
end
else
s.add_dependency(%q<activesupport>, [">= 2.3.2"])
s.add_dependency(%q<builder>, [">= 2.1.2"])
end
end
1 change: 1 addition & 0 deletions lib/activesupport/time_formats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS[:gnap] = "%Y%m%d%H%M"
9 changes: 9 additions & 0 deletions lib/gnap.rb
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
require 'activesupport'
require 'base64'
require 'net/http'
require 'net/https'
require 'builder'
require File.dirname(__FILE__) + "/activesupport/time_formats"
require File.dirname(__FILE__) + "/gnap/connection"
require File.dirname(__FILE__) + "/gnap/publisher"
require File.dirname(__FILE__) + "/gnap/notification"
require File.dirname(__FILE__) + "/gnap/filter"
36 changes: 32 additions & 4 deletions lib/gnap/connection.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
require 'net/http'
require 'net/https'

module Gnap
class Connection
include Net

attr_writer :publisher

def initialize config
p 'ballz'
config = config.symbolize_keys
def config.inspect; "*SNIP*"; end
@http = HTTP.new(config[:uri] || "api-v21.gnip.com", 443)
@http.use_ssl = true
@config = config
end

def publisher name
Publisher.new(@config, name)
end

private
def prepare_request(type, path)
returning type.new(path) do |req|
req.basic_auth(@config[:username], @config[:password])
end
end

def get(path)
req = prepare_request(HTTP::Get, path)
@http.request(req).body
end

def post(path, data)
req = prepare_request(HTTP::Post, path)
req.body = data
req.content_type = "application/xml"
@http.request(req).body
end
end
end
56 changes: 56 additions & 0 deletions lib/gnap/filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Gnap
class Filter < Publisher
def initialize config, publisher, name, rules = nil
@publisher = publisher
@filter = name
@rules = rules.try(:symbolize_keys)
super config, publisher
end

def add_rules! rules
doc = Builder::XmlMarkup.new
doc.instruct!
doc.rules {
rules_to_xml(doc, rules)
}
post(rules_path, doc.target!)
end

def add_rule! type, value
add_rules!({type => value})
end

def to_xml
return unless @rules
fullData = @rules.delete(:fullData)
postURL = @rules.delete(:postURL)
doc = Builder::XmlMarkup.new
doc.instruct!
doc.filter(:name => @filter, :fullData => fullData || false) {
if postURL
doc.postURL(postURL)
end
rules_to_xml(doc, @rules)
}
doc.target!
end

def path
"#{super}/filters/#{@filter}"
end

def rules_path
"#{path}/rules.xml"
end

private
def rules_to_xml doc, rules
rules.keys.each do |key|
[rules[key]].flatten.each do |value|
doc.rule(value, :type => key)
end
end
end
end

end
5 changes: 5 additions & 0 deletions lib/gnap/notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Gnap
class Notification
# TODO
end
end
35 changes: 35 additions & 0 deletions lib/gnap/publisher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Gnap
class Publisher < Connection
PATH = "/gnip/publishers/%s"
NOTIFICATION_PATH = "/notification/%s.xml"
NEW_FILTER_PATH = "/filters.xml"

def initialize config, name
@publisher = name.to_s
super config
end

def notification(time = "current")
get(build_notification_path(time))
end

def filter name
Filter.new(@config, @publisher, name)
end

def create_filter! name, rules
data = Filter.new(@config, @publisher, name, rules).to_xml
post(path + NEW_FILTER_PATH % @publisher, data)
end

def path
PATH % @publisher
end

private
def build_notification_path time
time = time.to_s(:gnap) if Time === time
path + NOTIFICATION_PATH % time
end
end
end

0 comments on commit 5c78209

Please sign in to comment.