Class: GrepdataClient::Client
- Inherits:
-
Object
- Object
- GrepdataClient::Client
- Defined in:
- lib/grepdata_client.rb
Constant Summary collapse
- CONFIG =
{ :api_url => "https://api.grepdata.com/v1", }
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#parallel ⇒ Object
Returns the value of attribute parallel.
-
#parallel_manager ⇒ Object
Returns the value of attribute parallel_manager.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #dimensions(params) ⇒ Object
- #funneling(params) ⇒ Object
- #generate_access_key(api_key, options) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #query(params) ⇒ Object
- #query_with_token(params, access_key) ⇒ Object
- #request(action, options) ⇒ Object
- #run_requests ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 |
# File 'lib/grepdata_client.rb', line 20 def initialize(config) @api_key, @token = config.values_at(:api_key, :token) @api_url = config[:api_url] || CONFIG[:api_url] @parallel = config[:parallel] || false if config[:parallel_manager] @parallel_manager = config[:parallel_manager] end end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/grepdata_client.rb', line 14 def api_key @api_key end |
#api_url ⇒ Object
Returns the value of attribute api_url.
14 15 16 |
# File 'lib/grepdata_client.rb', line 14 def api_url @api_url end |
#parallel ⇒ Object
Returns the value of attribute parallel.
14 15 16 |
# File 'lib/grepdata_client.rb', line 14 def parallel @parallel end |
#parallel_manager ⇒ Object
Returns the value of attribute parallel_manager.
14 15 16 |
# File 'lib/grepdata_client.rb', line 14 def parallel_manager @parallel_manager end |
#token ⇒ Object
Returns the value of attribute token.
14 15 16 |
# File 'lib/grepdata_client.rb', line 14 def token @token end |
Instance Method Details
#dimensions(params) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/grepdata_client.rb', line 76 def dimensions(params) params[:api_key] = params[:api_key] || @api_key Utils.check_attributes "params", params: params, required: { api_key: String, endpoint: String } request(__method__, params: params) end |
#funneling(params) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/grepdata_client.rb', line 51 def funneling(params) params[:api_key] = params[:api_key] || @api_key params[:filters] = params[:filters] || {} params[:only_totals] = params[:only_totals] || false Utils.preprocess_dates params, [:start_date, :end_date] Utils.check_attributes "params", params: params, required: { api_key: String, datamart: String, funnel_dimension: String, dimensions: Array, metrics: Array, filters: Hash, time_interval: String, start_date: String, end_date: String, steps: Array } request(__method__, params: params) end |
#generate_access_key(api_key, options) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/grepdata_client.rb', line 142 def generate_access_key(api_key, ) restricted = [:restricted] || [] expiration = [:expiration] || Utils.default_expiration params = [:params] || [:request].params params = Utils.format_params 'query', params datamart = params[:datamart] values = "" restricted.each do |param| value = params segments = param.split(".") segments.each do |segment| value = JSON.parse value if value.is_a? String value = value[segment.to_sym] || value[segment] end value = segments.length > 1? value.to_json : value.to_s values += "&" if values.length > 0 values += "#{param}=#{value}" end scope = "datamart=#{datamart}" scope += "&#{values}" if signature = Utils.generate_key(api_key, datamart: datamart, values: values, expiration: expiration) return { :signature => signature, :restricted => restricted.join(','), :expiration => expiration.to_s, :scope => scope } end |
#query(params) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/grepdata_client.rb', line 29 def query(params) params[:api_key] = params[:api_key] || @api_key params[:filters] = params[:filters] || {} Utils.preprocess_dates params, [:start_date, :end_date] Utils.check_attributes "params", params: params, required: { api_key: String, datamart: String, dimensions: Array, metrics: Array, filters: Hash, time_interval: String, start_date: String, end_date: String } request(__method__, params: params) end |
#query_with_token(params, access_key) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/grepdata_client.rb', line 89 def query_with_token(params, access_key) params[:token] = params[:token] || @token params[:filters] = params[:filters] || {} Utils.preprocess_dates params, [:start_date, :end_date] Utils.preprocess_dates access_key, [:expiration] Utils.check_attributes "params", params: params, required: { token: String, datamart: String, dimensions: Array, metrics: Array, filters: Hash, time_interval: String, start_date: String, end_date: String } Utils.check_attributes "access_key", params: access_key, required: { signature: String, restricted: String, expiration: String } request('fetch', params: params, headers: access_key) end |
#request(action, options) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/grepdata_client.rb', line 124 def request(action, ) query = GrepdataClient::DataRequest.new action, url: @api_url, params: [:params], headers: [:headers] if @parallel unless @parallel_manager @parallel_manager = ::Typhoeus::Hydra.new end @parallel_manager.queue query.request else query.request.run end return query end |
#run_requests ⇒ Object
120 121 122 |
# File 'lib/grepdata_client.rb', line 120 def run_requests @parallel_manager.run if @parallel_manager end |