Module: Localytics

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

Defined Under Namespace

Classes: App, Error, Event, Profile, Push

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

.api_secretObject

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

Class Method Details

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



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
69
70
71
72
73
74
75
76
77
78
# File 'lib/localytics.rb', line 29

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

  unless method == :get
    payload = JSON.generate(params)
    params  = nil
  end

  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
    return_hash = {}
    response    = execute_request(options)
    return return_hash  if  response.code == 204 && method == :delete

    if response.body && !response.body.empty?
      # Don't try to call JSON.parse on empty response body.
      return_hash = JSON.parse(response.body, :symbolize_names => true)
    end
    return return_hash

  rescue RestClient::Exception => e
    handle_errors e
  end
end