Module: Dito

Defined in:
lib/dito.rb,
lib/dito/alias.rb,
lib/dito/track.rb,
lib/dito/domains.rb,
lib/dito/request.rb,
lib/dito/version.rb,
lib/dito/identify.rb,
lib/dito/configuration.rb,
lib/helpers/symbolize_keys.rb,
lib/helpers/generate_credentials.rb

Defined Under Namespace

Classes: Configuration, Request

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.alias(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/dito/alias.rb', line 4

def self.alias(options = {})
  Dito.symbolize_keys!(options)

  settings = Dito.generate_alias_settings(options)

  return { :error => settings[:error] } if settings[:error].present?

  Dito::Request.post 'login', "/users/#{settings[:id]}/link", settings[:params]
end

.api_keyObject



28
29
30
# File 'lib/dito.rb', line 28

def api_key
  configuration.api_key
end

.configurationObject



20
21
22
# File 'lib/dito.rb', line 20

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



24
25
26
# File 'lib/dito.rb', line 24

def configure
  yield(configuration)
end

.domains(module_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dito/domains.rb', line 4

def self.domains module_name
  domains = {
    :js => 'js',
    :analytics => 'analytics',
    :login => 'login',
    :events => 'events',
    :share => 'share',
    :comments => 'comments',
    :ranking => 'ranking',
    :badge => 'badge',
    :notification => 'notification'
  }

  if domains[module_name.to_sym].present?
    name = domains[module_name.to_sym]

    url = case @configuration.environment
      when "production"
        "https://#{name}.plataformasocial.com.br"
      when "development" 
        "http://#{name}.dev.plataformasocial.com.br"
      when "test"
        "http://#{name}.dev.plataformasocial.com.br"
      when "staging"
        "http://#{name}.dev.plataformasocial.com.br"
      end
      
    url
  end
end

.environmentObject



36
37
38
# File 'lib/dito.rb', line 36

def environment
  configuration.environment
end

.generate_alias_settings(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dito/alias.rb', line 24

def self.generate_alias_settings(options = {})
  id, id_type = Dito.generate_credentials(options)

  return { :error => { :message => 'Missing the user id param. See the available options here: http://developers.dito.com.br/docs/sdks/ruby' } } if id.blank?
  return { :error => { :message => 'Missing the accounts param. See the available options here: http://developers.dito.com.br/docs/rest-api/users' } } if options[:accounts].blank?

  params = { :accounts => options[:accounts] }
  params[:id_type] = id_type if id_type.present?

  { :id => id, :params => params }
end

.generate_credentials(options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/helpers/generate_credentials.rb', line 6

def self.generate_credentials(options)
  if options[:reference].present?
    id = options[:reference]
    id_type = nil
  elsif options[:facebook_id].present?
    id = options[:facebook_id]
    id_type = 'facebook_id'
  elsif options[:google_plus_id].present?
    id = options[:google_plus_id]
    id_type = 'google_plus_id'
  elsif options[:twitter_id].present?
    id = options[:twitter_id]
    id_type = 'twitter_id'
  elsif options[:id].present?
    id = options[:id]
    id_type = 'id'
  else
    id = nil
    id_type = nil
  end

  return id, id_type
end

.identify(user = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dito/identify.rb', line 4

def self.identify user = {}
  Dito.symbolize_keys!(user)
  
  params = {}

  if user[:facebook_id].present?
    network_name = 'facebook'
    id = user[:facebook_id]
    params[:network_name] = 'fb'
  elsif user[:google_plus_id].present?
    network_name = 'plus'
    id = user[:google_plus_id]
    params[:network_name] = 'pl'
  elsif user[:twitter_id].present?
    network_name = 'twitter'
    id = user[:twitter_id]
    params[:network_name] = 'tw'
  elsif user[:id].present?
    network_name = 'portal'
    id = user[:id]
    params[:network_name] = 'pt'
  else
    return { :error => { :message => 'Missing the user id param. See the available options here: http://developers.dito.com.br/docs/sdks/ruby' } }
  end

  params[:user_data] = {}

  if network_name == 'portal'
    params[:user_data].merge!(user)
    params[:user_data].delete(:id)
  else
    params[:user_data][:data] = user[:data]
  end

  if params[:user_data][:data].present? && params[:user_data][:data].is_a?(Hash)
    params[:user_data][:data] = params[:user_data][:data].to_json
  end

  params[:access_token] = user[:access_token] if user[:access_token].present?
  params[:signed_request] = user[:signed_request] if user[:signed_request].present?
  params[:id_token] = user[:id_token] if user[:id_token].present?

  Dito::Request.post 'login', "/users/#{network_name}/#{id}/signup", params
end

.rootObject



15
16
17
# File 'lib/dito.rb', line 15

def self.root
  File.expand_path '../..', __FILE__
end

.secretObject



32
33
34
# File 'lib/dito.rb', line 32

def secret
  configuration.secret
end

.symbolize_keys(thing) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/helpers/symbolize_keys.rb', line 17

def self.symbolize_keys(thing)
  case thing
  when Array
    thing.map{|v| symbolize_keys(v)}
  when Hash
    inj = thing.inject({}) {|h, (k,v)| h[k] = symbolize_keys(v); h}
    inj.symbolize_keys
  else
    thing
  end
end

.symbolize_keys!(thing) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/helpers/symbolize_keys.rb', line 6

def self.symbolize_keys!(thing)
  case thing
  when Array
    thing.each{|v| symbolize_keys!(v)}
  when Hash
    thing.symbolize_keys!
    thing.values.each{|v| symbolize_keys!(v)}
  end
  thing
end

.track(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/dito/track.rb', line 4

def self.track options = {}
  Dito.symbolize_keys!(options)

  id, id_type = Dito.generate_credentials(options)
  
  return { :error => { :message => 'Missing the user id param. See the available options here: http://developers.dito.com.br/docs/sdks/ruby' } } if id.blank?

  params = { :event => options[:event].to_json }
  params[:id_type] = id_type if id_type.present?

  Dito::Request.post 'events', "/users/#{id}/", params
end

.unalias(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/dito/alias.rb', line 14

def self.unalias(options = {})
  Dito.symbolize_keys!(options)
  
  settings = Dito.generate_alias_settings(options)
  
  return { :error => settings[:error] } if settings[:error].present?

  Dito::Request.post 'login', "/users/#{settings[:id]}/unlink", settings[:params]
end