Class: Lab628

Inherits:
Object
  • Object
show all
Defined in:
lib/lab628.rb,
lib/lab628/version.rb,
lib/lab628/post_job.rb

Defined Under Namespace

Classes: PostJob

Constant Summary collapse

BASE_URL =
'http://www.lab628.com'.freeze
ConfigurationError =
Class.new StandardError
PayloadError =
Class.new StandardError
ApiError =
Class.new StandardError
VERSION =
'1.2.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Lab628

Returns a new instance of Lab628.



101
102
103
104
105
106
# File 'lib/lab628.rb', line 101

def initialize(opts = {})
  @api_key = opts[:api_key] || Lab628.api_key
  @secret = opts[:secret] || Lab628.secret
  @sync = opts[:sync] || Lab628.sync
  self.class.check @api_key, @secret
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



17
18
19
# File 'lib/lab628.rb', line 17

def api_key
  @api_key
end

.secretObject

Returns the value of attribute secret.



18
19
20
# File 'lib/lab628.rb', line 18

def secret
  @secret
end

.syncObject

Returns the value of attribute sync.



19
20
21
# File 'lib/lab628.rb', line 19

def sync
  @sync
end

Class Method Details

.activity(data) ⇒ Object



21
22
23
# File 'lib/lab628.rb', line 21

def activity(data)
  send_activity @api_key, @secret, data, @sync
end

.check(key, sec) ⇒ Object

Raises:



45
46
47
48
# File 'lib/lab628.rb', line 45

def check(key, sec)
  raise ConfigurationError, 'Lab628 api key is not set' unless key
  raise ConfigurationError, 'Lab628 shared secret is not set' unless sec
end

.post(command, key, sec, data) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/lab628.rb', line 50

def post(command, key, sec, data)
  connection.post do |req|
    req.url "/api/#{command}/#{key}"
    req.headers['X-Secret'] = sec
    req.headers['Content-Type'] = 'application/json'
    req.body = validate_payload data
  end
end

.query(data) ⇒ Object



34
35
36
# File 'lib/lab628.rb', line 34

def query(data)
  send_query @api_key, @secret, data
end

.send_activity(key, sec, data, sync_mode) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/lab628.rb', line 25

def send_activity(key, sec, data, sync_mode)
  response = send_command 'track', key, sec, data, sync_mode
  if sync_mode == :inline
    response.status == 200
  else
    response
  end
end

.send_query(key, sec, query) ⇒ Object



38
39
40
41
42
43
# File 'lib/lab628.rb', line 38

def send_query(key, sec, query)
  response = send_command 'query', key, sec, query
  response.status == 200 && JSON.parse(response.body)
rescue JSON::ParserError => e
  raise ApiError, e.message
end

Instance Method Details

#activity(data) ⇒ Object



108
109
110
# File 'lib/lab628.rb', line 108

def activity(data)
  self.class.send_activity @api_key, @secret, data, @sync
end

#query(query) ⇒ Object



112
113
114
# File 'lib/lab628.rb', line 112

def query(query)
  self.class.send_query @api_key, @secret, query
end