Class: Empire
- Inherits:
-
Object
- Object
- Empire
- Defined in:
- lib/empire.rb,
lib/exceptions.rb,
lib/walkthrough.rb
Defined Under Namespace
Classes: APIError, MissingEnduserError, MissingSecretsError, MissingServiceError
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
-
#connect(service, secrets = nil) ⇒ Object
Connect to specific service service: service name secrets: hash with service secrets (optional if the Empire instance was initialized with a secrets_yaml).
-
#describe(service = nil, table = nil) ⇒ Object
Describe all services, all tables within a given service, or a given table.
-
#drop_view(name) ⇒ Object
Delete a materialized view of SQL query.
-
#initialize(app_key = nil, opts = {}) ⇒ Empire
constructor
app_key is your Empire application key, and is necessary for using the API opts can include any of: * :api_server => the server to connect to (default: api.empiredata.com) * :enduser => a string identifying the end user, required for any operations on views (default: nil) * :secrets_yaml => the path to a YAML file generated by login.empiredata.co (default: nil).
-
#insert(service, table, row) ⇒ Object
Insert a new row into this service table.
-
#inspect ⇒ Object
emulate default Object#inspect method but only display the object_id, not the properties to make things cleaner and more similar to Python client.
-
#materialize_view(name, sql) ⇒ Object
Materialize a SQL query as a view.
-
#print_query(sql) ⇒ Object
Paginated printing of an SQL query.
-
#query(sql) ⇒ Object
Issue a SQL query, yielding each row.
-
#view_materialized_at(name) ⇒ Object
Datetime that this view was materialized at.
-
#view_ready?(name) ⇒ Boolean
Boolean check if a materialized view is ready for querying.
-
#walkthrough ⇒ Object
Run automatic test of all services from YAML.
Constructor Details
#initialize(app_key = nil, opts = {}) ⇒ Empire
app_key is your Empire application key, and is necessary for using the API opts can include any of:
* :api_server => the server to connect to (default: api.empiredata.com)
* :enduser => a string identifying the end user, required for any operations on views (default: nil)
* :secrets_yaml => the path to a YAML file generated by https://login.empiredata.co (default: nil)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/empire.rb', line 19 def initialize(app_key = nil, opts = {}) api_server = opts[:api_server] || 'api.empiredata.co' @app_key = app_key @enduser = opts[:enduser] @session_key = nil @http_client = HTTPClient.new protocol = api_server.start_with?('localhost') ? 'http' : 'https' @base_url = "#{protocol}://#{api_server}/empire/" @service_secrets = nil if opts[:secrets_yaml] @service_secrets = YAML.load_file opts[:secrets_yaml] end end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
12 13 14 |
# File 'lib/empire.rb', line 12 def base_url @base_url end |
Instance Method Details
#connect(service, secrets = nil) ⇒ Object
Connect to specific service service: service name secrets: hash with service secrets (optional if the Empire instance was initialized with a secrets_yaml)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/empire.rb', line 40 def connect(service, secrets = nil) path = "services/#{service}/connect" unless secrets unless @service_secrets raise Empire::MissingSecretsError.new end secrets = {} @service_secrets[service]['option'].each{|k, v| secrets[k] = v['value'] } end request path, :post, {}, secrets end |
#describe(service = nil, table = nil) ⇒ Object
Describe all services, all tables within a given service, or a given table
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/empire.rb', line 58 def describe(service = nil, table = nil) path = 'services' if service and table path += "/#{service}/#{table}" elsif service and !table path += "/#{service}" elsif !service and table raise Empire::MissingServiceError.new("Service must be specified if table is specified") end request path end |
#drop_view(name) ⇒ Object
Delete a materialized view of SQL query
106 107 108 109 110 111 112 |
# File 'lib/empire.rb', line 106 def drop_view(name) unless @enduser raise Empire::MissingEnduserError.new end path = "view/#{name}" request path, :delete end |
#insert(service, table, row) ⇒ Object
Insert a new row into this service table. The row should be a hash of value
90 91 92 93 |
# File 'lib/empire.rb', line 90 def insert(service, table, row) path = "services/#{service}/#{table}" request path, :post, {}, row end |
#inspect ⇒ Object
emulate default Object#inspect method but only display the object_id, not the properties to make things cleaner and more similar to Python client
136 137 138 |
# File 'lib/empire.rb', line 136 def inspect "#<Empire:#{(object_id << 1).to_s(16)}>" end |
#materialize_view(name, sql) ⇒ Object
Materialize a SQL query as a view. This creates or updates a view.
96 97 98 99 100 101 102 103 |
# File 'lib/empire.rb', line 96 def materialize_view(name, sql) unless @enduser raise Empire::MissingEnduserError.new end path = "view/#{name}" data = {'query' => sql} request path, :put, {}, data end |
#print_query(sql) ⇒ Object
Paginated printing of an SQL query
72 73 74 75 76 77 78 |
# File 'lib/empire.rb', line 72 def print_query(sql) IRB::Pager.pager { query(sql) do |l| puts l end } end |
#query(sql) ⇒ Object
Issue a SQL query, yielding each row
81 82 83 84 85 86 87 |
# File 'lib/empire.rb', line 81 def query(sql) path = 'query' io = request path, :post, {}, {query: sql}, stream: true io.each do |l| yield l end end |
#view_materialized_at(name) ⇒ Object
Datetime that this view was materialized at. nil if the materialization is currently pending.
129 130 131 132 |
# File 'lib/empire.rb', line 129 def view_materialized_at(name) status = view_status(name) Date.parse(status['materializedAt']) rescue nil end |
#view_ready?(name) ⇒ Boolean
The user is expected to check view_ready? before querying a view with query()
Boolean check if a materialized view is ready for querying.
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/empire.rb', line 116 def view_ready?(name) status = view_status(name) case status['viewStatus'] when 'ready' then true when 'pending' then false else raise APIError.new("Unknown view status: #{response['viewStatus']}") end end |
#walkthrough ⇒ Object
Run automatic test of all services from YAML
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/walkthrough.rb', line 5 def walkthrough @last_service = nil @last_table = nil unless @service_secrets puts "Please connect some services in https://login.empiredata.co, and download the new yaml file" return end @service_secrets.each do |secret| service = secret[0] walkthrough_service(service) end walkthrough_materialized_view(@last_service, @last_table) nil end |