Class: Tembin::Redash::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/tembin/redash/query.rb

Constant Summary collapse

INITIAL_DATA_SOURCE_ID =
1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Query

Returns a new instance of Query.



21
22
23
# File 'lib/tembin/redash/query.rb', line 21

def initialize(attributes)
  @attributes = attributes
end

Class Method Details

.allObject

Raises:

  • (RequestNotSucceedError)


6
7
8
9
10
# File 'lib/tembin/redash/query.rb', line 6

def self.all
  response = Tembin::Redash::Client.current.get('/api/queries')
  raise RequestNotSucceedError, response.body if !response.success?
  JSON.parse(response.body)['results'].map { |j| self.new(j) }
end

.create(name, sql) ⇒ Object



17
18
19
# File 'lib/tembin/redash/query.rb', line 17

def self.create(name, sql)
  Tembin::Redash::Client.current.post("/api/queries", body: { name: name, query: sql, data_source_id: INITIAL_DATA_SOURCE_ID })
end

.created_by_meObject



12
13
14
# File 'lib/tembin/redash/query.rb', line 12

def self.created_by_me
  all.select { |q| q.author_email == Tembin::Redash.config['authorized_user_email'] }
end

Instance Method Details

#author_emailObject



33
34
35
# File 'lib/tembin/redash/query.rb', line 33

def author_email
  @attributes['user'] && @attributes['user']['email']
end

#changed?(query) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/tembin/redash/query.rb', line 45

def changed?(query)
  Diffy::Diff.new(sql, query).to_s.length != 0
end

#delete!Object



53
54
55
# File 'lib/tembin/redash/query.rb', line 53

def delete!
  Tembin::Redash::Client.current.delete("/api/queries/#{id}")
end

#filenameObject



41
42
43
# File 'lib/tembin/redash/query.rb', line 41

def filename
  "#{@attributes['id']}_#{@attributes['name'].gsub(/(\/|-|\s)/, '_')}"
end

#idObject



25
26
27
# File 'lib/tembin/redash/query.rb', line 25

def id
  @attributes['id']
end

#nameObject



29
30
31
# File 'lib/tembin/redash/query.rb', line 29

def name
  @attributes['name']
end

#sqlObject



37
38
39
# File 'lib/tembin/redash/query.rb', line 37

def sql
  @attributes['query']
end

#update!(query) ⇒ Object



49
50
51
# File 'lib/tembin/redash/query.rb', line 49

def update!(query)
  Tembin::Redash::Client.current.post("/api/queries/#{id}", body: { query: query })
end