warbler-ruby 1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0ebbd99626d73ae5eb8e026fbd89b968079c274
4
+ data.tar.gz: c622ce6e5905364ecd3a7b7df0ed739f9d213878
5
+ SHA512:
6
+ metadata.gz: be6a456f449e2dc75d04e3f936dc7f9f4b3d9cba8fc4a78256c923dd6e4bfe703918cd35790425ccefdd990d267dca33020b04b7e0388fb839586edeaa7c6960
7
+ data.tar.gz: fa7ecfcb16746ec5ed1836346a1bf8e13edc809785297ae2d681f15fd9b642d003756a609a9f5d914545c190c65ddbfddb39f7576f17abaae5d47efc5e351386
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ License:
2
+ ========
3
+ The MIT License (MIT)
4
+ http://opensource.org/licenses/MIT
5
+
6
+ Copyright (c) 2014 APIMATIC Limited
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in
16
+ all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ THE SOFTWARE.
25
+
26
+ Trade Mark:
27
+ ==========
28
+ APIMATIC is a trade mark for APIMATIC Limited
@@ -0,0 +1,76 @@
1
+ # Warbler Ruby Client Library
2
+
3
+ The Warbler gem lets you integrate with Warbler to track activities, reward customers and let them redeem rewards without leaving your application.
4
+
5
+ ## Dependencies
6
+
7
+ * unirest
8
+
9
+ ## Installaction
10
+
11
+ ```bash
12
+ gem install warbler
13
+ ```
14
+
15
+ Or add it to your gemfile
16
+
17
+ ```ruby
18
+ gem 'warbler'
19
+ ```
20
+
21
+ ## Quick Start Example
22
+
23
+ 1. Sign up for a sandbox account on https://www.warblersandbox.com
24
+
25
+ 2. Get your `merchant_token` from the 'Authorization Tokens' area available under 'Settings'
26
+
27
+ 3. Define an initializer holding the following values
28
+
29
+ ```ruby
30
+ Warbler::Configuration.merchant_token = xxxxx # merchant_token from step 2
31
+ Warbler::Configuration.mode = 'sandbox' # or 'production'
32
+ Warbler::Configuration.device_identifier = 'webapp'
33
+ ```
34
+
35
+ 4. Define the methods you'll use accross the app
36
+
37
+ ```ruby
38
+ module Warblerable
39
+ def identify_warbler_user(user)
40
+ session[:device_token] = Warbler::UsersController.new.identify(user.email)['device_token']
41
+ end
42
+
43
+ def track_warbler_activity(activity_slug)
44
+ Warbler::ActivitiesController.new(session[:device_token]).track(activity_slug)
45
+ end
46
+
47
+ def list_warbler_rewards
48
+ Warbler::RewardsController.new(session[:device_token]).list
49
+ end
50
+
51
+ def show_warbler_reward(reward_id)
52
+ Warbler::RewardsController.new(session[:device_token]).show(reward_id)
53
+ end
54
+
55
+ def redeem_warbler_reward(reward_id)
56
+ Warbler::RewardsController.new(session[:device_token]).redeem(reward_id)
57
+ rescue Warbler::APIException => e
58
+ redirect_to :back, alert: JSON.parse(e.message).to_sentence
59
+ end
60
+ end
61
+ ```
62
+
63
+ 5. Use the methods defined in the previous step
64
+
65
+ ## Sample Project
66
+
67
+ Check out our [Sample Ruby on Rails project](https://github.com/warblerdigital/warbler-ruby-sample).
68
+
69
+ ## Help
70
+
71
+ If you have any questions feel free to email us at hello@warbler.com
72
+ We are happy to help.
73
+
74
+ ## Licence
75
+
76
+ See the LICENCE file.
@@ -0,0 +1,20 @@
1
+ # This file was automatically generated by APIMATIC BETA v2.0 on 03/02/2016
2
+ require 'openssl'
3
+ require 'json'
4
+ require 'unirest'
5
+
6
+ # APIMATIC Helper Files
7
+ require 'warbler/api_helper.rb'
8
+ require 'warbler/api_exception.rb'
9
+ require 'warbler/configuration.rb'
10
+
11
+ # Controllers
12
+ require 'warbler/controllers/application_controller.rb'
13
+ require 'warbler/controllers/users_controller.rb'
14
+ require 'warbler/controllers/activities_controller.rb'
15
+ require 'warbler/controllers/rewards_controller.rb'
16
+
17
+ # Models
18
+
19
+ # Warbler Overrides
20
+ require 'warbler/authorization.rb'
@@ -0,0 +1,23 @@
1
+ # This file was automatically generated by APIMATIC BETA v2.0 on 03/02/2016
2
+
3
+ module Warbler
4
+ class APIException < StandardError
5
+
6
+ # value store
7
+ attr_reader :response_code
8
+
9
+ # value store
10
+ attr_reader :response_body
11
+
12
+ # The HTTP response code from the API request
13
+ # @param [String] the reason for raising an exception
14
+ # @param [Numeric] the HTTP response code from the API request
15
+ # @param [Object] the HTTP unprased response from the API request
16
+ def initialize(reason, response_code, response_body)
17
+ super(reason)
18
+ @response_code = response_code
19
+ @response_body = response_body
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,82 @@
1
+ # This file was automatically generated by APIMATIC BETA v2.0 on 03/02/2016
2
+
3
+ module Warbler
4
+ class APIHelper
5
+
6
+ # Replaces template parameters in the given url
7
+ # @param [String] The query string builder to replace the template parameters
8
+ # @param [Array] The parameters to replace in the url
9
+ def self.append_url_with_template_parameters(query_builder, parameters)
10
+ # perform parameter validation
11
+ raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.is_a? String
12
+
13
+ # return if there are no parameters to replace
14
+ if parameters.nil? then
15
+ abort('no parameters to append')
16
+ end
17
+
18
+ # iterate and append parameters
19
+ parameters.map do |key, value|
20
+ replace_value = ''
21
+
22
+ if value.nil?
23
+ replace_value = ''
24
+ elsif value.is_a? Enumerable
25
+ replace_value = value.join("/")
26
+ else
27
+ replace_value = value.to_s
28
+ end
29
+
30
+ # find the template parameter and replace it with its value
31
+ query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
32
+ end
33
+
34
+ query_builder
35
+ end
36
+
37
+ # Appends the given set of parameters to the given query string
38
+ # @param [String] The query string builder to replace the template parameters
39
+ # @param [Array] The parameters to append
40
+ def self.append_url_with_query_parameters(query_builder, parameters)
41
+ # perform parameter validation
42
+ raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.is_a? String
43
+
44
+ # return if there are no parameters to replace
45
+ if parameters.nil? then
46
+ abort('no parameters to append')
47
+ end
48
+
49
+ #remove any nil values
50
+ parameters = parameters.reject {|key, value| value.nil?}
51
+
52
+ # does the query string already has parameters
53
+ has_params = query_builder.include? '?'
54
+ separator = if has_params then '&' else '?' end
55
+
56
+ # append query with separator and parameters
57
+ query_builder << separator << URI.unescape(URI.encode_www_form(parameters))
58
+ end
59
+
60
+ # Validates and processes the given Url
61
+ # @param [String] The given Url to process
62
+ # @return [String] Pre-processed Url as string
63
+ def self.clean_url(url)
64
+ # perform parameter validation
65
+ raise ArgumentError, 'Invalid Url.' unless url.is_a? String
66
+
67
+ # ensure that the urls are absolute
68
+ matches = url.match(/^(https?:\/\/[^\/]+)/)
69
+ raise ArgumentError, 'Invalid Url format.' if matches.nil?
70
+
71
+ # get the http protocol match
72
+ protocol = matches[1]
73
+
74
+ # remove redundant forward slashes
75
+ query = url[protocol.length..-1].gsub(/\/\/+/, '/')
76
+
77
+ # return process url
78
+ return protocol + query;
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,58 @@
1
+ require 'securerandom'
2
+ require 'addressable/uri'
3
+
4
+ module Warbler
5
+ class Authorization
6
+ def initialize(method, url, device_token = nil)
7
+ @method = method.to_s.upcase
8
+ @url = Addressable::URI.parse(url)
9
+ @device_token = device_token
10
+ end
11
+
12
+ def header
13
+ [ base_header, header_params ].compact.join(', ')
14
+ end
15
+
16
+ private
17
+
18
+ def base_header
19
+ "Token token=\"#{ Configuration.merchant_token }\""
20
+ end
21
+
22
+ def header_params
23
+ "nonce=\"#{ nonce }\", timestamp=\"#{ timestamp }\", auth=\"#{ signature }\"" if @device_token
24
+ end
25
+
26
+ def nonce
27
+ @nonce ||= SecureRandom.hex(8)
28
+ end
29
+
30
+ def timestamp
31
+ @timestamp ||= Time.now.to_i
32
+ end
33
+
34
+ def merchant_token
35
+ Configuration.merchant_token
36
+ end
37
+
38
+ def message
39
+ "#{ @method },#{ @url.host }:#{ port },nonce=#{ nonce },timestamp=#{ timestamp },#{ @url.path }"
40
+ end
41
+
42
+ def port
43
+ ('https' == @url.scheme) ? 443 : 80
44
+ end
45
+
46
+ def signature
47
+ Base64.strict_encode64(hmac)
48
+ end
49
+
50
+ def hmac
51
+ OpenSSL::HMAC.digest(digest, @device_token.encode('ASCII'), message.encode('ASCII'))
52
+ end
53
+
54
+ def digest
55
+ @digest ||= OpenSSL::Digest.new('sha1')
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,15 @@
1
+ # This file was automatically generated by APIMATIC BETA v2.0 on 03/02/2016
2
+
3
+ module Warbler
4
+ class Configuration
5
+ @mode = ''
6
+ @merchant_token = ''
7
+ @device_identifier = ''
8
+
9
+ class << self
10
+ attr_accessor :mode
11
+ attr_accessor :merchant_token
12
+ attr_accessor :device_identifier
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ # This file was automatically generated by APIMATIC BETA v2.0 on 03/02/2016
2
+
3
+ module Warbler
4
+ class ActivitiesController < ApplicationController
5
+
6
+ # This endpoint lets you track the actions your users perform.
7
+ # @param [String] activity_type Required parameter: The type of activity the user is performing. Example: 'purchased-a-product'
8
+ # @param [String] record_id Optional parameter: The ID for the record associated with the activity in your database.
9
+ # @return mixed response from the API call
10
+ def track activity_type, record_id: nil
11
+ # the base uri for api requests
12
+ query_builder = base_uri
13
+
14
+ # prepare query string for API call
15
+ query_builder << "/v3/activities"
16
+
17
+ # process optional query parameters
18
+ query_builder = APIHelper.append_url_with_query_parameters query_builder, {
19
+ "activity_type" => activity_type,
20
+ "record_id" => record_id,
21
+ }
22
+
23
+ perform_query('post', query_builder)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ module Warbler
2
+ class ApplicationController
3
+ def initialize(device_token = nil)
4
+ @device_token = device_token
5
+ end
6
+
7
+ private
8
+
9
+ def base_uri
10
+ Configuration.mode == 'production' ? 'https://api.warbler.com' : 'https://api.warblersandbox.com'
11
+ end
12
+
13
+ def headers(http_verb, query)
14
+ {
15
+ "User-Agent" => "APIMATIC 2.0",
16
+ "Accept" => "application/json",
17
+ "Content-Type" => "application/json",
18
+ "X-Device-Identifier" => Configuration.device_identifier,
19
+ "Authorization" => Authorization.new(http_verb.to_sym, APIHelper.clean_url(query), @device_token).header
20
+ }
21
+ end
22
+
23
+ def perform_query(http_verb, query)
24
+ # Invoke the API call request to fetch the response
25
+ response = Unirest.send(http_verb, query, headers: headers(http_verb, query))
26
+
27
+ # Handle errors using HTTP status codes
28
+ if !(response.code.between?(200,206)) # [200,206] = HTTP OK
29
+ raise APIException.new response.body['detail'], response.code, response.raw_body
30
+ end
31
+
32
+ # Return the response
33
+ response.body
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,84 @@
1
+ # This file was automatically generated by APIMATIC BETA v2.0 on 03/02/2016
2
+
3
+ module Warbler
4
+ class RewardsController < ApplicationController
5
+
6
+ # List all the available rewards.
7
+ # @param [Numeric] category_id Optional parameter: The id of the category to filter rewards by
8
+ # @param [Numeric] limit Optional parameter: The number of rewards you want to be retrieved.
9
+ # @param [Numeric] offset Optional parameter: The number of rewards you want to skip before starting the retrieval.
10
+ # @return mixed response from the API call
11
+ def list category_id: nil, limit: 25, offset: 0
12
+ # the base uri for api requests
13
+ query_builder = base_uri
14
+
15
+ # prepare query string for API call
16
+ query_builder << "/v3/rewards"
17
+
18
+ # process optional query parameters
19
+ query_builder = APIHelper.append_url_with_query_parameters query_builder, {
20
+ "category_id" => category_id,
21
+ "limit" => if limit.nil? then 25 else limit end,
22
+ "offset" => if offset.nil? then 0 else offset end,
23
+ }
24
+
25
+ perform_query('get', query_builder)
26
+ end
27
+
28
+ # Bid on a reward auction.
29
+ # @param [String] amount Required parameter: Can be either 'max' (when max bidding) or the number of points the user wants to bid.
30
+ # @param [Numeric] id Required parameter: Id of the reward
31
+ # @return mixed response from the API call
32
+ def bid amount, id
33
+ # the base uri for api requests
34
+ query_builder = base_uri
35
+
36
+ # prepare query string for API call
37
+ query_builder << "/v3/rewards/#{ id }/bids"
38
+
39
+ # process optional query parameters
40
+ query_builder = APIHelper.append_url_with_template_parameters query_builder, {
41
+ "id" => id,
42
+ "amount" => amount
43
+ }
44
+
45
+ perform_query('post', query_builder)
46
+ end
47
+
48
+ # Show a reward's details.
49
+ # @param [Numeric] id Required parameter: Id of the reward
50
+ # @return mixed response from the API call
51
+ def show id
52
+ # the base uri for api requests
53
+ query_builder = base_uri
54
+
55
+ # prepare query string for API call
56
+ query_builder << "/v3/rewards/#{ id }"
57
+
58
+ # process optional query parameters
59
+ query_builder = APIHelper.append_url_with_template_parameters query_builder, {
60
+ "id" => id,
61
+ }
62
+
63
+ perform_query('get', query_builder)
64
+ end
65
+
66
+ # Redeem a reward.
67
+ # @param [Numeric] id Required parameter: Id of the reward
68
+ # @return mixed response from the API call
69
+ def redeem id
70
+ # the base uri for api requests
71
+ query_builder = base_uri
72
+
73
+ # prepare query string for API call
74
+ query_builder << "/v3/rewards/#{ id }/redemptions"
75
+
76
+ # process optional query parameters
77
+ query_builder = APIHelper.append_url_with_template_parameters query_builder, {
78
+ "id" => id,
79
+ }
80
+
81
+ perform_query('post', query_builder)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,28 @@
1
+ # This file was automatically generated by APIMATIC BETA v2.0 on 03/02/2016
2
+
3
+ module Warbler
4
+ class UsersController < ApplicationController
5
+
6
+ # This endpoint lets you tie a user with his/her activities. You’ll want to identify a user with any relevant information as soon as they log-in or sign-up.
7
+ # @param [String] email Required parameter: The user's email address
8
+ # @param [String] first_name Optional parameter: The user's first name
9
+ # @param [String] last_name Optional parameter: The user's last name
10
+ # @return mixed response from the API call
11
+ def identify email, first_name: nil, last_name: nil
12
+ # the base uri for api requests
13
+ query_builder = base_uri
14
+
15
+ # prepare query string for API call
16
+ query_builder << "/v3/users"
17
+
18
+ # process optional query parameters
19
+ query_builder = APIHelper.append_url_with_query_parameters query_builder, {
20
+ "email" => CGI::escape(email),
21
+ "first_name" => first_name,
22
+ "last_name" => last_name,
23
+ }
24
+
25
+ perform_query('post', query_builder)
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: warbler-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - APIMATIC
8
+ - Zeeshan Ejaz Bhatti
9
+ - Vero Rebagliatte
10
+ - Gustavo Beathyate
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2016-03-23 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: unirest
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '1.1'
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 1.1.2
26
+ type: :runtime
27
+ prerelease: false
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.1'
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 1.1.2
36
+ description: The warbler gem lets you integrate with Warbler to track activities,
37
+ reward customers and let them redeem rewards without leaving your application.
38
+ email: hello@warbler.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - LICENSE
44
+ - README.md
45
+ - lib/warbler-ruby.rb
46
+ - lib/warbler/api_exception.rb
47
+ - lib/warbler/api_helper.rb
48
+ - lib/warbler/authorization.rb
49
+ - lib/warbler/configuration.rb
50
+ - lib/warbler/controllers/activities_controller.rb
51
+ - lib/warbler/controllers/application_controller.rb
52
+ - lib/warbler/controllers/rewards_controller.rb
53
+ - lib/warbler/controllers/users_controller.rb
54
+ homepage: https://www.warbler.com
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '2.0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.4.8
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: The warbler gem lets you integrate with Warbler
78
+ test_files: []