streamsend 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b8f5e42bd6932da70daf32dd3550c7e7823c559b
4
+ data.tar.gz: 755c92dc260b78049bdc446a2043d84f0a97ff65
5
+ SHA512:
6
+ metadata.gz: e409054d8e1ebad47497c99bad42ba8b3618149defd0b95878531c8414f42aabc0bc88b9b4caf03588822e05ae84dfa74dcbcf9c1308cb687885e2eacda49030
7
+ data.tar.gz: 112fddfa2ff633eea42cd41052d4fd59d375360ae92e31d4646b779d6a0eee409969fe6f7c95f582edba6ee8eba06ce2de0a8c5f01f3bce6689661c0b373f0f1
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ tags
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  gemspec
data/Gemfile.lock CHANGED
@@ -1,33 +1,55 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- streamsend (0.1.1)
5
- httparty (= 0.7.4)
4
+ streamsend (0.1.4)
5
+ activesupport (~> 3.2)
6
+ builder
7
+ httparty
6
8
 
7
9
  GEM
8
10
  remote: https://rubygems.org/
9
11
  specs:
10
- addressable (2.2.7)
11
- crack (0.1.8)
12
- diff-lcs (1.1.3)
13
- httparty (0.7.4)
14
- crack (= 0.1.8)
15
- rspec (2.9.0)
16
- rspec-core (~> 2.9.0)
17
- rspec-expectations (~> 2.9.0)
18
- rspec-mocks (~> 2.9.0)
19
- rspec-core (2.9.0)
20
- rspec-expectations (2.9.0)
21
- diff-lcs (~> 1.1.3)
22
- rspec-mocks (2.9.0)
23
- webmock (1.8.4)
12
+ activesupport (3.2.13)
13
+ i18n (= 0.6.1)
14
+ multi_json (~> 1.0)
15
+ addressable (2.3.5)
16
+ builder (3.2.2)
17
+ columnize (0.3.6)
18
+ crack (0.4.0)
19
+ safe_yaml (~> 0.9.0)
20
+ debugger (1.6.0)
21
+ columnize (>= 0.3.1)
22
+ debugger-linecache (~> 1.2.0)
23
+ debugger-ruby_core_source (~> 1.2.1)
24
+ debugger-linecache (1.2.0)
25
+ debugger-ruby_core_source (1.2.3)
26
+ diff-lcs (1.2.4)
27
+ httparty (0.11.0)
28
+ multi_json (~> 1.0)
29
+ multi_xml (>= 0.5.2)
30
+ i18n (0.6.1)
31
+ multi_json (1.7.7)
32
+ multi_xml (0.5.4)
33
+ rspec (2.13.0)
34
+ rspec-core (~> 2.13.0)
35
+ rspec-expectations (~> 2.13.0)
36
+ rspec-mocks (~> 2.13.0)
37
+ rspec-core (2.13.1)
38
+ rspec-expectations (2.13.0)
39
+ diff-lcs (>= 1.1.3, < 2.0)
40
+ rspec-mocks (2.13.1)
41
+ safe_yaml (0.9.3)
42
+ vcr (2.5.0)
43
+ webmock (1.12.3)
24
44
  addressable (>= 2.2.7)
25
- crack (>= 0.1.7)
45
+ crack (>= 0.3.2)
26
46
 
27
47
  PLATFORMS
28
48
  ruby
29
49
 
30
50
  DEPENDENCIES
31
- rspec (~> 2.9)
51
+ debugger
52
+ rspec
32
53
  streamsend!
54
+ vcr
33
55
  webmock (~> 1.6)
