forked from ehrenmurdick/gnap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ehren Murdick
committed
Apr 29, 2009
1 parent
3d1b1ff
commit 5c78209
Showing
7 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Gnap | ||
class Notification | ||
# TODO | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |