Class: Fboauth2::Newfbclient

Inherits:
Object
  • Object
show all
Defined in:
lib/Fboauth2.rb

Class Method Summary collapse

Class Method Details

.auth_callback(code, url) ⇒ Object



25
26
27
28
29
# File 'lib/Fboauth2.rb', line 25

def self.auth_callback(code, url)
  client = @facebook_client
  client.authorization.process_callback(code, :redirect_uri => url)
  client
end

.crate_new_m(info, params, user) ⇒ Object



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
79
80
81
82
83
# File 'lib/Fboauth2.rb', line 45

def self.crate_new_m(info, params, user)
  cname = params[:config]
  model_type = info[cname]["model"]["name"]
  m_data = info[cname]["model"]["params"]
  
  puts "-"*50
  puts "Creating #{model_type}..."
  
  m_data.each do |field, value|
    aux = value.to_s.split("_")
    if aux[0] == "fb"
      value = user["#{aux[1]}"]
      m_data[field] = value
    end  
  end
  
  if info[cname]["hsubmodel"] == "t" #si tiene algun tipo de subcategoria?
    sm_data = info[cname]["submodel"]["params"]
    sm_name = info[cname]["submodel"]["name"]
    sm_data["#{sm_name}"][0].each do |field, value|

      aux = value.to_s.split("_")
       if aux[0] == "fb"
         sm_data["#{sm_name}"][0][field] = user["#{aux[1]}"]
       end
       if aux[0] == "pm"
         sm_data["#{sm_name}"][0][field] = params[aux[1].to_sym]
       end
    end
    m_data = m_data.merge(sm_data)
  end
  
  m_aux = model_type.constantize.create(m_data)
  
  puts "-> ID: " + m_aux.id.to_s
  puts "-> Ready!"
  
  puts "-"*50
end

.get_auth(host, params) ⇒ Object



14
15
16
17
18
19
# File 'lib/Fboauth2.rb', line 14

def self.get_auth(host, params)
  @params = params
  url = Newfbclient.get_url(host)
  @facebook_client =  Newfbclient.get_client #creo un nuevo cliente
  @facebook_client.authorization.authorize_url(:redirect_uri => url , :scope => @fb_data["credentials"][Rails.env]["permission"]) #pido autorizacion q me regresa una url a facebook
end

.get_clientObject



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

def self.get_client
  FBGraph::Client.new(:client_id => @fb_data["credentials"][Rails.env]["api_key"], :secret_id => @fb_data["credentials"][Rails.env]["api_secret"]) 
end

.get_conf_dataObject



41
42
43
# File 'lib/Fboauth2.rb', line 41

def self.get_conf_data
  YAML.load_file("#{RAILS_ROOT}/config/fbconfig.yml")
end

.get_redirect_path(params) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/Fboauth2.rb', line 91

def self.get_redirect_path(params)
  url = @fb_data[params[:config]]['redirect'].split(" ")
  url_f = ""
  url.each do |value|
    aux = value.split("_")
    if aux[0] == "pm"
      url_f +=  params[aux[1].to_sym]
    else
      url_f += value
    end  
  end
  puts url_f
  return url_f
end

.get_url(host) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/Fboauth2.rb', line 31

def self.get_url(host)
  @aux_params = ""
  @params.delete("controller")
  @params.delete("action")
  @params.each do |key, value|        
    @aux_params = @aux_params + key + "=" + value + "&"
  end
  "http://"+ host + "/oauth2_facebook/fb_callback?" + @aux_params #get url para el callback
end

.send_fb_msg(cname, user) ⇒ Object



86
87
88
89
# File 'lib/Fboauth2.rb', line 86

def self.send_fb_msg(cname, user)
  @facebook_client.selection.user(user[:id]).feed.publish!(:message => @fb_data[cname]["facebook"]["message"], :name => @fb_data[cname]["facebook"]["title"], :link => @fb_data[cname]["facebook"]["link"], :picture => @fb_data[cname]["facebook"]["picture"], :description => @fb_data[cname]["facebook"]["description"])
  puts "-> Facebook Message, successfully published..."    
end