retreaverjs-rails 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # jsdocs
2
+
3
+ https://retreaver.com/documentation/javascript/v1/Retreaver.Number.html
4
+
5
+
6
+ # Usage
7
+
8
+ Include the retreaver.js script on the page.
9
+
10
+ ```
11
+ <script src="/path/to/retreaver.js" type="text/javascript"></script>
12
+ ```
13
+
14
+
15
+ # Examples
16
+
17
+ ### Campaign
18
+
19
+ Initialize a campaign that will return numbers matching home_value:50000
20
+
21
+ ```javascript
22
+ var campaign = new Retreaver.Campaign({ campaign_key: 'the_key_example' });
23
+ ```
24
+
25
+ Request a number from the campaign matching tags, and use jQuery to display it on the page.
26
+
27
+ ```javascript
28
+ var tags = {
29
+ home_value: '50000'
30
+ };
31
+ campaign.request_number(tags, function (number) {
32
+ $('#number_on_page').html( number.get('formatted_number') );
33
+ });
34
+ ```
35
+
36
+
37
+ ### Number
38
+
39
+ ##### Tags
40
+
41
+ Display the tags currently attached to this number.
42
+
43
+ ```javascript
44
+ number.get('tag_values');
45
+ ```
46
+
47
+ Add / Remove / Clear Tags
48
+
49
+ ```javascript
50
+ number.add_tags({'date_of_birth': '1980/01/01'});
51
+ number.remove_tags({'date_of_birth': '1980/01/01'});
52
+ number.clear_tags();
53
+ ```
54
+
55
+ ##### Release
56
+
57
+ When you are finished with a number you can release it back to the campaign.
58
+
59
+ ```javascript
60
+ number.release();
61
+ ```
62
+
63
+ ##### Initiate Call
64
+
65
+ You can initiate a call with the user.
66
+
67
+ ```javascript
68
+ number.initiate_call('1112224444', {}, function(call){
69
+ // Call initiated
70
+ });
71
+ ```
72
+
73
+ ##### Attributes
74
+
75
+ Numbers have attributes that can be retrieved.
76
+
77
+ ```javascript
78
+ number.get('campaign_key');
79
+ ```
80
+
81
+ | Attribute | Type | Description |
82
+ | ----------------- |:---------------:|:-------------------------------------------------------------------:|
83
+ | id | numeric | number id |
84
+ | campaign_key | alpha-numeric | campaign key |
85
+ | formatted_number | string | formatted number according to national style `(866) 898-7878` |
86
+ | number | string | E.164 formatted number `+18668987878` |
87
+ | plain_number | string | unformatted phone number digits `8668987878` |
88
+ | target_open | boolean | does this number have targets that are available to take calls? |
89
+ | is_per_visitor | boolean | does this number track unique visitors? |
90
+ | tag_values | object | the tags currently assigned to this number |
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require 'fileutils'
3
+ require_relative 'lib/retreaverjs/compile.rb'
4
+
5
+ namespace :retreaverjs do
6
+ desc "compile retreaverjs"
7
+ task :compile do
8
+ Retreaver::Compile.perform
9
+ end
10
+ end
11
+
12
+ task :before_build do
13
+ Rake::Task["retreaverjs:compile"].execute
14
+ end
15
+
16
+ task :build => :before_build
data/config/README ADDED
@@ -0,0 +1 @@
1
+ See our <a href="http://support.retreaver.com/retreaver-js/">knowledge base article</a> for more information.
@@ -0,0 +1,26 @@
1
+ {
2
+ "tags": {
3
+ "allowUnknownTags": true
4
+ },
5
+ "source": {
6
+ "include": [
7
+ "config/README",
8
+ "src/retreaver/number.js",
9
+ "src/retreaver/campaign.js"
10
+ ],
11
+ "exclude": [],
12
+ "includePattern": ".+\\.js(doc)?$",
13
+ "excludePattern": "(^|\\/|\\\\)_"
14
+ },
15
+ "opts": {
16
+ "destination": "./vendor/documentation/javascript/v1/"
17
+ },
18
+ "plugins": [],
19
+ "templates": {
20
+ "cleverLinks": false,
21
+ "monospaceLinks": false,
22
+ "default": {
23
+ "outputSourceFiles": true
24
+ }
25
+ }
26
+ }
@@ -0,0 +1,32 @@
1
+ module Retreaver
2
+ class Compile
3
+
4
+ class << self
5
+ def perform
6
+ Dir.chdir(root) do |f|
7
+ run("npm install") unless Dir.exist?('./node_modules')
8
+ # compile src
9
+ run("grunt")
10
+ output = 'vendor/assets/javascripts/'
11
+ FileUtils.rm_rf(output)
12
+ FileUtils.mkdir_p(output)
13
+ Dir.glob('dist/*.js'){|f| FileUtils.cp( f, output ) }
14
+ # generate jsdocs
15
+ output = 'vendor/documentation/javascripts/'
16
+ FileUtils.rm_rf(output)
17
+ run("./node_modules/.bin/jsdoc -c config/jsdocs.json")
18
+ end
19
+ end
20
+
21
+ def run(command)
22
+ puts(command)
23
+ system(command)
24
+ end
25
+
26
+ def root
27
+ File.expand_path(File.join(__FILE__, '../../../'))
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Retreaver
2
+ module Rails
3
+ VERSION = '0.0.12'
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "retreaverjs/rails/version"
2
+
3
+ module Retreaver
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
data/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "retreaver",
3
+ "description": "Retreaver Javascript API",
4
+ "version": "0.1.0",
5
+ "homepage": "https://github.com/retreaver/retreaverjs",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/retreaver/retreaverjs.git"
9
+ },
10
+ "bugs": {
11
+ "url": "https://github.com/retreaver/retreaverjs/issues"
12
+ },
13
+ "author": {
14
+ "name": "Blake Hilscher",
15
+ "email": "blake@retreaver.com",
16
+ "url": "http://retreaver.com/"
17
+ },
18
+ "contributors": [
19
+ {
20
+ "name": "Blake Hilscher",
21
+ "email": "blake@hilscher.ca",
22
+ "url": "http://blake.hilscher.ca/"
23
+ }
24
+ ],
25
+ "licenses": [
26
+ {
27
+ "type": "GPLv3",
28
+ "url": "http://github.com/retreaver/retreaverjs/blob/master/LICENSE"
29
+ }
30
+ ],
31
+ "main": "src/package.js",
32
+ "engines": {
33
+ "node": ">= 0.6.x"
34
+ },
35
+ "dependencies": {
36
+ },
37
+ "devDependencies": {
38
+ "grunt": "~0.4.5",
39
+ "jsdoc": "~3.2.2",
40
+ "grunt-contrib-jshint": "~0.10.0",
41
+ "grunt-contrib-nodeunit": "~0.4.1",
42
+ "grunt-contrib-uglify": "~0.5.0",
43
+ "grunt-contrib-watch": "~0.6.1",
44
+ "grunt-contrib-concat": "~0.4.0"
45
+ }
46
+ }
@@ -0,0 +1,126 @@
1
+ // http://www.webtoolkit.info/javascript-base64.html#.U-qwzYBdUwQ
2
+ (function () {
3
+ var Base64 = {
4
+ _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
5
+ };
6
+ // public method for encoding
7
+ Base64.encode = function (input) {
8
+ var output = "";
9
+ var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
10
+ var i = 0;
11
+
12
+ input = Base64._utf8_encode(input);
13
+
14
+ while (i < input.length) {
15
+
16
+ chr1 = input.charCodeAt(i++);
17
+ chr2 = input.charCodeAt(i++);
18
+ chr3 = input.charCodeAt(i++);
19
+
20
+ enc1 = chr1 >> 2;
21
+ enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
22
+ enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
23
+ enc4 = chr3 & 63;
24
+
25
+ if (isNaN(chr2)) {
26
+ enc3 = enc4 = 64;
27
+ } else if (isNaN(chr3)) {
28
+ enc4 = 64;
29
+ }
30
+ output = output +
31
+ Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) +
32
+ Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);
33
+
34
+ }
35
+ return output;
36
+ };
37
+ Base64.decode = function (input) {
38
+ var output = "";
39
+ var chr1, chr2, chr3;
40
+ var enc1, enc2, enc3, enc4;
41
+ var i = 0;
42
+
43
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
44
+
45
+ while (i < input.length) {
46
+
47
+ enc1 = Base64._keyStr.indexOf(input.charAt(i++));
48
+ enc2 = Base64._keyStr.indexOf(input.charAt(i++));
49
+ enc3 = Base64._keyStr.indexOf(input.charAt(i++));
50
+ enc4 = Base64._keyStr.indexOf(input.charAt(i++));
51
+
52
+ chr1 = (enc1 << 2) | (enc2 >> 4);
53
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
54
+ chr3 = ((enc3 & 3) << 6) | enc4;
55
+
56
+ output = output + String.fromCharCode(chr1);
57
+
58
+ if (enc3 != 64) {
59
+ output = output + String.fromCharCode(chr2);
60
+ }
61
+ if (enc4 != 64) {
62
+ output = output + String.fromCharCode(chr3);
63
+ }
64
+
65
+ }
66
+
67
+ output = Base64._utf8_decode(output);
68
+
69
+ return output;
70
+ };
71
+ Base64._utf8_encode = function (string) {
72
+ //REGEX_2: /\r\n/g NEWLINE: "\n"
73
+ string = string.replace(eval("/\\r\\n/g"),eval("String('\\n')"));
74
+ var utftext = "";
75
+
76
+ for (var n = 0; n < string.length; n++) {
77
+
78
+ var c = string.charCodeAt(n);
79
+
80
+ if (c < 128) {
81
+ utftext += String.fromCharCode(c);
82
+ }
83
+ else if ((c > 127) && (c < 2048)) {
84
+ utftext += String.fromCharCode((c >> 6) | 192);
85
+ utftext += String.fromCharCode((c & 63) | 128);
86
+ }
87
+ else {
88
+ utftext += String.fromCharCode((c >> 12) | 224);
89
+ utftext += String.fromCharCode(((c >> 6) & 63) | 128);
90
+ utftext += String.fromCharCode((c & 63) | 128);
91
+ }
92
+
93
+ }
94
+
95
+ return utftext;
96
+ };
97
+ Base64._utf8_decode = function (utftext) {
98
+ var string = "";
99
+ var i = 0;
100
+ var c = c1 = c2 = 0;
101
+
102
+ while (i < utftext.length) {
103
+
104
+ c = utftext.charCodeAt(i);
105
+
106
+ if (c < 128) {
107
+ string += String.fromCharCode(c);
108
+ i++;
109
+ }
110
+ else if ((c > 191) && (c < 224)) {
111
+ c2 = utftext.charCodeAt(i + 1);
112
+ string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
113
+ i += 2;
114
+ }
115
+ else {
116
+ c2 = utftext.charCodeAt(i + 1);
117
+ c3 = utftext.charCodeAt(i + 2);
118
+ string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
119
+ i += 3;
120
+ }
121
+
122
+ }
123
+ return string;
124
+ };
125
+ Retreaver.Base.Base64 = Base64;
126
+ })();
@@ -0,0 +1,59 @@
1
+ // https://github.com/evertton/cookiejs
2
+ (function (f) {
3
+ var a = function (b, c, d) {
4
+ return 1 === arguments.length ? a.get(b) : a.set(b, c, d)
5
+ };
6
+ a._document = document;
7
+ a._navigator = navigator;
8
+ a.defaults = {path: "/"};
9
+ a.get = function (b) {
10
+ a._cachedDocumentCookie !== a._document.cookie && a._renewCache();
11
+ return a._cache[b]
12
+ };
13
+ a.set = function (b, c, d) {
14
+ d = a._getExtendedOptions(d);
15
+ a._document.cookie = a._generateCookieString(b, c, d);
16
+ return a
17
+ };
18
+ a._getExtendedOptions = function (b) {
19
+ return{path: b && b.path || a.defaults.path, domain: b && b.domain || a.defaults.domain, secure: b && b.secure !== f ? b.secure :
20
+ a.defaults.secure}
21
+ };
22
+ a._isValidDate = function (a) {
23
+ return"[object Date]" === Object.prototype.toString.call(a) && !isNaN(a.getTime())
24
+ };
25
+ a._generateCookieString = function (a, c, d) {
26
+ a = encodeURIComponent(a);
27
+ c = (c + "").replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);
28
+ d = d || {};
29
+ a = a + "=" + c + (d.path ? ";path=" + d.path : "");
30
+ a += d.domain ? ";domain=" + d.domain : "";
31
+ return a += d.secure ? ";secure" : ""
32
+ };
33
+ a._getCookieObjectFromString = function (b) {
34
+ var c = {};
35
+ b = b ? b.split("; ") : [];
36
+ for (var d = 0; d < b.length; d++) {
37
+ var e = a._getKeyValuePairFromCookieString(b[d]);
38
+ c[e.key] === f && (c[e.key] = e.value)
39
+ }
40
+ return c
41
+ };
42
+ a._getKeyValuePairFromCookieString = function (a) {
43
+ var c = a.indexOf("="), c = 0 > c ? a.length : c;
44
+ return{key: decodeURIComponent(a.substr(0, c)), value: decodeURIComponent(a.substr(c + 1))}
45
+ };
46
+ a._renewCache = function () {
47
+ a._cache = a._getCookieObjectFromString(a._document.cookie);
48
+ a._cachedDocumentCookie = a._document.cookie
49
+ };
50
+ a._areEnabled = function () {
51
+ return a._navigator.cookieEnabled || "1" === a.set("cookies.js", 1).get("cookies.js")
52
+ };
53
+ a.enabled = a._areEnabled();
54
+ "function" === typeof define &&
55
+ define.amd ? define(function () {
56
+ return a
57
+ }) : "undefined" !== typeof exports ? ("undefined" !== typeof module && module.exports && (exports = module.exports = a), exports.Cookies = a) : a;
58
+ Retreaver.Base.Cookies = a;
59
+ })();
@@ -0,0 +1,81 @@
1
+ (function () {
2
+ // Dependencies
3
+ var Base = Retreaver.Base;
4
+ /**
5
+ * @constructor
6
+ * @memberof Retreaver.Base
7
+ * @param {Object} config - Configuration hash
8
+ * @param {String} config.type - The data type
9
+ * @param {Numeric} config.primary_key - The primary_key
10
+ */
11
+ var Data = function (config) {
12
+
13
+ function initialize() {
14
+ Base.assert_required_keys(config, 'type', 'primary_key');
15
+ if (typeof Retreaver.Base.Data._store[config.type] === 'undefined') {
16
+ Retreaver.Base.Data._store[config.type] = {};
17
+ }
18
+ if (typeof Retreaver.Base.Data._store[config.type][config.primary_key] === 'undefined') {
19
+ Retreaver.Base.Data._store[config.type][config.primary_key] = {};
20
+ }
21
+ }
22
+
23
+ var self = this;
24
+
25
+ /**
26
+ * Request data from the host
27
+ * @memberOf Retreaver.Base.Data
28
+ * @function get
29
+ * @instance
30
+ * @param {String} key - The key to retrieve
31
+ * @returns {*}
32
+ */
33
+ self.get = function () {
34
+ var output = {};
35
+ if (typeof arguments[0] === 'undefined') {
36
+ output = Retreaver.Base.Data._store[config.type][config.primary_key];
37
+ } else if (arguments.length === 1) {
38
+ output = Retreaver.Base.Data._store[config.type][config.primary_key][arguments[0]];
39
+ } else {
40
+ for (var i = 0; i < arguments.length; i++) {
41
+ var key = arguments[i];
42
+ output[key] = Retreaver.Base.Data._store[config.type][config.primary_key][key];
43
+ }
44
+ }
45
+ return output;
46
+ };
47
+
48
+ /**
49
+ * Request data from the host
50
+ * @memberOf Retreaver.Base.Data
51
+ * @function set
52
+ * @instance
53
+ * @param {String} key - The key to retrieve
54
+ * @param {String} value - The value to assign
55
+ * @returns {*}
56
+ */
57
+ self.set = function (key, value) {
58
+ Retreaver.Base.Data._store[config.type][config.primary_key][key] = value;
59
+ return value;
60
+ };
61
+
62
+ /**
63
+ * Merge data
64
+ * @memberOf Retreaver.Base.Data
65
+ * @function set
66
+ * @instance
67
+ * @param {String} object - The object to merge
68
+ * @returns {*}
69
+ */
70
+ self.merge = function (object) {
71
+ for (var key in object) {
72
+ Retreaver.Base.Data._store[config.type][config.primary_key][key] = object[key];
73
+ }
74
+ return object;
75
+ };
76
+
77
+ initialize();
78
+ };
79
+ Data._store = {};
80
+ Retreaver.Base.Data = Data;
81
+ })();
@@ -0,0 +1,55 @@
1
+ (function () {
2
+ // ensure namespace is present
3
+ if (typeof window.Retreaver === 'undefined') window.Retreaver = {};
4
+ var Base = {};
5
+ // define helpers
6
+ Base.assert_required_keys = function () {
7
+ var args = Array.prototype.slice.call(arguments);
8
+ var object = args.shift();
9
+ for (var i = 0; i < args.length; i++) {
10
+ var key = args[i];
11
+ if (typeof object === 'undefined' || typeof object[key] === 'undefined') {
12
+ throw "ArgumentError: Required keys are not defined: " + args.join(', ');
13
+ }
14
+ }
15
+ return object;
16
+ };
17
+ Base.merge = function (obj1, obj2) {
18
+ for (var p in obj2) {
19
+ try {
20
+ if (obj2[p].constructor == Object) {
21
+ obj1[p] = Base.merge(obj1[p], obj2[p]);
22
+ } else {
23
+ obj1[p] = obj2[p];
24
+ }
25
+ } catch (e) {
26
+ obj1[p] = obj2[p];
27
+ }
28
+ }
29
+ return obj1;
30
+ };
31
+ Base.isArray = function (arg) {
32
+ return Object.prototype.toString.call(arg) === '[object Array]';
33
+ };
34
+ Base.ieVersion = function () {
35
+ if (Base._ieVersion == null) {
36
+ Base._ieVersion = (function () {
37
+ var v = 3,
38
+ div = document.createElement('div'),
39
+ all = div.getElementsByTagName('i');
40
+
41
+ while (
42
+ div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
43
+ all[0]
44
+ ) {
45
+ }
46
+ return v > 4 ? v : false;
47
+ }());
48
+ }
49
+ if (Base._ieVersion == 6 || Base._ieVersion == 7) {
50
+ if (Retreaver['easyxdm_loaded'] == null) Retreaver['easyxdm_loaded'] = false;
51
+ }
52
+ return Base._ieVersion;
53
+ };
54
+ Retreaver.Base = Base;
55
+ })();
@@ -0,0 +1,113 @@
1
+ (function () {
2
+ // Dependencies
3
+ var Base = Retreaver.Base;
4
+ var Data = Retreaver.Base.Data;
5
+
6
+ /**
7
+ * @constructor
8
+ * @memberof Retreaver.Base
9
+ */
10
+ var Model = function () {
11
+
12
+ this.api_host_uri = '/api/v1/';
13
+ this.type = 'model';
14
+
15
+ this.primary_key = function (primary_key) {
16
+ return Model.primary_key(this.type, primary_key);
17
+ };
18
+
19
+ this.store = function (data) {
20
+ // do we have data to store?
21
+ if (typeof(data) !== 'undefined') {
22
+ // does the data contain the required primary_key?
23
+ var key = this.primary_key();
24
+ if (typeof(data[key]) === 'undefined') {
25
+ throw("ArgumentError: Expected to receive primary_key " + key);
26
+ }
27
+ // has a store been initialized?
28
+ else if (typeof(this._store) === 'undefined') {
29
+ this._store = new Retreaver.Base.Data({type: this.type, primary_key: data[key] });
30
+ }
31
+ // merge the data
32
+ this._store.merge(data);
33
+ // update visitor is token present
34
+ Model.update_visitor_id(data);
35
+ }
36
+ return this._store;
37
+ };
38
+
39
+ this.get_data = function (path, callback) {
40
+ return this.connection().getJSON(this.api_host_uri + path, null, [Model.update, callback], this);
41
+ };
42
+
43
+ this.post_data = function (path, data, callback) {
44
+ return this.connection().postJSON(this.api_host_uri + path, data, [Model.update, callback], this);
45
+ };
46
+
47
+ this.set = function () {
48
+ if (typeof(this["set_" + arguments[0]]) === 'function') {
49
+ arguments[1] = this["set_" + arguments[0]].apply(this, [arguments[1]]);
50
+ }
51
+ return this._store.set.apply(this, arguments);
52
+ };
53
+ this.get = function () {
54
+ return this._store.get.apply(this, arguments);
55
+ };
56
+ this.connection = function () {
57
+ return Retreaver.Base.Request.connection();
58
+ };
59
+ };
60
+ Model.inflections = {
61
+ 'number': 'numbers',
62
+ 'campaign': 'campaigns'
63
+ };
64
+ Model.update = function (data) {
65
+ for (var key in data) {
66
+ var type = key;
67
+ var value = data[key];
68
+ if (typeof Model.inflections[key] !== 'undefined') {
69
+ type = Model.inflections[key];
70
+ }
71
+ if (typeof Data._store[type] !== 'undefined') {
72
+ if (Base.isArray(value)) {
73
+ for (var i = 0; i < value.length; i++) {
74
+ Model.update_record(type, value[i]);
75
+ }
76
+ } else {
77
+ Model.update_record(type, value[i]);
78
+ }
79
+ }
80
+ }
81
+ return data;
82
+ };
83
+ Model.update_record = function (type, record) {
84
+ // update visitor is token present
85
+ Model.update_visitor_id(record);
86
+ // update data store
87
+ if (typeof record.id !== 'undefined') {
88
+ var primary_key = Model.primary_key(type);
89
+ for (var key in record) {
90
+ Retreaver.Base.Data._store[type][record[primary_key]][key] = record[key];
91
+ }
92
+ return true
93
+ } else {
94
+ return false
95
+ }
96
+ return record;
97
+ };
98
+ Model.update_visitor_id = function (record) {
99
+ if (typeof(record) !== 'undefined' && typeof(record.visitor_id) !== 'undefined') {
100
+ Retreaver.Base.Cookies.set('CallPixels-vid', record.visitor_id);
101
+ }
102
+ };
103
+ Model.primary_key = function (type, primary_key) {
104
+ if (typeof(Model.primary_keys) === 'undefined') Model.primary_keys = {};
105
+ // default key
106
+ if (typeof(Model.primary_keys[type]) === 'undefined') Model.primary_keys[type] = 'id';
107
+ // assign key if passed
108
+ if (typeof(primary_key) !== 'undefined') Model.primary_keys[type] = primary_key;
109
+ // return value
110
+ return Model.primary_keys[type];
111
+ }
112
+ Retreaver.Base.Model = Model;
113
+ })();