Class: ThisData::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/this_data/client.rb

Overview

For the ThisData REST APIv1 help.thisdata.com/docs/apiv1events

Constant Summary collapse

USER_AGENT =
"ThisData Ruby v#{ThisData::VERSION}"
NO_API_KEY_MESSAGE =
"Oops: you've got no ThisData API Key configured, so we can't talk to the API. Specify your ThisData API key using ThisData#setup (find yours at https://thisdata.com)"

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
# File 'lib/this_data/client.rb', line 13

def initialize
  @api_key = require_api_key
  @headers = {
    "User-Agent" => USER_AGENT
  }
  @default_query = {
    api_key: ThisData.configuration.api_key
  }
end

Instance Method Details

#delete(path) ⇒ Object

Perform a DELETE request against the ThisData API, with the API key prepopulated



53
54
55
# File 'lib/this_data/client.rb', line 53

def delete(path)
  self.class.delete(path, query: @default_query, headers: @headers)
end

#get(path, query: {}) ⇒ Object

Perform a GET request against the ThisData API, with the API key prepopulated



39
40
41
42
# File 'lib/this_data/client.rb', line 39

def get(path, query: {})
  query = @default_query.merge(query)
  self.class.get(path, query: query, headers: @headers)
end

#post(path, query: {}, body: {}) ⇒ Object

Perform a POST request against the ThisData API, with the API key prepopulated



46
47
48
49
# File 'lib/this_data/client.rb', line 46

def post(path, query: {}, body: {})
  query = @default_query.merge(query)
  self.class.post(path, query: query, headers: @headers, body: body)
end

#require_api_keyObject



23
24
25
# File 'lib/this_data/client.rb', line 23

def require_api_key
  ThisData.configuration.api_key || print_api_key_warning
end

#track(event, query: {}) ⇒ Object

A convenience method for tracking Events.

Parameters:

  • event (Required: Hash) a Hash containing details about the event.

    See http://help.thisdata.com/v1.0/docs/apiv1events for a
    full & current list of available options.
    


33
34
35
# File 'lib/this_data/client.rb', line 33

def track(event, query: {})
  post(ThisData::EVENTS_ENDPOINT, query: query, body: JSON.generate(event))
end