@@ -0,0 +1,62 @@
1
+ require "active_support/core_ext/hash"
2
+ require File.expand_path(__FILE__, "exception")
3
+
4
+ module StreamSend
5
+ module Api
6
+ class Account < Resource
7
+ def self.all
8
+ response = StreamSend::Api.get("/accounts.xml")
9
+
10
+ case response.code
11
+ when 200
12
+ response["accounts"].collect { |data| new(data) }
13
+ else
14
+ raise StreamSend::Api::Exception.new("Could not find any accounts. Make sure your api username and password are correct. (#{response.code})")
15
+ end
16
+ end
17
+
18
+ def self.show(id)
19
+ response = StreamSend::Api.get("/accounts/#{id}.xml")
20
+ case response.code
21
+ when 200
22
+ new(response["account"])
23
+ else
24
+ raise StreamSend::Api::Exception.new("Could not find the account. Make sure your account ID is correct. (#{response.code})")
25
+ end
26
+ end
27
+
28
+ def self.update(account_hash)
29
+ response = StreamSend::Api.put("/accounts/#{account_hash['account_id']}/users/#{account_hash['user_id']}.xml", :body => account_hash.to_xml)
30
+ case response.code
31
+ when 200
32
+ true
33
+ else
34
+ false
35
+ end
36
+ end
37
+
38
+ def self.create(account_hash)
39
+ response = StreamSend::Api.post("/accounts.xml", :body => {:account => account_hash})
40
+ case response.code
41
+ when 201
42
+ response.headers["location"] =~ %r(/accounts/(\d+))
43
+ account_id = $1
44
+ account_id.to_i
45
+ else
46
+ raise StreamSend::Api::Exception.new("Could not create the account. (#{response.code})")
47
+ end
48
+ end
49
+
50
+ def self.destroy(id)
51
+ id = id.to_i
52
+ response = StreamSend::Api.delete("/accounts/#{id}.xml")
53
+ case response.code
54
+ when 200
55
+ true
56
+ else
57
+ raise StreamSend::Api::Exception.new("Could not delete the account. (#{response.code})")
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,2 @@
1
+ class StreamSend::Api::Exception < Exception
2
+ end
@@ -0,0 +1,55 @@
1
+ require File.expand_path(__FILE__, "exception")
2
+
3
+ module StreamSend
4
+ module Api
5
+ class List < Resource
6
+ def self.all
7
+ response = StreamSend::Api.get("/audiences/#{audience_id}/lists.xml")
8
+
9
+ case response.code
10
+ when 200
11
+ response["lists"].collect { |data| new(data) }
12
+ else
13
+ raise StreamSend::Api::Exception.new("Could not find any lists. (#{response.code})")
14
+ end
15
+ end
16
+
17
+ def self.find(list_id)
18
+ response = StreamSend::Api.get("/audiences/#{audience_id}/lists/#{list_id}.xml")
19
+
20
+ case response.code
21
+ when 200
22
+ new(response["list"])
23
+ else
24
+ raise StreamSend::Api::Exception.new("Could not find any lists. (#{response.code})")
25
+ end
26
+ end
27
+
28
+ def self.create(list_name)
29
+ response = StreamSend::Api.post("/audiences/#{audience_id}/lists.xml", :query => {:list => {:name => list_name}})
30
+
31
+ if response.code == 201
32
+ response.headers["location"] =~ /audiences\/\d\/lists\/(\d+)$/
33
+ new_list_id = $1.to_i
34
+ else
35
+ raise StreamSend::Api::Exception.new("Could not create a list. (#{response.body})")
36
+ end
37
+ end
38
+
39
+ def destroy
40
+ self.class.destroy( id )
41
+ end
42
+
43
+ def self.destroy( id )
44
+ id_as_integer = id.to_i
45
+ response = StreamSend::Api.delete("/audiences/#{audience_id}/lists/#{id_as_integer}.xml")
46
+ case response.code
47
+ when 200
48
+ true
49
+ else
50
+ raise StreamSend::Api::Exception.new "Could not delete list (#{response.code})."
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(__FILE__, "exception")
2
+
3
+ module StreamSend
4
+ module Api
5
+ class Resource
6
+ def initialize(data)
7
+ @data = data
8
+ end
9
+
10
+ def method_missing(method, *args, &block)
11
+ if @data.include?(method.to_s)
12
+ @data[method.to_s]
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ def id
19
+ @data["id"]
20
+ end
21
+
22
+ def self.clear_audience
23
+ @audience_id = nil
24
+ end
25
+
26
+ def self.audience_id
27
+ if @audience_id.nil?
28
+ audiences_repsonse = StreamSend::Api.get("/audiences.xml")
29
+ audiences_entity = audiences_repsonse.parsed_response
30
+ audiences = audiences_entity["audiences"]
31
+ audience = audiences.first
32
+ @audience_id = audience["id"]
33
+ end
34
+ @audience_id
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,86 @@
1
+ require File.expand_path(__FILE__, "exception")
2
+ require 'debugger'
3
+
4
+ module StreamSend
5
+ module Api
6
+ class Subscriber < Resource
7
+ def self.all
8
+ response = StreamSend::Api.get("/audiences/#{audience_id}/people.xml")
9
+
10
+ case response.code
11
+ when 200
12
+ response["people"].collect { |data| new(data) }
13
+ else
14
+ raise StreamSend::Api::Exception.new("Could not find any subscribers. Make sure your audience ID is correct. (#{response.code})")
15
+ end
16
+ end
17
+
18
+ def self.find(email_address)
19
+ response = StreamSend::Api.get("/audiences/#{audience_id}/people.xml?email_address=#{email_address}")
20
+
21
+ case response.code
22
+ when 200
23
+ if subscriber = response["people"].first
24
+ new(subscriber)
25
+ else
26
+ nil
27
+ end
28
+ else
29
+ raise StreamSend::Api::Exception.new("Could not find the subscriber. Make sure your audience ID is correct. (#{response.code})")
30
+ end
31
+ end
32
+
33
+ def self.create(person_hash)
34
+ response = StreamSend::Api.post("/audiences/#{audience_id}/people.xml", :query => {:person => person_hash})
35
+
36
+ case response.code
37
+ when 201
38
+ response.headers["location"] =~ /audiences\/\d+\/people\/(\d+)$/
39
+ subscriber_id = $1
40
+ unless subscriber_id.nil?
41
+ subscriber_id.to_i
42
+ end
43
+ else
44
+ raise StreamSend::Api::Exception.new("Could not create the subscriber. (#{response.code})")
45
+ end
46
+ end
47
+
48
+ def show
49
+ response = StreamSend::Api.get("/audiences/#{audience_id}/people/#{id}.xml")
50
+
51
+ case response.code
52
+ when 200
53
+ if subscriber = response["person"]
54
+ self.class.new(subscriber)
55
+ else
56
+ nil
57
+ end
58
+ else
59
+ raise StreamSend::Api::Exception.new("Could not show the subscriber. (#{response.code})")
60
+ end
61
+ end
62
+
63
+ def activate
64
+ response = StreamSend::Api.post("/audiences/#{audience_id}/people/#{id}/activate.xml")
65
+
66
+ case response.code
67
+ when 200
68
+ true
69
+ else
70
+ raise StreamSend::Api::Exception.new("Could not activate the subscriber. (#{response.code})")
71
+ end
72
+ end
73
+
74
+ def unsubscribe
75
+ response = StreamSend::Api.post("/audiences/#{audience_id}/people/#{id}/unsubscribe.xml")
76
+
77
+ case response.code
78
+ when 200
79
+ true
80
+ else
81
+ raise StreamSend::Api::Exception("Could not subscribe the subscriber. (#{response.code})")
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,56 @@
1
+ require "active_support/core_ext/hash"
2
+ require File.expand_path(__FILE__, "exception")
3
+
4
+ module StreamSend
5
+ module Api
6
+ class User < Resource
7
+ def self.all
8
+ response = StreamSend::Api.get("/users.xml")
9
+
10
+ case response.code
11
+ when 200
12
+ response["users"].collect { |data| new(data) }
13
+ else
14
+ raise StreamSend::Api::Exception.new("Could not find any users. Make sure your account ID is correct. (#{response.code})")
15
+ end
16
+ end
17
+
18
+ def self.show(id)
19
+ response = StreamSend::Api.get("/users/#{id}.xml")
20
+ case response.code
21
+ when 200
22
+ new(response["user"])
23
+ else
24
+ raise StreamSend::Api::Exception.new("Could not find the user. Make sure your account ID and user ID are correct. (#{response.code})")
25
+ end
26
+ end
27
+
28
+ def self.update(user_hash)
29
+ user_properties = user_hash.clone
30
+ user_properties.delete 'user_id'
31
+ user_properties.delete 'account_id'
32
+
33
+ xml_hash = user_properties.to_xml :root => "user"
34
+ response = StreamSend::Api.put("/accounts/#{user_hash['account_id']}/users/#{user_hash['user_id']}.xml", :body => xml_hash, :headers => {'Content-Type' => 'application/xml', 'Accept' => 'application/xml'})
35
+ case response.code
36
+ when 200
37
+ true
38
+ else
39
+ false
40
+ end
41
+ end
42
+
43
+ def self.create(user_hash)
44
+ response = StreamSend::Api.post("/accounts/#{user_hash['account_id']}/users.xml", :body => {:user => user_hash})
45
+ case response.code
46
+ when 201
47
+ response.headers["location"] =~ /\/accounts\/\d+\/users\/(\d+)/
48
+ user_id = $1
49
+ user_id.to_i
50
+ else
51
+ raise StreamSend::Api::Exception.new("Could not create the user. (#{response.code})")
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/streamsend.rb CHANGED
@@ -1,16 +1,22 @@
1
1
  require 'rubygems'
