sunnytrail 0.0.1.3 → 0.0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sunnytrail.rb +35 -10
- data/spec/mock.rb +3 -4
- data/spec/sunnytrail_spec.rb +44 -14
- metadata +3 -3
data/lib/sunnytrail.rb
CHANGED
@@ -2,6 +2,7 @@ require 'net/http'
|
|
2
2
|
require 'net/https'
|
3
3
|
require 'json'
|
4
4
|
require 'hashie'
|
5
|
+
require 'logger'
|
5
6
|
|
6
7
|
class Sunnytrail
|
7
8
|
|
@@ -14,12 +15,17 @@ class Sunnytrail
|
|
14
15
|
# class methods
|
15
16
|
|
16
17
|
class << self
|
17
|
-
def configure(options = {})
|
18
|
+
def configure(options = {}, &block)
|
18
19
|
default_options = {
|
19
|
-
:api_url
|
20
|
-
:api_key
|
21
|
-
:use_ssl
|
20
|
+
:api_url => "api.thesunnytrail.com",
|
21
|
+
:api_key => nil,
|
22
|
+
:use_ssl => true,
|
23
|
+
:verbose => false,
|
24
|
+
:send_events => true,
|
25
|
+
:log_path => 'sunnytrail.log'
|
22
26
|
}
|
27
|
+
|
28
|
+
block.call(options) if block_given?
|
23
29
|
@options = default_options.merge(options)
|
24
30
|
end
|
25
31
|
|
@@ -28,21 +34,29 @@ class Sunnytrail
|
|
28
34
|
end
|
29
35
|
|
30
36
|
def add_event(args={})
|
31
|
-
sunny_trail
|
32
|
-
sunny_trail.add_event(args)
|
37
|
+
@sunny_trail ||= Sunnytrail.new
|
38
|
+
@sunny_trail.add_event(args)
|
33
39
|
end
|
34
40
|
end
|
35
41
|
|
36
42
|
# EOF class methods
|
37
43
|
|
44
|
+
|
38
45
|
# instance methods
|
39
46
|
def initialize(init_options={})
|
40
47
|
@options = Sunnytrail.options.merge(init_options)
|
48
|
+
start_logger if verbose?
|
41
49
|
raise ConfigurationError, "API KEY not set" if @options[:api_key].nil?
|
42
50
|
end
|
43
51
|
|
44
52
|
def add_event(args={})
|
45
|
-
|
53
|
+
message = args.to_json
|
54
|
+
@logger.info("Event sent: #{message}") if verbose?
|
55
|
+
request(message) if @options[:send_events]
|
56
|
+
end
|
57
|
+
|
58
|
+
def verbose?
|
59
|
+
!!@options[:verbose]
|
46
60
|
end
|
47
61
|
|
48
62
|
# EOF instance methods
|
@@ -61,7 +75,7 @@ class Sunnytrail
|
|
61
75
|
api = setup_call
|
62
76
|
|
63
77
|
response = api.post("/messages?apikey=#{@options[:api_key]}",
|
64
|
-
"message=#{message
|
78
|
+
"message=#{message}")
|
65
79
|
|
66
80
|
case response.code.to_i
|
67
81
|
when 200..202
|
@@ -75,6 +89,17 @@ class Sunnytrail
|
|
75
89
|
end
|
76
90
|
end
|
77
91
|
|
92
|
+
def start_logger
|
93
|
+
@logger = Logger.new(@options[:log_path]) if verbose?
|
94
|
+
@logger.formatter = proc{|s,t,p,m|"%5s [%s] (%s) %s :: %s\n" % [s, t.strftime("%Y-%m-%d %H:%M:%S"), $$, p, m]}
|
95
|
+
@logger.info "Options set:"
|
96
|
+
@options.each_pair do |option, value|
|
97
|
+
@logger.info "#{option} => #{value}"
|
98
|
+
end
|
99
|
+
@logger.info "EOF Options\n\n"
|
100
|
+
end
|
101
|
+
|
102
|
+
|
78
103
|
class Event < Hashie::Dash
|
79
104
|
|
80
105
|
attr_writer :action, :plan
|
@@ -93,8 +118,8 @@ class Sunnytrail
|
|
93
118
|
|
94
119
|
def to_hash
|
95
120
|
out = {}
|
96
|
-
out[
|
97
|
-
out[
|
121
|
+
out["action"] = @action.nil? ? {} : @action.to_hash
|
122
|
+
out["plan"] = @plan.nil? ? {} : @plan.to_hash
|
98
123
|
keys.each do |k|
|
99
124
|
out[k] = Hashie::Hash === self[k] ? self[k].to_hash : self[k]
|
100
125
|
end
|
data/spec/mock.rb
CHANGED
data/spec/sunnytrail_spec.rb
CHANGED
@@ -5,17 +5,17 @@ describe Sunnytrail do
|
|
5
5
|
|
6
6
|
before :all do
|
7
7
|
TIME_NOW = Time.now.to_i
|
8
|
-
OPTIONS_HASH = {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
OPTIONS_HASH = {"id" => 123,
|
9
|
+
"email" => "user123@example.com",
|
10
|
+
"name" => "User123",
|
11
|
+
"action" => {
|
12
|
+
"name" => "Signup",
|
13
|
+
"created" => TIME_NOW
|
14
14
|
},
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
"plan" => {
|
16
|
+
"name" => "Gold",
|
17
|
+
"price" => 123.0,
|
18
|
+
"recurring" => 31
|
19
19
|
}
|
20
20
|
}
|
21
21
|
|
@@ -39,13 +39,43 @@ describe Sunnytrail do
|
|
39
39
|
it "should override conf when specified in constructor" do
|
40
40
|
sunnytrail = Sunnytrail.new :api_key => "thisisthenewtoken"
|
41
41
|
sunnytrail.options[:api_key].should == "thisisthenewtoken"
|
42
|
+
Sunnytrail.options[:api_key].should == "abcdefghijklmnoprstvuxywz"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should override conf when specified in block" do
|
46
|
+
Sunnytrail.configure do |config|
|
47
|
+
config[:api_key] = "evennewertoken"
|
48
|
+
config[:use_ssl] = false
|
49
|
+
end
|
50
|
+
Sunnytrail.options[:api_key].should == "evennewertoken"
|
51
|
+
Sunnytrail.options[:use_ssl].should be_false
|
52
|
+
Sunnytrail.options[:use_ssl] = true
|
42
53
|
end
|
43
54
|
|
44
55
|
it "should send event" do
|
45
|
-
sunnytrail = Sunnytrail.new
|
56
|
+
sunnytrail = Sunnytrail.new
|
46
57
|
sunnytrail.add_event(OPTIONS_HASH).should be_true
|
47
58
|
end
|
48
59
|
|
60
|
+
it "should not send event if send_events set to false" do
|
61
|
+
sunnytrail = Sunnytrail.new :send_events => false
|
62
|
+
sunnytrail.add_event(OPTIONS_HASH).should be_nil
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should create log file when in verbose mode" do
|
66
|
+
sunnytrail = Sunnytrail.new(:verbose => true)
|
67
|
+
sunnytrail.add_event(OPTIONS_HASH).should be_true
|
68
|
+
File.exist?("sunnytrail.log").should be_true
|
69
|
+
|
70
|
+
sunnytrail = Sunnytrail.new(:verbose => true,
|
71
|
+
:log_path => "sunnylogger.log")
|
72
|
+
sunnytrail.add_event(OPTIONS_HASH).should be_true
|
73
|
+
File.exist?("sunnylogger.log").should be_true
|
74
|
+
|
75
|
+
File.delete("sunnytrail.log")
|
76
|
+
File.delete("sunnylogger.log")
|
77
|
+
|
78
|
+
end
|
49
79
|
|
50
80
|
describe Sunnytrail::Event do
|
51
81
|
|
@@ -61,7 +91,7 @@ describe Sunnytrail do
|
|
61
91
|
event.plan.price = 123.0
|
62
92
|
event.plan.recurring = 31
|
63
93
|
event.to_hash.should == OPTIONS_HASH
|
64
|
-
event.to_json.should == OPTIONS_HASH
|
94
|
+
JSON.parse(event.to_json).should == OPTIONS_HASH
|
65
95
|
end
|
66
96
|
|
67
97
|
it "should set up and clear action and plan attributes properly" do
|
@@ -79,8 +109,8 @@ describe Sunnytrail do
|
|
79
109
|
it "should add empty plan and action node to hash when they are not specified" do
|
80
110
|
event = Sunnytrail::Event.new
|
81
111
|
hash = event.to_hash
|
82
|
-
hash[
|
83
|
-
hash[
|
112
|
+
hash["action"].should == {}
|
113
|
+
hash["plan"].should == {}
|
84
114
|
end
|
85
115
|
end
|
86
116
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sunnytrail
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 67
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
9
|
- 1
|
10
|
-
-
|
11
|
-
version: 0.0.1.
|
10
|
+
- 4
|
11
|
+
version: 0.0.1.4
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- Mateusz Zawisza
|