peat 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .ruby-version
24
+ .rbenv-gemsets
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in peat.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Hubert Huang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Peat
2
+
3
+ Peat is a thin wrapper around the Exact Target Fuel Rest API, which abstracts away oauth token and connection management.
4
+ Currently, it only supports the use of Triggered Sends.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'peat'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install peat
19
+
20
+ ## Usage
21
+
22
+ The Peat::TokenManager class depends on two global variables being set -- $fuel_client_id and $fuel_secret.
23
+
24
+ If you're using a rails app, this could be added to config/initializers/peat.rb
25
+
26
+ ```
27
+ $fuel_client_id = 'yourclientid'
28
+ $fuel_secret = 'yoursecret'
29
+ ```
30
+
31
+ Typical usage is as so:
32
+
33
+ ```
34
+ mail_params = {
35
+ "From" => {
36
+ "Address" => 'my_email@gmail.com',
37
+ "Name" => 'Don Johnson',
38
+ },
39
+ "To" => {
40
+ "Address" => 'my_friend@gmail.com',
41
+ "SubscriberKey" => 'my_friend@gmail.com',
42
+ "ContactAttributes" => {
43
+ "SubscriberAttributes" => {
44
+ 'foo' => 'bar',
45
+ 'baz' => 'qux',
46
+ }
47
+ }
48
+ }
49
+ }
50
+ peat_client = Peat::Client.deliver('name_of_my_triggered_send', mail_params')
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ module Peat
2
+ module Client
3
+ module_function
4
+
5
+ def connection
6
+ @connection ||= Faraday.new(url: "https://www.exacttargetapis.com/messaging/v1/") do |conn|
7
+ conn.request :json
8
+
9
+ conn.response :logger
10
+
11
+ conn.adapter Faraday.default_adapter
12
+ end
13
+ end
14
+
15
+ def deliver(trigger_name, params)
16
+ resp = connection.post do |req|
17
+ req.url "messageDefinitionSends/key:#{trigger_name}/send"
18
+ req.headers['Content-Type'] = 'application/json'
19
+ req.headers['Authorization'] = "Bearer #{Peat::TokenManager.token}"
20
+ req.body = params.to_json
21
+ end
22
+
23
+ resp.success?
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,43 @@
1
+ module Peat
2
+ module TokenManager
3
+ module_function
4
+
5
+ def token
6
+ fetch_token do
7
+ connection.post do |req|
8
+ req.url 'requestToken'
9
+ req.headers['Content-Type'] = 'application/json'
10
+ req.body = {
11
+ 'clientId' => $fuel_client_id,
12
+ 'clientSecret' => $fuel_secret,
13
+ }.to_json
14
+ end.body.merge('created_at' => Time.now)
15
+ end
16
+ end
17
+
18
+ def connection
19
+ @connection = Faraday.new(url: 'https://auth.exacttargetapis.com/v1/') do |conn|
20
+ conn.request :json
21
+
22
+ conn.response :logger
23
+ conn.response :json, :content_type => /\bjson$/
24
+
25
+ conn.adapter Faraday.default_adapter
26
+ end
27
+ end
28
+
29
+ def token_expired?
30
+ # token is good for an hour, but to be safe...
31
+ Time.now > 50.minutes.since(@token['created_at'])
32
+ end
33
+
34
+ def fetch_token
35
+ if @token && !token_expired?
36
+ @token['accessToken']
37
+ else
38
+ @token = yield
39
+ @token['accessToken']
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Peat
2
+ VERSION = "0.0.1"
3
+ end
data/lib/peat.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "peat/version"
2
+
3
+ module Peat
4
+ autoload :TokenManager, 'peat/token_manager'
5
+ autoload :Client, 'peat/client'
6
+ end
data/peat.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'peat/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "peat"
8
+ spec.version = Peat::VERSION
9
+ spec.authors = ["Hubert Huang"]
10
+ spec.email = ["hhuang@practicefusion.com"]
11
+ spec.summary = %q{Wrapper for the Exact Target fuel api}
12
+ spec.description = %q{Wrapper for the Exact Target fuel api}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'faraday'
22
+ spec.add_dependency 'activesupport'
23
+
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,29 @@
1
+ require 'peat'
2
+ require 'active_support/core_ext'
3
+
4
+ module Peat
5
+ describe TokenManager do
6
+ context 'token is nil' do
7
+ it 'fetches token' do
8
+ expect(TokenManager).to receive(:fetch_token)
9
+ TokenManager.token
10
+ end
11
+ end
12
+
13
+ context 'token is expired' do
14
+ it 'fetches token' do
15
+ TokenManager.instance_variable_set('@token', { 'created_at' => 1.hour.ago })
16
+ expect(TokenManager).to receive(:fetch_token)
17
+ TokenManager.token
18
+ end
19
+ end
20
+
21
+ context 'token is present and not expired' do
22
+ it 'returns token' do
23
+ token = { 'accessToken' => 'foobar123', 'created_at' => 1.day.from_now }
24
+ TokenManager.instance_variable_set('@token', token)
25
+ expect(TokenManager.token).to eql('foobar123')
26
+ end
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hubert Huang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.6'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.6'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Wrapper for the Exact Target fuel api
95
+ email:
96
+ - hhuang@practicefusion.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/peat.rb
107
+ - lib/peat/client.rb
108
+ - lib/peat/token_manager.rb
109
+ - lib/peat/version.rb
110
+ - peat.gemspec
111
+ - spec/token_manager_spec.rb
112
+ homepage: ''
113
+ licenses:
114
+ - MIT
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 1.8.23.2
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Wrapper for the Exact Target fuel api
137
+ test_files:
138
+ - spec/token_manager_spec.rb