Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds the multimap gem (epic fail) in order to get proper route create…

…/update actions
  • Loading branch information...
commit 312c6811dddf5fde9110d7f6b9f3397a495ef8d8 1 parent 65b6759
@sgrove sgrove authored
View
1  lib/mailgun.rb
@@ -1,5 +1,6 @@
require "rest-client"
require "json"
+require "multimap"
require "mailgun/mailgun_error"
require "mailgun/base"
View
88 lib/mailgun/route.rb
@@ -6,49 +6,52 @@ def initialize(mailgun)
end
def list(limit=100, skip=0)
- response = Mailgun.submit :get, route_url, :limit => limit, :skip => skip
- if response
- return (response["items"] || [])
- end
+ Mailgun.submit(:get, route_url, :limit => limit, :skip => skip)["items"] || []
end
- def get(route_id)
- response = Mailgun.submit :get, route_url(route_id)
- response["route"] if response
+ def find(route_id)
+ Mailgun.submit(:get, route_url(route_id))["route"]
end
- def create(description, priority, expression, actions)
- params = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }
- params[:description] = description
- params[:priority] = priority
- params[:expression] = expression
-
+ def create(description, priority, filter, actions)
+ data = ::Multimap.new
+
+ data['priority'] = priority
+ data['description'] = description
+ data['expression'] = build_filter(filter)
+
+ actions = build_actions(actions)
+
actions.each do |action|
- params[:action] = action
+ data['action'] = action
end
-
- response = Mailgun.submit :post, route_url, params
- response["route"]["id"] if response
+
+ # TODO: Raise an error or return false if unable to create route
+ Mailgun.submit(:post, route_url, data)["route"]["id"]
end
def update(route_id, params)
- params_to_update = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }
+ data = ::Multimap.new
- params_to_update[:priority] = params[:priority] if params.has_key?(:priority)
- params_to_update[:description] = params[:description] if params.has_key?(:description)
- params_to_update[:expression] = params[:expression] if params.has_key?(:expression)
+ ['priority', 'description'].each do |key|
+ data[key] = params[key] if params.has_key?(key)
+ end
+
+ data['expression'] = build_filter(params['expression']) if params.has_key?('expression')
+
+ if params.has_key?('actions')
+ actions = build_actions(params['actions'])
- if params.has_key?(:action)
- params[:action].each do |action|
- params_to_update[:action] = action
+ actions.each do |action|
+ data['action'] = action
end
end
- Mailgun.submit :put, route_url(route_id), params_to_update
+
+ Mailgun.submit(:put, route_url(route_id), data)
end
def destroy(route_id)
- response = Mailgun.submit :delete, route_url(route_id)
- response["id"] if response
+ Mailgun.submit(:delete, route_url(route_id))["id"]
end
private
@@ -56,5 +59,36 @@ def destroy(route_id)
def route_url(route_id=nil)
"#{@mailgun.base_url}/routes#{'/' + route_id if route_id}"
end
+
+ def build_actions(actions)
+ _actions = []
+
+ actions.each do |action|
+ case action.first.to_sym
+ when :forward
+ _actions << "forward(\"#{action.last}\")"
+ when :stop
+ _actions << "stop()"
+ else
+ raise Mailgun::Error.new("Unsupported action requested, see http://documentation.mailgun.net/user_manual.html#routes for a list of allowed actions")
+ end
+ end
+
+ _actions
+ end
+
+
+ def build_filter(filter)
+ case filter.first.to_sym
+ when :match_recipient
+ return "match_recipient('#{filter.last}')"
+ when :match_header
+ return "match_header('#{filter[1]}', '#{filter.last}')"
+ when :catch_all
+ return "catch_all()"
+ else
+ raise Mailgun::Error.new("Unsupported filter requested, see http://documentation.mailgun.net/user_manual.html#routes for a list of allowed filters")
+ end
+ end
end
end
View
1  mailgun.gemspec
@@ -15,4 +15,5 @@ Gem::Specification.new do |gem|
gem.version = "0.0.3"
gem.add_dependency(%q<rest-client>, [">= 0"])
+ gem.add_dependency(%q<multimap>, [">= 0"])
end
View
27 spec/route_spec.rb
@@ -29,44 +29,39 @@
RestClient.should_receive(:get)
.with("#{@mailgun.routes.send(:route_url, @sample_route_id)}", {})
.and_return("{\"route\": {\"id\": \"#{@sample_route_id}\" }}")
- @mailgun.routes.get @sample_route_id
+ @mailgun.routes.find @sample_route_id
end
end
describe "create route" do
it "should make a POST request with the right params" do
- options = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }
+ options = {}
+
options[:description] = "test_route"
- options[:priority] = 1
- options[:expression] = "match_recipent(\"sample.mailgun.org\")"
- options[:action] = "forward(\"http://test-site.com\")"
-
+ options[:priority] = 1
+ options[:expression] = [:match_recipient, "sample.mailgun.org"]
+ options[:action] = [[:forward, "http://test-site.com"], [:stop]]
+
RestClient.should_receive(:post)
- .with("#{@mailgun.routes.send(:route_url)}", {
- :description => options[:description],
- :priority => options[:priority],
- :expression => options[:expression],
- :action => options[:action]
- })
+ .with(@mailgun.routes.send(:route_url), instance_of(Multimap))
.and_return("{\"route\": {\"id\": \"@sample_route_id\"}}")
@mailgun.routes.create(
options[:description],
options[:priority],
options[:expression],
- [options[:action]]
+ options[:action],
)
end
end
describe "update route" do
it "should make a PUT request with the right params" do
-
- options = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }
+ options = {}
options[:description] = "test_route"
RestClient.should_receive(:put)
- .with("#{@mailgun.routes.send(:route_url, @sample_route_id)}", options)
+ .with("#{@mailgun.routes.send(:route_url, @sample_route_id)}", instance_of(Multimap))
.and_return("{\"id\": \"#{@sample_route_id}\"}")
@mailgun.routes.update @sample_route_id, options
end
Please sign in to comment.
Something went wrong with that request. Please try again.