empire-client 0.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 528dc8d235093675e0f22b738a0f16d8e7134e09
4
- data.tar.gz: c6b479ff28bef1e9f7ad0a9d219923b3e3a4078b
3
+ metadata.gz: 0f4fb9c86e7df8d24e6b73bee4f2daa7494dedca
4
+ data.tar.gz: 16ec7ecebb1b7be684c9e2605f037c3117f722c6
5
5
  SHA512:
6
- metadata.gz: 0534ce3f0f14d9525eecbd921a7f6e9d1b51e378d6ffc39f5f67b0f841b1c376cb130116928a0ab41843c97da7aab69b2f69e080cce88502fca147029c2ff498
7
- data.tar.gz: 5948cc3f5ee664f930b4586a9cd21231ffc2e1806a5c0e4ea084c8c2835c0792371493b574c1f750b65794960e719c5afa012261ec818fd7485990b21f689854
6
+ metadata.gz: 0a78109f1f14a5150ec7fec450dae4f26c04e88183166557a123f45e5466f411e3d1169747d61846ecfd2672df22cd27fa98cffdef11226e6e2667e3b1dda362
7
+ data.tar.gz: 2b2e51e26f9a9fc6836790967715e4cd7f9d8135918fa9321e837f56cdbc66f79a8556d2830f6a29c62d0816d35e123a62e5f081f108c8f4ecb1221f73ccb7c4
data/lib/empire.rb CHANGED
@@ -6,6 +6,7 @@ require 'httpclient'
6
6
  require 'irb-pager'
7
7
 
8
8
  require_relative 'walkthrough.rb'
9
+ require_relative 'exceptions.rb'
9
10
 
10
11
  class Empire
11
12
  attr_reader :base_url
@@ -13,13 +14,13 @@ class Empire
13
14
  # app_key is your Empire application key, and is necessary for using the API
14
15
  # opts can include any of:
15
16
  # * :api_server => the server to connect to (default: api.empiredata.com)
16
- # * :end_user => a string identifying the end user, required for any operations on views (default: nil)
17
+ # * :enduser => a string identifying the end user, required for any operations on views (default: nil)
17
18
  # * :secrets_yaml => the path to a YAML file generated by https://login.empiredata.co (default: nil)
18
19
  def initialize(app_key = nil, opts = {})
19
20
  api_server = opts[:api_server] || 'api.empiredata.co'
20
21
 
21
22
  @app_key = app_key
22
- @end_user = opts[:end_user]
23
+ @enduser = opts[:enduser]
23
24
  @session_key = nil
24
25
 
25
26
  @http_client = HTTPClient.new
@@ -27,9 +28,13 @@ class Empire
27
28
  protocol = api_server.start_with?('localhost') ? 'http' : 'https'
28
29
  @base_url = "#{protocol}://#{api_server}/empire/"
29
30
 
30
- @service_secrets = nil
31
31
  if opts[:secrets_yaml]
32
- @service_secrets = YAML.load_file opts[:secrets_yaml]
32
+ service_secrets = YAML.load_file opts[:secrets_yaml]
33
+
34
+ @secrets = {}
35
+ @service_secrets[service]['option'].each{|k, v|
36
+ secrets[k] = v['value']
37
+ }
33
38
  end
34
39
  end
35
40
 
@@ -39,13 +44,7 @@ class Empire
39
44
  def connect(service, secrets = nil)
40
45
  path = "services/#{service}/connect"
41
46
  unless secrets
42
- unless @service_secrets
43
- raise "secrets must be provided on connect command,
44
- or secrets_yaml file must be given when constructing this instance"
45
- end
46
-
47
- secrets = {}
48
- @service_secrets[service]['option'].each{|k, v| secrets[k] = v['value']}
47
+ raise Empire::MissingSecretsError.new
49
48
  end
50
49
  request path, :post, {}, secrets
51
50
  end
@@ -58,7 +57,7 @@ class Empire
58
57
  elsif service and !table
59
58
  path += "/#{service}"
60
59
  elsif !service and table
61
- raise "Service must be specified if table is specified"
60
+ raise Empire::MissingServiceError.new("Service must be specified if table is specified")
62
61
  end
63
62
 
64
63
  request path
@@ -90,8 +89,8 @@ class Empire
90
89
 
91
90
  # Materialize a SQL query as a view. This creates or updates a view.
92
91
  def materialize_view(name, sql)
93
- unless @end_user
94
- raise "Cannot use materialized view within a session initiated without an enduser"
92
+ unless @enduser
93
+ raise Empire::MissingEnduserError.new
95
94
  end
96
95
  path = "view/#{name}"
97
96
  data = {'query' => sql}
@@ -100,8 +99,8 @@ class Empire
100
99
 
101
100
  # Delete a materialized view of SQL query
102
101
  def drop_view(name)
103
- unless @end_user
104
- raise "Cannot use materialized view within a session initiated without an enduser"
102
+ unless @enduser
103
+ raise Empire::MissingEnduserError.new
105
104
  end
106
105
  path = "view/#{name}"
107
106
  request path, :delete
@@ -116,7 +115,7 @@ class Empire
116
115
  when 'ready' then true
117
116
  when 'pending' then false
118
117
  else
