kahuna_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ *.gem
2
+ .ruby-version
3
+ .ruby-gemset
4
+ Gemfile.lock
5
+ .DS_Store
6
+ # YARD artifacts
7
+ .yardoc
8
+ _yardoc
9
+ doc/
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ matrix:
3
+ allow_failures:
4
+ - rvm: ruby-head
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.0.0
8
+ - 2.1.0
9
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Edgar Gonzalez
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,90 @@
1
+ # KahunaClient [![Build Status](https://travis-ci.org/edgar/KahunaClient.png?branch=master)](https://travis-ci.org/edgar/KahunaClient)
2
+ A ruby gem for the Kahuna Push API - https://app.usekahuna.com/tap/getstarted/pushapi/
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'kahuna_client'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install kahuna_client
19
+
20
+
21
+ ## API Usage Examples
22
+
23
+ ```ruby
24
+ require "rubygems"
25
+ require "kahuna_client"
26
+
27
+ client = KahunaClient::Client.new(secret_key: 'your secret key', api_key: 'your api key')
28
+
29
+ push_object = {
30
+ target: {
31
+ username: "iamawesome1989",
32
+ email: "awesome@mail.com",
33
+ fbid: "42",
34
+ user_id: "789"
35
+ },
36
+ notification: {
37
+ alert: "Look at this Push!"
38
+ },
39
+ params: {
40
+ sale_id: "1234",
41
+ landing_page: "share_page"
42
+ },
43
+ config: {
44
+ start_time: 1382652322,
45
+ optimal_hours: 8,
46
+ influence_rate_limiting: true,
47
+ observe_rate_limiting: true
48
+ }
49
+ }
50
+
51
+ # Send the pushes (one or more)
52
+ client.push([push_object])
53
+ ```
54
+
55
+ For more information about extra parameters and error conditions check the specs folder
56
+
57
+ ## Configuration
58
+
59
+ Because KahunaClient gem is based on [Faraday](https://github.com/lostisland/faraday), it supports the following adapters:
60
+
61
+ * Net::HTTP (default)
62
+ * [Excon](https://github.com/geemus/excon)
63
+ * [Typhoeus](https://github.com/typhoeus/typhoeus)
64
+ * [Patron](http://toland.github.com/patron/)
65
+ * [EventMachine](https://github.com/igrigorik/em-http-request)
66
+
67
+ Beside the adapter, you can change the following properties:
68
+
69
+ * endpoint
70
+ * user_agent
71
+ * proxy
72
+ * debug
73
+ * environment
74
+
75
+ For instance:
76
+
77
+ ```ruby
78
+ require 'typhoeus/adapters/faraday' # You will need the typhoeus gem
79
+
80
+ client = KahunaClient.client(adapter: :typhoeus, user_agent: "foobar v1", debug: true, secret_key: 'foo', api_key: 'bar')
81
+ client.push(push_array)
82
+ ```
83
+
84
+ ## Contributing
85
+
86
+ 1. Fork it
87
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
88
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
89
+ 4. Push to the branch (`git push origin my-new-feature`)
90
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+ task :default => :spec
7
+ task :test => :spec
8
+
9
+ namespace :doc do
10
+ begin
11
+ require 'yard'
12
+ rescue LoadError
13
+ # ignore
14
+ else
15
+ YARD::Rake::YardocTask.new do |task|
16
+ task.files = ['lib/**/*.rb']
17
+ task.options = [
18
+ '--protected',
19
+ '--output-dir', 'doc/yard',
20
+ '--tag', 'format:Supported formats',
21
+ '--markup', 'markdown',
22
+ ]
23
+ end
24
+ end
25
+ end
26
+
27
+ desc "Open an irb session preloaded with this library"
28
+ task :console do
29
+ sh "irb -rubygems -I lib -r kahuna_client.rb"
30
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kahuna_client/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.files = `git ls-files`.split($\)
8
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
9
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
10
+ gem.summary = %q{A ruby wrapper for Kahuna Push API}
11
+ gem.name = "kahuna_client"
12
+ gem.require_paths = ["lib"]
13
+ gem.version = KahunaClient::VERSION
14
+ gem.license = "MIT"
15
+
16
+ gem.add_development_dependency 'rake'
17
+ gem.add_development_dependency 'rspec'
18
+ gem.add_development_dependency 'webmock'
19
+ gem.add_development_dependency 'yard'
20
+ gem.add_development_dependency 'bluecloth'
21
+
22
+ gem.add_dependency 'faraday', '~> 0.8.7'
23
+ gem.add_dependency 'faraday_middleware', '~> 0.9.1'
24
+ gem.add_dependency 'multi_json', '~> 1.10.0'
25
+ gem.add_dependency 'hashie'
26
+ gem.authors = ["Edgar Gonzalez"]
27
+ gem.email = ["edgargonzalez@gmail.com"]
28
+ gem.description = %q{A ruby wrapper for Kahuna Push API}
29
+ gem.homepage = "http://github.com/edgar/kahuna_client"
30
+ end
@@ -0,0 +1,26 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module FaradayMiddleware
5
+ # @private
6
+ class RaiseHttpException < Faraday::Middleware
7
+ def call(env)
8
+ @app.call(env).on_complete do |response|
9
+ if [400, 401, 404, 409, 500].include? response[:status].to_i
10
+ raise KahunaClient::Error.new(error_message(response), response[:status].to_i, response[:body])
11
+ end
12
+ end
13
+ end
14
+
15
+ def initialize(app)
16
+ super app
17
+ @parser = nil
18
+ end
19
+
20
+ private
21
+
22
+ def error_message(response)
23
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s, response[:body]].compact.join(': ')}"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'kahuna_client/error'
2
+ require 'kahuna_client/configuration'
3
+ require 'kahuna_client/api'
4
+ require 'kahuna_client/client'
5
+
6
+ module KahunaClient
7
+ extend Configuration
8
+
9
+ # Alias for KahunaClient::Client.new
10
+ #
11
+ # @return [KahunaClient::Client]
12
+ def self.client(options={})
13
+ KahunaClient::Client.new(options)
14
+ end
15
+
16
+ # Delegate to KahunaClient::Client
17
+ def self.method_missing(method, *args, &block)
18
+ return super unless client.respond_to?(method)
19
+ client.send(method, *args, &block)
20
+ end
21
+
22
+ # Delegate to KahunaClient::Client
23
+ def self.respond_to?(method)
24
+ return client.respond_to?(method) || super
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ require 'kahuna_client/connection'
2
+ require 'kahuna_client/request'
3
+
4
+ module KahunaClient
5
+ # @private
6
+ class API
7
+ # @private
8
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
9
+
10
+ # Creates a new API
11
+ def initialize(options={})
12
+ options = KahunaClient.options.merge(options)
13
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
14
+ send("#{key}=", options[key])
15
+ end
16
+ end
17
+
18
+ include Connection
19
+ include Request
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ module KahunaClient
2
+ class Client < API
3
+
4
+ Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
5
+
6
+ include KahunaClient::Client::Push
7
+ end
8
+ end
@@ -0,0 +1,31 @@
1
+ module KahunaClient
2
+ class Client
3
+ module Push
4
+
5
+ # Send a Push notification to specific users.
6
+ #
7
+ # Params:
8
+ # push_array: Array of one or more Push Objects to target for Push messages
9
+ # default_params: (Optional) Dictionary of key/value pairs to send down with the Push that can be
10
+ # extracted with the Kahuna SDK from deep-linking
11
+ # default_config: (Optional) Dictionary of key/value configuration parameters that should be the default
12
+ # values for all pushes in the push array
13
+ #
14
+ #
15
+ def push(push_array, default_params = {}, default_config = {})
16
+ options = {
17
+ :default_params => default_params,
18
+ :default_config => default_config,
19
+ :push_array => push_array
20
+ }
21
+ post(push_path, options)
22
+ end
23
+
24
+ protected
25
+
26
+ def push_path
27
+ "api/push"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,78 @@
1
+ require 'faraday'
2
+ require 'kahuna_client/version'
3
+
4
+ module KahunaClient
5
+ # Defines constants and methods related to configuration
6
+ module Configuration
7
+ # An array of valid keys in the options hash when configuring a {NYCGeoClient::API}
8
+ VALID_OPTIONS_KEYS = [
9
+ :adapter,
10
+ :api_key, #password BasicAuthentication
11
+ :secret_key, #username BasicAuthentication
12
+ :endpoint,
13
+ :environment, # (s = sandbox, p = production)
14
+ :user_agent,
15
+ :proxy,
16
+ :debug
17
+ ].freeze
18
+
19
+ # The adapter that will be used to connect if none is set
20
+ #
21
+ # @note The default faraday adapter is Net::HTTP.
22
+ DEFAULT_ADAPTER = Faraday.default_adapter
23
+
24
+ # By default, don't set an API Key
25
+ DEFAULT_API_KEY = nil
26
+
27
+ # By default, don't set a Secret Key
28
+ DEFAULT_SECRET_KEY = nil
29
+
30
+ # The endpoint that will be used to connect if none is set
31
+ #
32
+ # @note There is no reason to use any other endpoint at this time
33
+ DEFAULT_ENDPOINT = 'https://tap-nexus.appspot.com/'.freeze
34
+
35
+ # By default, don't use a proxy server
36
+ DEFAULT_PROXY = nil
37
+
38
+ # By default, dont' log the request/response
39
+ DEFAULT_DEBUG = false
40
+
41
+ # The user agent that will be sent to the API endpoint if none is set
42
+ DEFAULT_USER_AGENT = "KahunaClient #{KahunaClient::VERSION}".freeze
43
+
44
+ # By default, use sandbox as environment
45
+ DEFAULT_ENVIRONMENT = 's'
46
+ # @private
47
+ attr_accessor *VALID_OPTIONS_KEYS
48
+
49
+ # When this module is extended, set all configuration options to their default values
50
+ def self.extended(base)
51
+ base.reset
52
+ end
53
+
54
+ # Convenience method to allow configuration options to be set in a block
55
+ def configure
56
+ yield self
57
+ end
58
+
59
+ # Create a hash of options and their values
60
+ def options
61
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
62
+ option.merge!(key => send(key))
63
+ end
64
+ end
65
+
66
+ # Reset all configuration options to defaults
67
+ def reset
68
+ self.adapter = DEFAULT_ADAPTER
69
+ self.api_key = DEFAULT_API_KEY
70
+ self.secret_key = DEFAULT_SECRET_KEY
71
+ self.endpoint = DEFAULT_ENDPOINT
72
+ self.environment = DEFAULT_ENVIRONMENT
73
+ self.user_agent = DEFAULT_USER_AGENT
74
+ self.proxy = DEFAULT_PROXY
75
+ self.debug = DEFAULT_DEBUG
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,33 @@
1
+ require 'faraday_middleware'
2
+ Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
3
+
4
+ module KahunaClient
5
+ # @private
6
+ module Connection
7
+ private
8
+
9
+ def connection(raw=false)
10
+ options = {
11
+ :headers => {
12
+ 'User-Agent' => user_agent
13
+ },
14
+ :proxy => proxy,
15
+ :ssl => {:verify => false},
16
+ :url => endpoint,
17
+ }
18
+
19
+ Faraday::Connection.new(options) do |connection|
20
+ connection.request :basic_auth, secret_key, api_key
21
+ connection.request :json
22
+ connection.use Faraday::Request::UrlEncoded
23
+ unless raw
24
+ connection.use FaradayMiddleware::Mashify
25
+ connection.use Faraday::Response::ParseJson
26
+ end
27
+ connection.use FaradayMiddleware::RaiseHttpException
28
+ connection.use Faraday::Response::Logger if debug
29
+ connection.adapter(adapter)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ require 'multi_json'
2
+
3
+ module KahunaClient
4
+ # Custom error class for rescuing from all KahunaClient errors
5
+ class Error < StandardError
6
+ attr_reader :status, :error_code, :error_message
7
+
8
+ def initialize(message, status=nil, data=nil)
9
+ super(message)
10
+ @status = status
11
+ if data
12
+ begin
13
+ json_hash = MultiJson.load(data, symbolize_keys: true)
14
+ @error_code = json_hash[:error_code] # should be the same as the HTTP status
15
+ @error_message = json_hash[:error]
16
+ rescue MultiJson::ParseError => exception
17
+ # nothing to do
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,47 @@
1
+ module KahunaClient
2
+ # Defines HTTP request methods
3
+ module Request
4
+ # Perform an HTTP GET request
5
+ def get(path, options={}, raw=false)
6
+ request(:get, path, options, raw)
7
+ end
8
+
9
+ # Perform an HTTP POST request
10
+ def post(path, options={}, raw=false)
11
+ request(:post, path, options, raw)
12
+ end
13
+
14
+ # Perform an HTTP PUT request
15
+ def put(path, options={}, raw=false)
16
+ request(:put, path, options, raw)
17
+ end
18
+
19
+ # Perform an HTTP DELETE request
20
+ def delete(path, options={}, raw=false)
21
+ request(:delete, path, options, raw)
22
+ end
23
+
24
+ private
25
+
26
+ # Perform an HTTP request
27
+ def request(method, path, options, raw=false)
28
+ new_options = options.dup
29
+ response = connection(raw).send(method) do |request|
30
+ case method
31
+ when :get, :delete
32
+ request.url(path, new_options)
33
+ when :post, :put
34
+ request.path = path
35
+ request.params = {:env => environment}
36
+ request.body = new_options.to_json unless new_options.empty?
37
+ end
38
+ end
39
+ if raw
40
+ response
41
+ else
42
+ response.body
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module KahunaClient
2
+ VERSION = "0.0.1".freeze unless defined?(::KahunaClient::VERSION)
3
+ end
@@ -0,0 +1,113 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Faraday::Response do
4
+ let(:dummy_payload) {
5
+ {
6
+ default_params: {},
7
+ default_config: {},
8
+ push_array: [],
9
+ }
10
+ }
11
+
12
+ before do
13
+ @client = KahunaClient::Client.new
14
+ end
15
+
16
+ context "Authentication Failed - HTTP status 401" do
17
+ before do
18
+ stub_post("api/push?env=s").
19
+ with(body: dummy_payload).
20
+ to_return(body: fixture("authentication_failed.json"), status: 401)
21
+ end
22
+
23
+ it "should raise a KahunaClient error" do
24
+ expect {
25
+ @client.push([])
26
+ }.to raise_error(KahunaClient::Error)
27
+ end
28
+
29
+ it "should raise a KahunaClient error with the proper attributes" do
30
+ begin
31
+ @client.push([])
32
+ rescue KahunaClient::Error => exception
33
+ expect(exception.status).to eq(401)
34
+ expect(exception.error_code).to eq(401)
35
+ expect(exception.error_message).to eq("Authentication failed")
36
+ end
37
+ end
38
+ end
39
+
40
+ context "User Not Found - HTTP status 404" do
41
+ before do
42
+ stub_post("api/push?env=s").
43
+ with(body: dummy_payload).
44
+ to_return(body: fixture("user_not_found.json"), status: 404)
45
+ end
46
+
47
+ it "should raise a KahunaClient error" do
48
+ expect {
49
+ @client.push([])
50
+ }.to raise_error(KahunaClient::Error)
51
+ end
52
+
53
+ it "should raise a KahunaClient error with the proper attributes" do
54
+ begin
55
+ @client.push([])
56
+ rescue KahunaClient::Error => exception
57
+ expect(exception.status).to eq(404)
58
+ expect(exception.error_code).to eq(404)
59
+ expect(exception.error_message).to eq("User not found")
60
+ end
61
+ end
62
+ end
63
+
64
+ context "User Not Enabled For Push - HTTP status 409" do
65
+ before do
66
+ stub_post("api/push?env=s").
67
+ with(body: dummy_payload).
68
+ to_return(body: fixture("user_not_enabled_for_push.json"), status: 409)
69
+ end
70
+
71
+ it "should raise a KahunaClient error" do
72
+ expect {
73
+ @client.push([])
74
+ }.to raise_error(KahunaClient::Error)
75
+ end
76
+
77
+ it "should raise a KahunaClient error with the proper attributes" do
78
+ begin
79
+ @client.push([])
80
+ rescue KahunaClient::Error => exception
81
+ expect(exception.status).to eq(409)
82
+ expect(exception.error_code).to eq(409)
83
+ expect(exception.error_message).to eq("User not enabled for push")
84
+ end
85
+ end
86
+ end
87
+
88
+ [400, 500].each do |status|
89
+ context "when HTTP status is #{status}" do
90
+ before do
91
+ stub_post("api/push?env=s").
92
+ with(body: dummy_payload).
93
+ to_return(body: "", status: status) # test with an empty response
94
+ end
95
+
96
+ it "should raise a KahunaClient error" do
97
+ expect {
98
+ @client.push([])
99
+ }.to raise_error(KahunaClient::Error)
100
+ end
101
+
102
+ it "should raise a KahunaClient error with the proper attributes" do
103
+ begin
104
+ @client.push([])
105
+ rescue KahunaClient::Error => exception
106
+ expect(exception.status).to eq(status)
107
+ expect(exception.error_code).to be_nil
108
+ expect(exception.error_message).to be_nil
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1 @@
1
+ {"error_code": 401, "success": false, "error": "Authentication failed"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "success": true
3
+ }
@@ -0,0 +1 @@
1
+ {"error_code": 409, "success": false, "error": "User not enabled for push"}
@@ -0,0 +1 @@
1
+ {"error_code": 404, "success": false, "error": "User not found"}
@@ -0,0 +1,65 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe KahunaClient::API do
4
+ before do
5
+ @keys = KahunaClient::Configuration::VALID_OPTIONS_KEYS
6
+ end
7
+
8
+ context "with module configuration" do
9
+ before do
10
+ KahunaClient.configure do |config|
11
+ @keys.each do |key|
12
+ config.send("#{key}=", key)
13
+ end
14
+ end
15
+ end
16
+
17
+ after do
18
+ KahunaClient.reset
19
+ end
20
+
21
+ it "should inherit module configuration" do
22
+ api = KahunaClient::API.new
23
+ @keys.each do |key|
24
+ expect(api.send(key)).to be(key)
25
+ end
26
+ end
27
+ end
28
+
29
+ context "with class configuration" do
30
+ before do
31
+ @configuration = {
32
+ :api_key => 'api key',
33
+ :secret_key => 'secret key',
34
+ :adapter => :typhoeus,
35
+ :endpoint => 'http://tumblr.com/',
36
+ :environment => 'p',
37
+ :proxy => 'http://shayne:sekret@proxy.example.com:8080',
38
+ :user_agent => 'Custom User Agent',
39
+ :debug => true
40
+ }
41
+ end
42
+
43
+ context "during initialization"
44
+
45
+ it "should override module configuration" do
46
+ api = KahunaClient::API.new(@configuration)
47
+ @keys.each do |key|
48
+ expect(api.send(key)).to be(@configuration[key])
49
+ end
50
+ end
51
+
52
+ context "after initilization" do
53
+
54
+ it "should override module configuration after initialization" do
55
+ api = KahunaClient::API.new
56
+ @configuration.each do |key, value|
57
+ api.send("#{key}=", value)
58
+ end
59
+ @keys.each do |key|
60
+ expect(api.send(key)).to be(@configuration[key])
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe KahunaClient::Client do
4
+
5
+ before do
6
+ @client = KahunaClient::Client.new(app_id: 'ID', app_key: 'KEY', environment: 'p')
7
+ end
8
+
9
+ describe ".push" do
10
+ let(:push_object) {
11
+ {
12
+ target: {
13
+ username: "iamawesome1989",
14
+ email: "awesome@mail.com",
15
+ fbid: "42",
16
+ user_id: "789"
17
+ },
18
+ notification: {
19
+ alert: "Look at this Push!"
20
+ },
21
+ params: {
22
+ sale_id: "1234",
23
+ landing_page: "share_page"
24
+ },
25
+ config: {
26
+ start_time: 1382652322,
27
+ optimal_hours: 8,
28
+ influence_rate_limiting: true,
29
+ observe_rate_limiting: true
30
+ }
31
+ }
32
+ }
33
+ let(:default_config) {
34
+ {
35
+ start_time: 1382652322,
36
+ optimal_hours: 4,
37
+ influence_rate_limiting: false,
38
+ observe_rate_limiting: false,
39
+ campaign_name: "New user campaign"
40
+ }
41
+ }
42
+ let(:default_params) {
43
+ {
44
+ sale_id: "1234",
45
+ landing_page: "share_page"
46
+ }
47
+ }
48
+ let(:payload) {
49
+ {
50
+ default_config: default_config,
51
+ default_params: default_params,
52
+ push_array: [push_object]
53
+ }
54
+ }
55
+ before do
56
+ # note here the env param
57
+ stub_post("api/push?env=p").
58
+ with(body: payload).
59
+ to_return(body: fixture("success.json"))
60
+ end
61
+
62
+ it "should get the correct resource" do
63
+ @client.push([push_object], default_params, default_config)
64
+ expect(a_post("api/push?env=p").with(body: payload)).to have_been_made
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe KahunaClient::Client do
4
+ it "should connect using the endpoint configuration" do
5
+ client = KahunaClient::Client.new
6
+ endpoint = URI.parse(client.endpoint)
7
+ connection = client.send(:connection).build_url(nil).to_s
8
+ expect(connection).to eq(endpoint.to_s)
9
+ end
10
+
11
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ describe KahunaClient do
4
+ after do
5
+ KahunaClient.reset
6
+ end
7
+
8
+ context "when delegating to a client" do
9
+
10
+ let(:push_object) {
11
+ {
12
+ target: {
13
+ username: "iamawesome1989",
14
+ email: "awesome@mail.com",
15
+ fbid: "42",
16
+ user_id: "789"
17
+ },
18
+ notification: {
19
+ alert: "Look at this Push!"
20
+ },
21
+ params: {
22
+ sale_id: "1234",
23
+ landing_page: "share_page"
24
+ },
25
+ config: {
26
+ start_time: 1382652322,
27
+ optimal_hours: 8,
28
+ influence_rate_limiting: true,
29
+ observe_rate_limiting: true
30
+ }
31
+ }
32
+ }
33
+ let(:payload) {
34
+ {
35
+ default_config:{},
36
+ default_params:{},
37
+ push_array: [push_object]
38
+ }
39
+ }
40
+ before do
41
+ stub_post("api/push?env=s").
42
+ with(body: payload).
43
+ to_return(body: fixture("success.json"))
44
+ end
45
+
46
+ it "should get the correct resource" do
47
+ KahunaClient.push([push_object], {}, {})
48
+ expect(a_post("api/push?env=s").
49
+ with(body: payload)).to have_been_made
50
+ end
51
+
52
+ it "should return the same results as a client" do
53
+ expect(KahunaClient.push([push_object])).to eq(KahunaClient::Client.new.push([push_object]))
54
+ end
55
+
56
+ end
57
+
58
+ describe ".client" do
59
+ it "should be a KahunaClient::Client" do
60
+ expect(KahunaClient.client).to be_a(KahunaClient::Client)
61
+ end
62
+ end
63
+
64
+ describe ".adapter" do
65
+ it "should return the default adapter" do
66
+ expect(KahunaClient.adapter).to be(KahunaClient::Configuration::DEFAULT_ADAPTER)
67
+ end
68
+ end
69
+
70
+ describe ".adapter=" do
71
+ it "should set the adapter" do
72
+ KahunaClient.adapter = :typhoeus
73
+ expect(KahunaClient.adapter).to be(:typhoeus)
74
+ end
75
+ end
76
+
77
+ describe ".endpoint" do
78
+ it "should return the default endpoint" do
79
+ expect(KahunaClient.endpoint).to eq(KahunaClient::Configuration::DEFAULT_ENDPOINT)
80
+ end
81
+ end
82
+
83
+ describe ".endpoint=" do
84
+ it "should set the endpoint" do
85
+ KahunaClient.endpoint = 'http://tumblr.com'
86
+ expect(KahunaClient.endpoint).to eq('http://tumblr.com')
87
+ end
88
+ end
89
+
90
+ describe ".user_agent" do
91
+ it "should return the default user agent" do
92
+ expect(KahunaClient.user_agent).to eq(KahunaClient::Configuration::DEFAULT_USER_AGENT)
93
+ end
94
+ end
95
+
96
+ describe ".user_agent=" do
97
+ it "should set the user_agent" do
98
+ KahunaClient.user_agent = 'Custom User Agent'
99
+ expect(KahunaClient.user_agent).to eq('Custom User Agent')
100
+ end
101
+ end
102
+
103
+ describe ".environment" do
104
+ it "should return the default environment" do
105
+ expect(KahunaClient.environment).to eq(KahunaClient::Configuration::DEFAULT_ENVIRONMENT)
106
+ end
107
+ end
108
+
109
+ describe ".environment=" do
110
+ it "should set the environment" do
111
+ KahunaClient.environment = 'p'
112
+ expect(KahunaClient.environment).to eq('p')
113
+ end
114
+ end
115
+
116
+ describe ".configure" do
117
+
118
+ KahunaClient::Configuration::VALID_OPTIONS_KEYS.each do |key|
119
+
120
+ it "should set the #{key}" do
121
+ KahunaClient.configure do |config|
122
+ config.send("#{key}=", key)
123
+ expect(KahunaClient.send(key)).to eq(key)
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ end
@@ -0,0 +1,50 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'kahuna_client'
3
+ require 'rspec'
4
+ require 'webmock/rspec'
5
+
6
+ RSpec.configure do |config|
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.include WebMock::API
10
+ end
11
+
12
+ def a_delete(path)
13
+ a_request(:delete, KahunaClient.endpoint + path)
14
+ end
15
+
16
+ def a_get(path)
17
+ a_request(:get, KahunaClient.endpoint + path)
18
+ end
19
+
20
+ def a_post(path)
21
+ a_request(:post, KahunaClient.endpoint + path)
22
+ end
23
+
24
+ def a_put(path)
25
+ a_request(:put, KahunaClient.endpoint + path)
26
+ end
27
+
28
+ def stub_delete(path)
29
+ stub_request(:delete, KahunaClient.endpoint + path)
30
+ end
31
+
32
+ def stub_get(path)
33
+ stub_request(:get, KahunaClient.endpoint + path)
34
+ end
35
+
36
+ def stub_post(path)
37
+ stub_request(:post, KahunaClient.endpoint + path)
38
+ end
39
+
40
+ def stub_put(path)
41
+ stub_request(:put, KahunaClient.endpoint + path)
42
+ end
43
+
44
+ def fixture_path
45
+ File.expand_path("../fixtures", __FILE__)
46
+ end
47
+
48
+ def fixture(file)
49
+ File.new(fixture_path + '/' + file)
50
+ end
metadata ADDED
@@ -0,0 +1,228 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kahuna_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Edgar Gonzalez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
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: webmock
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: yard
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
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: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bluecloth
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
+ - !ruby/object:Gem::Dependency
95
+ name: faraday
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 0.8.7
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.8.7
110
+ - !ruby/object:Gem::Dependency
111
+ name: faraday_middleware
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.9.1
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.9.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: multi_json
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 1.10.0
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 1.10.0
142
+ - !ruby/object:Gem::Dependency
143
+ name: hashie
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :runtime
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: A ruby wrapper for Kahuna Push API
159
+ email:
160
+ - edgargonzalez@gmail.com
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files: []
164
+ files:
165
+ - .gitignore
166
+ - .travis.yml
167
+ - Gemfile
168
+ - LICENSE
169
+ - README.md
170
+ - Rakefile
171
+ - kahuna_client.gemspec
172
+ - lib/faraday/raise_http_exception.rb
173
+ - lib/kahuna_client.rb
174
+ - lib/kahuna_client/api.rb
175
+ - lib/kahuna_client/client.rb
176
+ - lib/kahuna_client/client/push.rb
177
+ - lib/kahuna_client/configuration.rb
178
+ - lib/kahuna_client/connection.rb
179
+ - lib/kahuna_client/error.rb
180
+ - lib/kahuna_client/request.rb
181
+ - lib/kahuna_client/version.rb
182
+ - spec/faraday/response_spec.rb
183
+ - spec/fixtures/authentication_failed.json
184
+ - spec/fixtures/success.json
185
+ - spec/fixtures/user_not_enabled_for_push.json
186
+ - spec/fixtures/user_not_found.json
187
+ - spec/kahuna_client/api_spec.rb
188
+ - spec/kahuna_client/client/push_spec.rb
189
+ - spec/kahuna_client/client_spec.rb
190
+ - spec/kahuna_client_spec.rb
191
+ - spec/spec_helper.rb
192
+ homepage: http://github.com/edgar/kahuna_client
193
+ licenses:
194
+ - MIT
195
+ post_install_message:
196
+ rdoc_options: []
197
+ require_paths:
198
+ - lib
199
+ required_ruby_version: !ruby/object:Gem::Requirement
200
+ none: false
201
+ requirements:
202
+ - - ! '>='
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ none: false
207
+ requirements:
208
+ - - ! '>='
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ requirements: []
212
+ rubyforge_project:
213
+ rubygems_version: 1.8.25
214
+ signing_key:
215
+ specification_version: 3
216
+ summary: A ruby wrapper for Kahuna Push API
217
+ test_files:
218
+ - spec/faraday/response_spec.rb
219
+ - spec/fixtures/authentication_failed.json
220
+ - spec/fixtures/success.json
221
+ - spec/fixtures/user_not_enabled_for_push.json
222
+ - spec/fixtures/user_not_found.json
223
+ - spec/kahuna_client/api_spec.rb
224
+ - spec/kahuna_client/client/push_spec.rb
225
+ - spec/kahuna_client/client_spec.rb
226
+ - spec/kahuna_client_spec.rb
227
+ - spec/spec_helper.rb
228
+ has_rdoc: