airship-client 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/airship-client.rb +193 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9792eca645365cca2fec018fc71ba8ed7d0201c6
4
+ data.tar.gz: 120797120575ca20dd777b1f94ffccbfb250d07b
5
+ SHA512:
6
+ metadata.gz: adda40bb20ee35ddc0e76936b906f248e1f24115174a02bb4e2d07a20becf88eb58f7610bc9a2502bb9d8343ef786a483163f0db0ee95b887ecee33bac775720
7
+ data.tar.gz: c71433b83e299c285b05b9978c4a81f285ebc3ddcea8a9a39c40aa69c3c5fd14c00564e1441b7c3d61616c6d4e53a2d0ddd762f49a6e89e26ccab99ad832e794
@@ -0,0 +1,193 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ =begin
5
+ AirshipClient.init("r9b72kqdh1wbzkpkf7gntwfapqoc26bl", "nxmqp35umrd3djth")
6
+ AirshipClient.set_env_key("c1087mh6a3hjxiaz")
7
+ client = AirshipClient.new
8
+ client = AirshipClient.new("nxmqp35umrd3djth")
9
+ client = AirshipClient.new("c1087mh6a3hjxiaz")
10
+
11
+ 1. Error checking for the keys
12
+ 2. identify and gate endpoint
13
+ client.get_value("<control_name>", <obj>) -> [false, true]
14
+ client.get_variation("<control_name>", <obj>) -> [nil, "<variation1>", "<variation2">, ...]
15
+ client.identify([<obj>, ...]) -> dictionary
16
+ client.gate(<obj>) -> dictionary
17
+ =end
18
+
19
+ API_BASE_ENDPOINT = "https://api.airshiphq.com"
20
+ V1_IDENTIFY_ENDPOINT = "/v1/identify"
21
+ V1_GATE_ENDPOINT = "/v1/gate"
22
+
23
+ DEFAULT_TIMEOUT = 2
24
+
25
+ SERVER_INFO_KEY = "server_info"
26
+ SERVER_STATE_MAINTENANCE = "maintenance"
27
+
28
+
29
+ class AirshipClient
30
+ @@api_key = nil
31
+ @@env_key = nil
32
+ @@timeout = DEFAULT_TIMEOUT
33
+ @@fail_gracefully = true
34
+
35
+ @api_key = nil
36
+ @env_key = nil
37
+ @conn = nil
38
+
39
+ class << self
40
+ def init(api_key, env_key = nil, timeout = DEFAULT_TIMEOUT, fail_gracefully = true)
41
+ if api_key.nil?
42
+ raise ArgumentError.new("api_key cannot be nil")
43
+ end
44
+
45
+ if !api_key.nil?
46
+ self._validate_api_key(api_key)
47
+ end
48
+
49
+ if !env_key.nil?
50
+ self._validate_env_key(env_key)
51
+ end
52
+
53
+ self._validate_timeout(timeout)
54
+ self._validate_fail_gracefully(fail_gracefully)
55
+
56
+ @@api_key = api_key
57
+ @@env_key = env_key
58
+ @@timeout = timeout
59
+ @@fail_gracefully = fail_gracefully
60
+ nil
61
+ end
62
+
63
+ def set_env_key(env_key)
64
+ if env_key.nil?
65
+ raise ArgumentError.new("env_key cannot be nil")
66
+ end
67
+ self._validate_env_key(env_key)
68
+ @@env_key = env_key
69
+ end
70
+
71
+ def _validate_api_key(api_key)
72
+ if !api_key.instance_of?(String)
73
+ raise ArgumentError.new("api_key must be a string")
74
+ end
75
+ end
76
+
77
+ def _validate_env_key(env_key)
78
+ if !env_key.instance_of?(String)
79
+ raise ArgumentError.new("env_key must be a string")
80
+ end
81
+ end
82
+
83
+ def _validate_timeout(timeout)
84
+ if !timeout.is_a?(Integer)
85
+ raise ArgumentError.new("timeout must be an integer")
86
+ end
87
+ end
88
+
89
+ def _validate_fail_gracefully(fail_gracefully)
90
+ if !(fail_gracefully == true || fail_gracefully == false)
91
+ raise ArgumentError.new("fail_gracefully must be true or false")
92
+ end
93
+ end
94
+ end
95
+
96
+ def initialize(api_key = nil, env_key = nil)
97
+ if !api_key.nil?
98
+ self.class._validate_api_key(api_key)
99
+ end
100
+
101
+ if !env_key.nil?
102
+ self.class._validate_env_key(env_key)
103
+ end
104
+
105
+ @api_key = api_key
106
+ @env_key = env_key
107
+ @conn = Faraday.new(:url => API_BASE_ENDPOINT)
108
+ nil
109
+ end
110
+
111
+ def identify(objs)
112
+ if !objs.instance_of?(Array)
113
+ objs = [objs]
114
+ end
115
+ begin
116
+ response = @conn.post do |req|
117
+ req.url(V1_IDENTIFY_ENDPOINT)
118
+ req.headers["Content-Type"] = "application/json"
119
+ req.headers["api-key"] = @api_key || @@api_key
120
+ req.options.timeout = @@timeout
121
+ request_obj = {}
122
+ request_obj["env_key"] = @env_key || @@env_key
123
+ request_obj["objects"] = objs
124
+ req.body = request_obj.to_json
125
+ end
126
+ result = JSON.parse(response.body)
127
+ result
128
+ rescue Faraday::TimeoutError => e
129
+ raise
130
+ end
131
+ end
132
+
133
+ def gate(control_name, obj)
134
+ begin
135
+ response = @conn.post do |req|
136
+ req.url(V1_GATE_ENDPOINT)
137
+ req.headers["Content-Type"] = "application/json"
138
+ req.headers["api-key"] = @api_key || @@api_key
139
+ req.options.timeout = @@timeout
140
+ request_obj = {}
141
+ request_obj["env_key"] = @env_key || @@env_key
142
+ request_obj["control_short_name"] = control_name
143
+ request_obj["object"] = obj
144
+ req.body = request_obj.to_json
145
+ end
146
+ result = JSON.parse(response.body)
147
+ if result[SERVER_INFO_KEY] == SERVER_STATE_MAINTENANCE
148
+ if @@fail_gracefully
149
+ return {
150
+ "type" => obj["type"],
151
+ "id" => obj["id"],
152
+ "display_name" => obj["display_name"],
153
+ "control" => {
154
+ "control_short_name" => control_name,
155
+ "value" => false,
156
+ "variation" => nil,
157
+ "from_server" => false,
158
+ "from_cache" => false
159
+ }
160
+ }
161
+ end
162
+ end
163
+ result
164
+ rescue Faraday::TimeoutError => e
165
+ if @@fail_gracefully
166
+ return {
167
+ "type" => obj["type"],
168
+ "id" => obj["id"],
169
+ "display_name" => obj["display_name"],
170
+ "control" => {
171
+ "control_short_name" => control_name,
172
+ "value" => false,
173
+ "variation" => nil,
174
+ "from_server" => false,
175
+ "from_cache" => false
176
+ }
177
+ }
178
+ else
179
+ raise
180
+ end
181
+ end
182
+ end
183
+
184
+ def get_value(control_name, obj)
185
+ result = self.gate(control_name, obj)
186
+ result["control"]["value"]
187
+ end
188
+
189
+ def get_variation(control_name, obj)
190
+ result = self.gate(control_name, obj)
191
+ result["control"]["variation"]
192
+ end
193
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: airship-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Airship Dev Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.14.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.14.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.0
41
+ description: Ruby SDK
42
+ email: hello@airshiphq.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/airship-client.rb
48
+ homepage: https://airshiphq.com
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ source_code_uri: https://github.com/airshiphq/airship-ruby
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.0.14
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A light Ruby SDK to access Airship
73
+ test_files: []