sant0sk1-dreamy 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,7 +22,7 @@ To get started with the library:
22
22
  require 'rubygems'
23
23
  require 'dreamy'
24
24
 
25
- account = Dreamy::Control.new(username,api_key)
25
+ account = Dreamy::Base.new(username,api_key)
26
26
 
27
27
  # fetch an array of Dreamy::Domain objects
28
28
  account.domains
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 2
3
3
  :major: 0
4
4
  :minor: 1
@@ -7,13 +7,16 @@ require 'uuid'
7
7
 
8
8
 
9
9
  $:.unshift(File.dirname(__FILE__) + "/dreamy")
10
+ require 'dreamy/core_extensions'
10
11
  require 'dreamy/easy_class_maker'
11
- require 'dreamy/control'
12
+ require 'dreamy/base'
12
13
  require 'dreamy/domain'
13
14
  require 'dreamy/dns'
15
+ require 'dreamy/subscriber'
14
16
  require 'dreamy/user'
15
17
 
16
18
  module Dreamy
17
19
  class CantConnect < StandardError; end
18
20
  class Unavailable < StandardError; end
21
+ class ApiError < StandardError; end
19
22
  end
@@ -1,5 +1,5 @@
1
1
  module Dreamy
2
- class Control
2
+ class Base
3
3
 
4
4
  @@host = "api.dreamhost.com"
5
5
 
@@ -7,13 +7,14 @@ module Dreamy
7
7
  @username = username
8
8
  @key = key
9
9
  end
10
-
10
+
11
11
  # returns an array of domain objects
12
12
  def domains
13
13
  doc = request("domain-list_domains")
14
+ raise ApiError if (doc/:result).innerHTML == "error"
14
15
  (doc/:data).inject([]) { |domains, domain| domains << Domain.new_from_xml(domain); domains }
15
16
  end
16
-
17
+
17
18
  # returns an array of user objects
18
19
  def users(passwords=false)
19
20
  if passwords
@@ -21,32 +22,41 @@ module Dreamy
21
22
  else
22
23
  doc = request("user-list_users_no_pw")
23
24
  end
24
-
25
+ raise ApiError if (doc/:result).innerHTML == "error"
25
26
  (doc/:data).inject([]) { |users, user| users << User.new_from_xml(user); users }
26
27
  end
27
-
28
+
28
29
  # returns an array of dns objects
29
30
  def dns
30
31
  doc = request("dns-list_records")
32
+ raise ApiError if (doc/:result).innerHTML == "error"
31
33
  (doc/:data).inject([]) { |records, dns| records << Dns.new_from_xml(dns); records }
32
34
  end
33
-
35
+
36
+ # returns an array of subscriber objects
37
+ def announce_list(listname,domain)
38
+ doc = request("announcement_list-list_subscribers",{ "listname" => listname, "domain" => domain})
39
+ raise ApiError if (doc/:result).innerHTML == "error"
40
+ (doc/:data).inject([]) { |subs, sub| subs << Subscriber.new_from_xml(sub); subs }
41
+ end
42
+
43
+ private
44
+
34
45
  def request(cmd,values={})
35
46
  handle_response!(response(cmd,values))
36
47
  end
37
-
38
- private
48
+
39
49
 
40
50
  def response(cmd,values={})
41
- params = [
42
- "username=#{@username}",
43
- "key=#{@key}",
44
- "cmd=#{cmd}",
45
- "format=xml",
46
- "unique_id=#{UUID.new.generate}",
47
- ]
48
-
49
- path = "/?#{params.join("&")}"
51
+ values = {
52
+ "username" => @username,
53
+ "key" => @key,
54
+ "cmd" => cmd,
55
+ "format" => "xml",
56
+ "unique_id" => UUID.new.generate
57
+ }.merge(values)
58
+
59
+ path = "/?#{values.to_param_array.join("&")}"
50
60
  http = Net::HTTP.new(@@host, 443)
51
61
  http.use_ssl = true
52
62
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@@ -57,7 +67,7 @@ module Dreamy
57
67
  raise CantConnect, error.message
58
68
  end
59
69
  end
60
-
70
+
61
71
  def handle_response!(response)
62
72
  if %w[200 304].include?(response.code)
63
73
  response = parse(response.body)
@@ -69,7 +79,7 @@ module Dreamy
69
79
  raise CantConnect, "Dreamy is returning a #{response.code}: #{response.message}"
70
80
  end
71
81
  end
72
-
82
+
73
83
  # Converts a string response into an Hpricot xml element.
74
84
  def parse(response)
75
85
  Hpricot.XML(response || '')
@@ -0,0 +1,31 @@
1
+ module Dreamy::Command
2
+ class Announce < Base
3
+
4
+ def list
5
+ if args.length > 0
6
+ listname, domain = extract_values(args.shift)
7
+ subscribers = @account.announce_list(listname,domain)
8
+ if subscribers.empty?
9
+ display "No subscribers to this list"
10
+ else
11
+ subscriber_table = table do |t|
12
+ t.headings = 'email', 'name', 'subscribe_date', 'bounces'
13
+ subscribers.each { |s| t << [s.email, s.name, s.subscribe_date, s.num_bounces] }
14
+ end
15
+ display subscriber_table
16
+ display "#{subscribers.size} total subscribers"
17
+ end
18
+ else
19
+ display "Must specify announcement list. eg - 'my_list@example.com'"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def extract_values(arg)
26
+ arg =~ /^(.*)@(.*)$/
27
+ return $1, $2
28
+ end
29
+
30
+ end
31
+ end
@@ -53,7 +53,7 @@ module Dreamy::Command
53
53
  private
54
54
 
55
55
  def configure_account
56
- Dreamy::Control.new(user,key)
56
+ Dreamy::Base.new(user,key)
57
57
  end
58
58
 
59
59
  def user
@@ -17,6 +17,10 @@ module Dreamy::Command
17
17
  dns # list your DNS records
18
18
  dns <name> # list DNS records for <ame>
19
19
 
20
+ announce:list <name> # lists all subscribers to <name> list (eg - 'my_list@example.com')
21
+ announce:add <name> # add a subscriber to <name> list (COMING SOON)
22
+ announce:remove <name> # remove a subscriber from <name> list (COMING SOON)
23
+
20
24
  users # list user accounts
21
25
 
22
26
  EOTXT
@@ -0,0 +1,9 @@
1
+ class Hash
2
+ def to_param_array
3
+ p = Array.new
4
+ self.each do |k,v|
5
+ p << "#{k}=#{v}"
6
+ end
7
+ p
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module Dreamy
2
+ class Subscriber
3
+ include EasyClassMaker
4
+
5
+ attributes :email, :confirmed, :subscribe_date, :name, :num_bounces
6
+
7
+ def self.new_from_xml(xml)
8
+ s = new
9
+ s.email = (xml).at('email').innerHTML
10
+ # s.confirmed = (xml).at('confirmed').innerHTML
11
+ s.subscribe_date = (xml).at('subscribe_date').innerHTML
12
+ s.name = (xml).at('name').innerHTML
13
+ s.num_bounces = (xml).at('num_bounces').innerHTML
14
+ s
15
+ end
16
+ end
17
+ end
@@ -1,13 +1,13 @@
1
1
  require File.dirname(__FILE__) + "/test_helper.rb"
2
2
 
3
- class DreamyControlTest < Test::Unit::TestCase
3
+ class DreamyBaseTest < Test::Unit::TestCase
4
4
 
5
- @@base = Dreamy::Control.new(CREDS["user"],CREDS["key"])
5
+ @@base = Dreamy::Base.new(CREDS["user"],CREDS["key"])
6
6
 
7
7
  context "Initialization" do
8
8
 
