sunnytrail 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Sunnytrail
2
+
3
+ Sunnytrail API Wrapper
data/lib/sunnytrail.rb ADDED
@@ -0,0 +1,119 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+ require 'hashie'
5
+
6
+ class Sunnytrail
7
+
8
+
9
+ class ConfigurationError < StandardError; end
10
+
11
+ attr_accessor :options
12
+
13
+
14
+ # class methods
15
+
16
+ class << self
17
+ def configure(options = {})
18
+ default_options = {
19
+ :api_url => "api.thesunnytrail.com",
20
+ :api_key => nil,
21
+ :use_ssl => true
22
+ }
23
+ @options = default_options.merge(options)
24
+ end
25
+
26
+ def options
27
+ @options ||= {}
28
+ end
29
+
30
+ def add_event(args={})
31
+ sunny_trail = Sunnytrail.new
32
+ sunny_trail.add_event(args)
33
+ end
34
+ end
35
+
36
+ # EOF class methods
37
+
38
+ # instance methods
39
+ def initialize(init_options={})
40
+ @options = Sunnytrail.options.merge(init_options)
41
+ raise ConfigurationError, "API KEY not set" if @options[:api_key].nil?
42
+ end
43
+
44
+ def add_event(args={})
45
+ request args
46
+ end
47
+
48
+ # EOF instance methods
49
+
50
+
51
+ private
52
+
53
+ def setup_call
54
+ api = Net::HTTP.new(@options[:api_url], @options[:use_ssl] ? 443 : 80)
55
+ api.use_ssl = true if @options[:use_ssl]
56
+ api
57
+ end
58
+
59
+ def request(message)
60
+
61
+ api = setup_call
62
+
63
+ response = api.post("/messages?apikey=#{@options[:api_key]}",
64
+ "message=#{message.to_json}")
65
+
66
+ case response.code.to_i
67
+ when 200..202
68
+ return true
69
+ when 403
70
+ raise "The request is invalid: #{response.body}"
71
+ when 503
72
+ raise "Service Unavailable"
73
+ else
74
+ raise "#{response.code}: #{response.message}"
75
+ end
76
+ end
77
+
78
+ class Event < Hashie::Dash
79
+
80
+ property :id
81
+ property :name
82
+ property :email
83
+
84
+ def action
85
+ @action ||= Action.new
86
+ end
87
+
88
+ def plan
89
+ @plan ||= Plan.new
90
+ end
91
+
92
+ def to_hash
93
+ out = {}
94
+ out[:action] = @action.to_hash if @action
95
+ out[:plan] = @plan.to_hash if @plan
96
+ keys.each do |k|
97
+ out[k] = Hashie::Hash === self[k] ? self[k].to_hash : self[k]
98
+ end
99
+ out
100
+ end
101
+
102
+ def to_json
103
+ to_hash.to_json
104
+ end
105
+
106
+ class Action < Hashie::Dash
107
+ property :name
108
+ property :created, :default => Time.now.to_i
109
+ end
110
+
111
+ class Plan < Hashie::Dash
112
+ property :name
113
+ property :price
114
+ property :recurring
115
+ end
116
+
117
+ end
118
+
119
+ end
data/spec/mock.rb ADDED
@@ -0,0 +1,9 @@
1
+ #Sunnytrail Mock. We override request method not to sent requests to SunnyTrail
2
+
3
+ class Sunnytrail
4
+ private
5
+
6
+ def request(message)
7
+ return true
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+ # optionally add autorun support
3
+ require 'rspec/autorun'
4
+
5
+ Rspec.configure do |c|
6
+ c.mock_with :rspec
7
+ end
8
+
@@ -0,0 +1,68 @@
1
+ require 'sunnytrail'
2
+ require 'mock'
3
+
4
+ describe Sunnytrail do
5
+
6
+ before :all do
7
+ TIME_NOW = Time.now.to_i
8
+ OPTIONS_HASH = {:id => 123,
9
+ :email => "user123@example.com",
10
+ :name => "User123",
11
+ :action => {
12
+ :name => "Signup",
13
+ :created => TIME_NOW
14
+ },
15
+ :plan => {
16
+ :name => "Gold",
17
+ :price => 123.0,
18
+ :recurring => 31
19
+ }
20
+ }
21
+
22
+ end
23
+
24
+ it "should raise exception if not configured" do
25
+ lambda {Sunnytrail.new}.should raise_error(Sunnytrail::ConfigurationError)
26
+ end
27
+
28
+ it "should configure properly" do
29
+ Sunnytrail.configure :api_key => "abcdefghijklmnoprstvuxywz"
30
+ Sunnytrail.options[:api_key].should == "abcdefghijklmnoprstvuxywz"
31
+ sunnytrail = Sunnytrail.new
32
+ sunnytrail.options[:api_key].should == "abcdefghijklmnoprstvuxywz"
33
+ end
34
+
35
+ it "should be configured" do
36
+ Sunnytrail.options[:api_key].should == "abcdefghijklmnoprstvuxywz"
37
+ end
38
+
39
+ it "should override conf when specified in constructor" do
40
+ sunnytrail = Sunnytrail.new :api_key => "thisisthenewtoken"
41
+ sunnytrail.options[:api_key].should == "thisisthenewtoken"
42
+ end
43
+
44
+ it "should send event" do
45
+ sunnytrail = Sunnytrail.new
46
+ sunnytrail.add_event(OPTIONS_HASH).should be_true
47
+ end
48
+
49
+
50
+ describe Sunnytrail::Event do
51
+
52
+ it "should translate to hash and json properly" do
53
+
54
+ event = Sunnytrail::Event.new
55
+ event.id = 123
56
+ event.name = "User123"
57
+ event.email = "user123@example.com"
58
+ event.action.name = "Signup"
59
+ event.action.created = TIME_NOW
60
+ event.plan.name = "Gold"
61
+ event.plan.price = 123.0
62
+ event.plan.recurring = 31
63
+ event.to_hash.should == OPTIONS_HASH
64
+ event.to_json.should == OPTIONS_HASH.to_json
65
+ end
66
+ end
67
+
68
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sunnytrail
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mateusz Zawisza
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-10 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: hashie
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: Wrapper for SunnyTrail API
50
+ email: mateusz@applicake.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - README.rdoc
57
+ files:
58
+ - README.rdoc
59
+ - lib/sunnytrail.rb
60
+ - spec/mock.rb
61
+ - spec/spec_helper.rb
62
+ - spec/sunnytrail_spec.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/mateuszzawisza/sunnytrail
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.7
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Wrapper for SunnyTrail API
97
+ test_files: []
98
+