2
2
  require 'httparty'
3
3
 
4
- require "streamsend/resource"
5
- require "streamsend/subscriber"
6
- require "streamsend/list"
4
+ require "streamsend/api/resource"
5
+ require "streamsend/api/subscriber"
6
+ require "streamsend/api/list"
7
+ require "streamsend/api/user"
8
+ require "streamsend/api/account"
9
+ require "streamsend/api/exception"
7
10
 
8
11
  module StreamSend
9
- include HTTParty
10
- format :xml
12
+ module Api
13
+ include HTTParty
14
+ format :xml
11
15
 
12
- def self.configure(username, password, host = "app.streamsend.com")
13
- base_uri host
14
- basic_auth username, password
16
+ def self.configure(username, password, host = "app.streamsend.com")
17
+ base_uri host
18
+ basic_auth username, password
19
+ StreamSend::Api::Subscriber.clear_audience
20
+ end
15
21
  end
16
22
  end
@@ -0,0 +1,69 @@
1
+ require "spec_helper"
2
+
3
+ require File.join(Dir.pwd, "lib/streamsend")
4
+ require "integration/spec_helper"
5
+ require File.expand_path(__FILE__, "../../exception")
6
+
7
+ describe "user via api" do
8
+ before do
9
+ @account = StreamSend::Api::IntegrationConfiguration.root_account
10
+ StreamSend::Api.configure(@account.api_username, @account.api_password, @account.app_host)
11
+ end
12
+
13
+ let(:create_account) do
14
+ pending "TODO -> Broken 2013-04-05"
15
+ create_result = StreamSend::Api::Account.create(
16
+ :name => "account2",
17
+ :automated_email_address => "admin@localhost.com",
18
+ :quota => 100,
19
+ :quota_cap => 100,
20
+ :plan_id => 1,
21
+ :owner => {
22
+ "password" => "password2",
23
+ "password_confirmation" => "password2",
24
+ "email_address" => "joe+#{Time.now.to_i}@gmail.com",
25
+ "first_name" => "Joe",
26
+ "last_name" => "user",
27
+ "may_export" => true,
28
+ "administrator" => true
29
+ })
30
+ end
31
+
32
+ describe ".all" do
33
+ it "lists all accounts" do
34
+ VCR.use_cassette('streamsend') do
35
+ accounts = StreamSend::Api::Account.all
36
+ accounts.count.should > 0
37
+ end
38
+ end
39
+ end
40
+
41
+ describe ".show" do
42
+ it "finds the root account" do
43
+ VCR.use_cassette('streamsend') do
44
+ root_account = StreamSend::Api::Account.show(1)
45
+ root_account.name.should == "EZ Publishing"
46
+ end
47
+ end
48
+ end
49
+
50
+ describe ".create" do
51
+ it "creates a new account" do
52
+ VCR.use_cassette('streamsend') do
53
+ id = create_account
54
+ StreamSend::Api::Account.destroy(id)
55
+ end
56
+ end
57
+ end
58
+
59
+ describe ".destroy" do
60
+ it "deletes an account" do
61
+ VCR.use_cassette('streamsend') do
62
+ id = create_account
63
+ response = StreamSend::Api::Account.destroy(id)
64
+ response.should == true
65
+ expect { StreamSend::Api::Account.show(id) }.to raise_exception(StreamSend::Api::Exception, /could not find/i)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,5 @@
1
+ root_account:
2
+ api_username: "rootapiuser"
3
+ api_password: <%= ENV['STREAMSEND_PASSWORD'] %>
4
+ app_host: "localhost:3000"
5
+ account_id: 1
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+ require File.join(Dir.pwd, "lib/streamsend")
3
+ require "integration/spec_helper"
4
+ require File.expand_path(__FILE__, "../../exception")
5
+
6
+ describe "List API" do
7
+ let( :test_list_prefix ) { "Ruby Gem Client API List #" }
8
+ let( :test_list_name ) { test_list_prefix }
9
+
10
+ before do
11
+ account = StreamSend::Api::IntegrationConfiguration.root_account
12
+ StreamSend::Api.configure(account.api_username, account.api_password, account.app_host)
13
+ end
14
+
15
+ after do
16
+ VCR.use_cassette('streamsend') do
17
+ StreamSend::Api::List.all.select do | list |
18
+ list.name == test_list_name
19
+ end.each do | list |
20
+ list.destroy
21
+ end
22
+ end
23
+ end
24
+
25
+ context "#create" do
26
+ it "creates a list when given a unique name" do
27
+ VCR.use_cassette('streamsend') do
28
+ expect {
29
+ StreamSend::Api::List.create test_list_name
30
+ }.not_to raise_error
31
+ end
32
+ end
33
+
34
+ it "returns the list id" do
35
+ VCR.use_cassette('streamsend') do
36
+ id = StreamSend::Api::List.create test_list_name
37
+ id.should_not be_nil
38
+ end
39
+ end
40
+
41
+ it "creating a new list with an existing name raises an error" do
42
+ VCR.use_cassette('streamsend') do
43
+ StreamSend::Api::List.create test_list_name
44
+ expect {
45
+ StreamSend::Api::List.create test_list_name
46
+ }.to raise_error
47
+ end
48
+ end
49
+
50
+ it "name is properly set on the list" do
51
+ VCR.use_cassette('streamsend') do
52
+ id = StreamSend::Api::List.create test_list_name
53
+ list = StreamSend::Api::List.find id
54
+ list.name.should == test_list_name
55
+ end
56
+ end
57
+ end
58
+
59
+ context "#delete" do
60
+ it "removes the list" do
61
+ VCR.use_cassette('streamsend') do
62
+ list_id = StreamSend::Api::List.create test_list_name
63
+ list = StreamSend::Api::List.find list_id
64
+ list.destroy.should be_true
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,16 @@
1
+ require "yaml"
2
+ require "ostruct"
3
+
4
+ module StreamSend
5
+ module Api
6
+ class IntegrationConfiguration
7
+ def self.config
8
+ @config ||= YAML.load(ERB.new(IO.read("spec/integration/integration.yml")).result)
9
+ end
10
+
11
+ def self.root_account
12
+ OpenStruct.new(config["root_account"])
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+ require File.join(Dir.pwd, "lib/streamsend")
3
+ require "integration/spec_helper.rb"
4
+ require "debugger"
5
+
6
+ describe "user via api" do
7
+ before do
8
+ account = StreamSend::Api::IntegrationConfiguration.root_account
9
+ StreamSend::Api.configure(account.api_username, account.api_password, account.app_host)
10
+ end
11
+
12
+ it "exists" do
13
+ VCR.use_cassette('streamsend') do
14
+ the_user = StreamSend::Api::User.all.first
15
+ the_user.first_name.should == "Admin"
16
+ end
17
+ end
18
+
19
+ it "gets created" do
20
+ VCR.use_cassette('streamsend') do
21
+ the_user = StreamSend::Api::User.create(
22
+ "password" => "password2",
23
+ "password_confirmation" => "password2",
24
+ "email_address" => "joe#{Time.now.to_i}@gmail.com",
25
+ "first_name" => "Joe",
26
+ "last_name" => "user",
27
+ "account_id" => 1,
28
+ "may_export" => true,
29
+ "administrator" => true
30
+ )
31
+
32
+ saved_user = find_by_email_address("joe@gmail.com")
33
+ saved_user.first.id.should > 0
34
+ end
35
+ end
36
+
37
+ it "can be found" do
38
+ VCR.use_cassette('streamsend') do
39
+ user = StreamSend::Api::User.show(1)
40
+ expect(user.class).to eq(StreamSend::Api::User)
41
+ end
42
+ end
43
+
44
+ it "allows us to change the last name" do
45
+ VCR.use_cassette('streamsend') do
46
+ the_user = StreamSend::Api::User.all.last
47
+ expect(the_user.last_name).to eq("user")
48
+ response = StreamSend::Api::User.update("account_id" => StreamSend::Api::IntegrationConfiguration.root_account.account_id, "last-name" => "smithy", "user_id" => the_user.id)
49
+ new_user = StreamSend::Api::User.show(the_user.id)
50
+ expect(new_user.last_name).to eq("smithy")
51
+ end
52
+ end
53
+
54
+ def find_by_email_address(email_address)
55
+ the_user = StreamSend::Api::User.all.each do |user|
56
+ return user if user.email_address == email_address
57
+ end
58
+ end
59
+ end