9
9
  should "require email and API key" do
10
- assert_raise(ArgumentError) { Dreamy::Control.new }
10
+ assert_raise(ArgumentError) { Dreamy::Base.new }
11
11
  end
12
12
 
13
13
  end
@@ -41,5 +41,29 @@ class DreamyControlTest < Test::Unit::TestCase
41
41
  end
42
42
  end
43
43
 
44
+ context "Announcement lists" do
45
+ context "subscribers" do
46
+
47
+ should "require values for listname and domain" do
48
+ assert_raise(ArgumentError) { @@base.announce_list() }
49
+ end
50
+
51
+ should "return array of Subscriber records" do
52
+ subscribers = @@base.announce_list(CREDS["listname"],CREDS["domain"])
53
+ assert_kind_of Array, subscribers
54
+ assert_kind_of Dreamy::Subscriber, subscribers.first
55
+ end
56
+
57
+ end
58
+
59
+ context "add subscriber" do
60
+ end
61
+
62
+ context "remove subscriber" do
63
+ end
64
+
65
+ end
66
+
67
+
44
68
 
45
69
  end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + "/test_helper.rb"
2
+
3
+ class DreamySubscriberTest < Test::Unit::TestCase
4
+
5
+ context "Creation" do
6
+ setup do
7
+ @xml = <<EOF
8
+ <data>
9
+ <email>joe@schmoe.com</email>
10
+ <confirmed>1</confirmed>
11
+ <subscribe_date>2007-12-13 16:55:15</subscribe_date>
12
+ <name>Josh</name>
13
+ <num_bounces>0</num_bounces>
14
+ </data>
15
+ EOF
16
+ end
17
+
18
+ should "create a new user from xml" do
19
+ s = Dreamy::Subscriber.new_from_xml(Hpricot.XML(@xml))
20
+ assert_equal "joe@schmoe.com", s.email
21
+ # assert_equal "1", s.confirmed
22
+ assert_equal "2007-12-13 16:55:15", s.subscribe_date
23
+ assert_equal "Josh", s.name
24
+ assert_equal "0", s.num_bounces
25
+ end
26
+ end
27
+
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sant0sk1-dreamy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jerod Santo
@@ -36,20 +36,24 @@ files:
36
36
  - VERSION.yml
37
37
  - bin/dh
38
38
  - lib/dreamy.rb
39
+ - lib/dreamy/base.rb
39
40
  - lib/dreamy/command.rb
41
+ - lib/dreamy/commands/announce.rb
40
42
  - lib/dreamy/commands/base.rb
41
43
  - lib/dreamy/commands/dns.rb
42
44
  - lib/dreamy/commands/domains.rb
43
45
  - lib/dreamy/commands/help.rb
44
46
  - lib/dreamy/commands/users.rb
45
- - lib/dreamy/control.rb
47
+ - lib/dreamy/core_extensions.rb
46
48
  - lib/dreamy/dns.rb
47
49
  - lib/dreamy/domain.rb
48
50
  - lib/dreamy/easy_class_maker.rb
51
+ - lib/dreamy/subscriber.rb
49
52
  - lib/dreamy/user.rb
50
- - test/control_test.rb
53
+ - test/base_test.rb
51
54
  - test/dns_test.rb
52
55
  - test/domain_test.rb
56
+ - test/subscriber_test.rb
53
57
  - test/test_helper.rb
54
58
  - test/user_test.rb
55
59
  has_rdoc: true
@@ -79,8 +83,9 @@ signing_key:
79
83
  specification_version: 2
80
84
  summary: A Ruby library and command line tool for accessing DreamHost's API
81
85
  test_files:
82
- - test/control_test.rb
86
+ - test/base_test.rb
83
87
  - test/dns_test.rb
84
88
  - test/domain_test.rb
89
+ - test/subscriber_test.rb
85
90
  - test/test_helper.rb
86
91
  - test/user_test.rb