119
- raise "Unknown view status: #{response['viewStatus']}"
118
+ raise APIError.new("Unknown view status: #{response['viewStatus']}")
120
119
  end
121
120
  end
122
121
 
@@ -136,8 +135,8 @@ class Empire
136
135
  private
137
136
 
138
137
  def view_status(name)
139
- unless @end_user
140
- raise "Cannot use materialized view within a session initiated without an enduser"
138
+ unless @enduser
139
+ raise Empire::MissingEnduserError.new
141
140
  end
142
141
  path = "view/#{name}/status"
143
142
  request path, :get
@@ -152,8 +151,8 @@ class Empire
152
151
  def create_session
153
152
  headers = {'Authorization' => "Empire appkey=\"#{@app_key}\""}
154
153
  session_url = "session/create"
155
- if @end_user
156
- session_url = session_url + "?enduser=#{@end_user}"
154
+ if @enduser
155
+ session_url = session_url + "?enduser=#{@enduser}"
157
156
  end
158
157
  data = do_request session_url, :post, headers
159
158
  @sessionkey = data['sessionkey']
@@ -167,12 +166,20 @@ class Empire
167
166
  conn = @http_client.request_async method, url, nil, data.to_json, headers
168
167
  conn.pop.content
169
168
  else
170
- # return the response body
169
+ # return the response body, parsed as JSON
170
+
171
171
  response = @http_client.request method, url, body: data.to_json, header: headers
172
- if response.status == HTTP::Status::INTERNAL
173
- raise "Internal Server Error"
172
+
173
+ begin
174
+ json = JSON.parse(response.body)
175
+ rescue => e
176
+ raise Empire::APIError.new("#{e} #{response.body}")
177
+ end
178
+
179
+ if json["status"] != "OK"
180
+ raise Empire::APIError.new(json["error"])
174
181
  end
175
- JSON.parse(response.body)
182
+ json
176
183
  end
177
184
  end
178
185
  end
data/lib/exceptions.rb ADDED
@@ -0,0 +1,22 @@
1
+
2
+ class Empire
3
+ class APIError < RuntimeError; end
4
+
5
+ class MissingSecretsError < RuntimeError
6
+ def initialize(msg = "Secrets must be provided when connecting to a service, or a secrets YAML file must be given when constructing this instance")
7
+ super
8
+ end
9
+ end
10
+
11
+ class MissingEnduserError < RuntimeError
12
+ def initialize(msg = "Cannot use a materialized view within a session initiated without an enduser")
13
+ super
14
+ end
15
+ end
16
+
17
+ class MissingServiceError < RuntimeError
18
+ def initialize(msg = "Service must be specified")
19
+ super
20
+ end
21
+ end
22
+ end
data/lib/walkthrough.rb CHANGED
@@ -59,7 +59,7 @@ class Empire
59
59
  sql = "SELECT * FROM #{service}.#{table} LIMIT 5"
60
60
  puts "empire.query '#{sql}'"
61
61
  query(sql) do |row|
62
- print_row(row, 75)
62
+ print_row(row)
63
63
  end
64
64
  rescue Exception => e
65
65
  puts "Problem with #{service}.#{table}"
@@ -77,21 +77,21 @@ class Empire
77
77
  puts "empire.materialize_view('view_name', 'SELECT * FROM #{service}.#{table} LIMIT 5')"
78
78
  materialize_view('view_name', "SELECT * FROM #{service}.#{table} LIMIT 5")
79
79
 
80
- puts "until empire.view_ready? 'view_name'\n sleep 0.1\nend"
80
+ puts "until empire.view_ready? 'view_name'\n sleep 0.01\nend"
81
81
  until view_ready? 'view_name'
82
82
  sleep 0.01
83
83
  end
84
84
 
85
85
  puts "empire.query 'SELECT * FROM view_name'"
86
86
  query('SELECT * FROM view_name') do |row|
87
- print_row(row, 75)
87
+ print_row(row)
88
88
  end
89
89
 
90
90
  puts "empire.drop_view 'view_name'"
91
91
  drop_view 'view_name'
92
92
  end
93
93
 
94
- def print_row(row, max_length)
94
+ def print_row(row, max_length = 70)
95
95
  fragment = row.slice(0, max_length)
96
96
  if fragment.length == max_length
97
97
  fragment = fragment + "..."
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: empire-client
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - UPSHOT Data, Inc.
@@ -66,16 +66,17 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.18'
69
- description: Empire is an API for accessing enterprise SaaS services such as Salesforce,
70
- Zendesk, Google Apps, etc. It provides a uniform, database-like interface to every
71
- service that it supports. Empire makes it easy to integrate data from multiple enterprise
72
- services into your own enterprise app.
69
+ description: Ruby client for the Empire API. Empire is an API for accessing enterprise
70
+ SaaS services such as Salesforce, Zendesk, Google Apps, etc. Empire provides a uniform,
71
+ database-like interface to every service that it supports. Empire makes it easy
72
+ to integrate data from multiple enterprise services into your own enterprise app.
73
73
  email: hello@empiredata.co
74
74
  executables: []
75
75
  extensions: []
76
76
  extra_rdoc_files: []
77
77
  files:
78
78
  - lib/empire.rb
79
+ - lib/exceptions.rb
79
80
  - lib/walkthrough.rb
80
81
  homepage: http://empiredata.co
81
82
  licenses: