Class: Tembin::Redash::Query

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

Defined Under Namespace

Classes: RequestNotSucceedError

Constant Summary collapse

PAGE_OPTION =
{
  page: 1,
  page_size: 5,
}.freeze
INITIAL_DATA_SOURCE_ID =
1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Query

Returns a new instance of Query.



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

def initialize(attributes)
  @attributes = attributes
end

Class Method Details

.allObject



8
9
10
11
12
# File 'lib/tembin/redash/query.rb', line 8

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



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

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_me(fetch_all_page: true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tembin/redash/query.rb', line 18

def self.created_by_me(fetch_all_page: true)
  if !fetch_all_page
    return request_my_query(PAGE_OPTION)['results'].map { |j| self.new(j) }
  end
  responses = []
  response = request_my_query(PAGE_OPTION)
  responses << response
  while response['count'] > (response['page'] * response['page_size'])
    response = request_my_query(page: response['page'] + 1, page_size: response['page_size'])
    responses << response
  end
  responses.flat_map { |r| r['results'].map { |j| new(j) } }
end

Instance Method Details

#author_emailObject



58
59
60
# File 'lib/tembin/redash/query.rb', line 58

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

#changed?(query) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/tembin/redash/query.rb', line 70

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

#delete!Object



78
79
80
# File 'lib/tembin/redash/query.rb', line 78

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

#filenameObject



66
67
68
# File 'lib/tembin/redash/query.rb', line 66

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

#idObject



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

def id
  @attributes['id']
end

#nameObject



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

def name
  @attributes['name']
end

#sqlObject



62
63
64
# File 'lib/tembin/redash/query.rb', line 62

def sql
  @attributes['query']
end

#update!(query) ⇒ Object



74
75
76
# File 'lib/tembin/redash/query.rb', line 74

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