Module: Localytics

Defined in:
lib/localytics.rb,
lib/localytics/app.rb,
lib/localytics/push.rb,
lib/localytics/profile.rb

Defined Under Namespace

Classes: App, Error, Profile, Push

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



25
26
27
# File 'lib/localytics.rb', line 25

def api_key
  @api_key
end

.api_secretObject

Returns the value of attribute api_secret.



25
26
27
# File 'lib/localytics.rb', line 25

def api_secret
  @api_secret
end

Class Method Details

.request(api_base, method, url, api_key = nil, api_secret = nil, params = {}, headers = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/localytics.rb', line 28

def self.request(api_base, method, url, api_key=nil, api_secret=nil, params={}, headers={})
  method = method.to_sym

  url = api_base + url

  unless api_key ||= @api_key
    raise Error.new('No API key provided')
  end

  unless api_secret ||= @api_secret
    raise Error.new('No API secret provided')
  end

  payload = JSON.generate(params) if method == :post || method == :patch
  params = nil unless method == :get

  auth = 'Basic ' + Base64.strict_encode64("#{api_key}:#{api_secret}")

  headers = {
      :params => params,
      :content_type => 'application/json',
      :accept => 'application/json',
      :authorization => auth

  }.merge(headers)

  options = {
      :headers => headers,
      :method => method,
      :url => url,
      :payload => payload
  }

  begin
    response = execute_request(options)
    return {} if response.code == 204 and method == :delete
    JSON.parse(response.body, :symbolize_names => true)
  rescue RestClient::Exception => e
    handle_errors e
  end
end