redash_dynamic_query 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 820ca9c05060b5aae4a6661b18b720ec61ab49c504bc7266ae2f2f95f8bc9c29
4
- data.tar.gz: d128117115042c3c903a5105cb97bca545349a6fe92ca4e8f911e873453ccdb8
3
+ metadata.gz: cbccab07df2c10d248989d87ea9a78fc3ae1db3258ab6f48295226f2f3af73ae
4
+ data.tar.gz: 9b41d7b04339be2ca31e362231fc1c642edd1025ba018837c0087a873963d7b0
5
5
  SHA512:
6
- metadata.gz: 2db7e63f32aa032ce1bb093bc7e43e2695e2234d8d2d5903c0cb4bb35c7a69ec1be5d65a757df436c49e1f6e8eeb123456fd00e684492723ebd7d144cb92e9f8
7
- data.tar.gz: 0a93e43b35ba0938005dc4401b02c5576128dc0ed8fc09044fb14b6d842ff352271a3bdfb5115e624ca324387bef05d72ac8128d4dc64fd9ec93df160be1dc89
6
+ metadata.gz: 350d58641bec262b0280b1864e50b0eaefb80d76aece85d241db583ef127cb5240764cf70ca6848492adb3c1ed10426a49aaabc655f720d23bf02c5af47647eb
7
+ data.tar.gz: 96bfd047ea1a0403fe38ea02ce60411b384af93b2e0a5d927af66a78ac4da004e440db2804ed0468900b2b0f69276904e7d47f71e0a49e53a38b006ad0bfcacc
data/Gemfile CHANGED
@@ -2,5 +2,7 @@ source "https://rubygems.org"
2
2
 
3
3
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
+ gem 'pry'
6
+
5
7
  # Specify your gem's dependencies in redash_dynamic_query.gemspec
6
8
  gemspec
@@ -6,7 +6,12 @@ PATH
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ coderay (1.1.2)
9
10
  diff-lcs (1.3)
11
+ method_source (0.9.0)
12
+ pry (0.11.3)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
10
15
  rake (10.5.0)
11
16
  rspec (3.8.0)
12
17
  rspec-core (~> 3.8.0)
@@ -27,6 +32,7 @@ PLATFORMS
27
32
 
28
33
  DEPENDENCIES
29
34
  bundler (~> 1.17)
35
+ pry
30
36
  rake (~> 10.0)
31
37
  redash_dynamic_query!
32
38
  rspec (~> 3.0)
@@ -10,5 +10,5 @@ require "redash_dynamic_query"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
14
- IRB.start(__FILE__)
13
+ require "pry"
14
+ Pry.start(__FILE__)
@@ -2,49 +2,89 @@ require 'net/https'
2
2
  require 'json'
3
3
  module RedashDynamicQuery
4
4
  class Client
5
- attr_reader :api_key, :endpoint, :wait_interval
5
+ attr_reader :api_key, :endpoint, :interval, :timeout
6
6
 
7
- def initialize(user_apikey:, endpoint:, wait_interval: 0.1)
8
- @api_key = user_apikey
7
+ def initialize(user_apikey:, endpoint:, interval: 0.1, timeout: 10)
8
+ @api_key = user_apikey
9
9
  @endpoint = endpoint
10
- @wait_interval = wait_interval
10
+ @interval = interval
11
+ @timeout = timeout
11
12
  end
12
13
 
13
14
  def query(query_id:, params:)
14
15
  job_id = post_job_request(query_id: query_id, params: params)
15
16
 
17
+ raise 'job_id not fuond.' if job_id.nil?
18
+
16
19
  query_result_id = poll_job(job_id: job_id)
17
20
 
21
+ raise 'query_result_id not fuond.' if query_result_id.nil?
22
+
18
23
  get_query_result(query_result_id: query_result_id)
19
24
  end
20
25
 
21
26
  private
22
27
 
23
28
  def post_job_request(query_id:, params:)
24
- query_string = URI.encode_www_form(params.transform_keys { |k| "p_#{k}" })
25
- url = "#{endpoint}/api/queries/#{query_id}/refresh?api_key=#{api_key}&#{query_string}"
26
- res = Net::HTTP.post_form(URI.parse(url), {})
29
+ res = with_error_handling do
30
+ Net::HTTP.post_form(
31
+ post_job_url(id: query_id, queries: query_string(params: params)),
32
+ {}
33
+ )
34
+ end
35
+
27
36
  json = JSON.parse(res.body)
28
- json.dig('job','id')
37
+ json.dig('job', 'id')
38
+ end
39
+
40
+ def poll_job(job_id:, count: 0)
41
+ res = with_error_handling do
42
+ Net::HTTP.get(poll_job_url(id: job_id))
43
+ end
44
+ id = JSON.parse(res).dig('job', 'query_result_id')
45
+
46
+ return id unless id.nil?
47
+
48
+ raise "wait timeout with #{timeout} seconds." if reach_limit?(count)
49
+
50
+ sleep interval
51
+ poll_job(job_id: job_id, count: count + 1)
29
52
  end
30
53
 
31
54
  def get_query_result(query_result_id:)
32
- url = "#{endpoint}/api/query_results/#{query_result_id}.json?api_key=#{api_key}"
33
- res = Net::HTTP.get(URI.parse(url))
55
+ res = with_error_handling do
56
+ Net::HTTP.get(query_result_url(id: query_result_id))
57
+ end
58
+
34
59
  JSON.parse(res)
35
60
  end
36
61
 
37
- def poll_job(job_id:)
38
- url = "#{endpoint}/api/jobs/#{job_id}?api_key=#{api_key}"
39
- res = Net::HTTP.get(URI.parse(url))
40
- id = JSON.parse(res).dig('job', 'query_result_id')
62
+ def post_job_url(id:, queries:)
63
+ URI.parse(
64
+ "#{endpoint}/api/queries/#{id}/refresh?api_key=#{api_key}&#{queries}"
65
+ )
66
+ end
41
67
 
42
- if id.nil?
43
- sleep wait_interval
44
- poll_job(job_id: job_id)
45
- else
46
- id
47
- end
68
+ def poll_job_url(id:)
69
+ URI.parse("#{endpoint}/api/jobs/#{id}?api_key=#{api_key}")
70
+ end
71
+
72
+ def query_result_url(id:)
73
+ URI.parse("#{endpoint}/api/query_results/#{id}.json?api_key=#{api_key}")
74
+ end
75
+
76
+ def query_string(params:)
77
+ URI.encode_www_form(params.transform_keys { |k| "p_#{k}" })
78
+ end
79
+
80
+ def reach_limit?(count)
81
+ count * interval > timeout
82
+ end
83
+
84
+ def with_error_handling
85
+ res = yield
86
+ raise res.body if res.is_a? Net::HTTPClientError
87
+ res
48
88
  end
49
89
  end
50
90
  end
@@ -1,3 +1,3 @@
1
1
  module RedashDynamicQuery
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redash_dynamic_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - swfz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-01 00:00:00.000000000 Z
11
+ date: 2018-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  requirements: []
99
99
  rubyforge_project:
100
- rubygems_version: 2.7.7
100
+ rubygems_version: 2.7.6
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: Redash query with dynamic parameter