Permalink
Browse files

Initial Commit

  • Loading branch information...
1 parent 0feacb2 commit eb6d96295f3e6e3baf0702ab30684c3cbe0c3e58 @jonbuda committed Mar 2, 2010
Showing with 103 additions and 5 deletions.
  1. +2 −2 Rakefile
  2. +57 −0 lib/ymlp.rb
  3. +1 −0 spec/fixtures/ping.json
  4. +30 −0 spec/spec_helper.rb
  5. +13 −3 spec/ymlp_spec.rb
View
@@ -5,8 +5,8 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "ymlp"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{Ruby library for the YMLP (YourMailingListProvider) API}
+ gem.description = %Q{Ruby library for the YMLP (YourMailingListProvider) API}
gem.email = "jon.buda@gmail.com"
gem.homepage = "http://github.com/jonbuda/ymlp"
gem.authors = ["Jon Buda"]
View
@@ -0,0 +1,57 @@
+require 'rubygems'
+require 'httparty'
+
+class YMLP
+ include HTTParty
+ base_uri 'https://www.ymlp.com/api'
+
+ def initialize(username, key, format = :json)
+ @base_options = {
+ 'Username' => username,
+ 'Key' => key,
+ 'Output' => format.to_s.upcase
+ }
+
+ self.class.format format
+ end
+
+ ["Ping",
+ "Contacts.Add",
+ "Contacts.Delete",
+ "Contacts.Unsubscribe",
+ "Contacts.GetContact",
+ "Contacts.GetList",
+ "Contacts.GetUnsubscribed",
+ "Contacts.GetDeleted",
+ "Contacts.GetBounced",
+ "Groups.GetList",
+ "Groups.Add",
+ "Groups.Delete",
+ "Groups.Update",
+ "Groups.Empty",
+ "Fields.GetList",
+ "Fields.Add",
+ "Fields.Delete",
+ "Fields.Update"].each do |method_name|
+ new_method = <<-method
+ def #{method_name.downcase.gsub('.', '_')}(query = {})
+ request("/#{method_name}", query)
+ end
+ method
+
+ class_eval( new_method, __FILE__, __LINE__)
+ end
+
+ private
+
+ def request(command, query = {}, method = :get)
+ result = self.class.post(command, :query => @base_options.merge(query))
+
+ if result.is_a?(Hash) && result.has_key?('Result')
+ raise(result['Result']['Output']) unless result['Result']['Code'] == 0
+ end
+
+ result
+ end
+
+end
View
@@ -0,0 +1 @@
+{"Code":"0","Output":"Hello!"}
View
@@ -1,8 +1,38 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+gem 'fakeweb', ">= 1.2.8"
+
require 'ymlp'
require 'spec'
require 'spec/autorun'
+require 'fakeweb'
+
+FakeWeb.allow_net_connect = false
+
+def fixture_file(filename)
+ return '' if filename == ''
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
+ File.read(file_path)
+end
+
+def ymlp_base_url(url = "/")
+ url = "https://www.ymlp.com/api#{url}"
+ Regexp.new(Regexp.escape(url))
+end
+
+def stub_get(url, filename)
+ # FIXME: We have to specify content type, otherwise HTTParty will not parse the
+ # body correctly. Is there any way we can get around this? Or is this a limitation
+ # of using FakeWeb?
+ options = { :body => fixture_file(filename), :content_type => 'application/json' }
+ FakeWeb.register_uri(:get, ymlp_base_url(url), options)
+end
+
+def stub_post(url, filename)
+ FakeWeb.register_uri(:post, ymlp_base_url(url), :body => fixture_file(filename), :content_type => 'application/json')
+end
+
Spec::Runner.configure do |config|
View
@@ -1,7 +1,17 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-describe "Ymlp" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+describe "YMLP" do
+ before(:each) do
+ @ymlp = YMLP.new('login', 'API KEY')
end
+
+ context "Ping" do
+ it "should have a successful response" do
+ stub_post('/Ping', 'ping.json')
+ @ymlp.ping['Output'].should == "Hello!"
+ end
+ end
+
+
+
end

0 comments on commit eb6d962

Please sign in to comment.