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", :beacon_url => "https://beacon.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.
-
#default_endpoint ⇒ Object
Returns the value of attribute default_endpoint.
-
#endpoint_map ⇒ Object
Returns the value of attribute endpoint_map.
-
#parallel ⇒ Object
Returns the value of attribute parallel.
-
#parallel_manager ⇒ Object
Returns the value of attribute parallel_manager.
-
#send_with_headers ⇒ Object
Returns the value of attribute send_with_headers.
-
#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
- #get_safe_url(params, options = {}) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #query(params) ⇒ Object
- #request(action, options) ⇒ Object
- #run_requests ⇒ Object
- #track(event, options) ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/grepdata_client.rb', line 23 def initialize(config) @api_key, @token, @default_endpoint = config.values_at(:api_key, :token, :default_endpoint) @send_with_headers = config[:send_with_headers] || false @endpoint_map = config[:endpoint_map] || {} @api_url = config[:api_url] || CONFIG[:api_url] @beacon_url = config[:beacon_url] || CONFIG[:beacon_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 |
#default_endpoint ⇒ Object
Returns the value of attribute default_endpoint.
14 15 16 |
# File 'lib/grepdata_client.rb', line 14 def default_endpoint @default_endpoint end |
#endpoint_map ⇒ Object
Returns the value of attribute endpoint_map.
14 15 16 |
# File 'lib/grepdata_client.rb', line 14 def endpoint_map @endpoint_map 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 |
#send_with_headers ⇒ Object
Returns the value of attribute send_with_headers.
14 15 16 |
# File 'lib/grepdata_client.rb', line 14 def send_with_headers @send_with_headers 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
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/grepdata_client.rb', line 176 def dimensions(params) params[:api_key] = params[:api_key] || @api_key Utils.check_attributes "Request", params: params, required: { api_key: String, endpoint: String } request(__method__, params: params) end |
#funneling(params) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/grepdata_client.rb', line 151 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 "Request", 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
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/grepdata_client.rb', line 211 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 |
#get_safe_url(params, options = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/grepdata_client.rb', line 105 def get_safe_url(params, ={}) params[:token] = params[:token] || @token api_key = params[:api_key] || @api_key params.delete(:api_key) params[:filters] = params[:filters] || {} expiration = [:expiration] || Utils.default_expiration restricted = [:restricted] || ["datamart", "dimensions", "metrics", "filters", "time_interval", "start_date", "end_date"] access_key = generate_access_key(api_key, params:params, restricted:restricted, expiration:expiration) Utils.preprocess_dates params, [:start_date, :end_date] Utils.preprocess_dates access_key, [:expiration] Utils.check_attributes "Request", 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 } params[:signature] = access_key[:signature] params[:restricted] = access_key[:restricted] params[:expiration] = access_key[:expiration] query = GrepdataClient::DataRequest.new "fetch", url: @api_url, params: params query.get_url end |
#query(params) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/grepdata_client.rb', line 83 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 "Request", 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 |
#request(action, options) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/grepdata_client.rb', line 193 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
189 190 191 |
# File 'lib/grepdata_client.rb', line 189 def run_requests @parallel_manager.run if @parallel_manager end |
#track(event, options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/grepdata_client.rb', line 39 def track(event, ) token = [:token] || @token endpoint = @endpoint_map[event.to_sym] || @endpoint_map[event] || @default_endpoint data = [:data] Utils.check_attributes "Request", params: { token: token, endpoint: endpoint, data: data}, required: { token: String, endpoint: String, data: Hash } params = { event: event, q: data.to_json, cb: Utils.cache_buster, token: token } params[:t] = [:timestamp] if [:timestamp] params[:domain] = [:domain] if [:domain] params[:ua] = [:user_agent] if [:user_agent] params[:r] = [:referer] if [:referer] params[:ip] = [:ip] if [:ip] params[:visitor] = [:visitor] if [:visitor] params[:session] = [:session] if [:session] url = "#{@beacon_url}/#{endpoint}" request = Typhoeus::Request.new url, params: params, timeout: 5 if @parallel unless @parallel_manager @parallel_manager = ::Typhoeus::Hydra.new end @parallel_manager.queue request else request.run end request end |