Class: Services::Facebook

Inherits:
Service
  • Object
show all
Defined in:
app/models/services/facebook.rb

Constant Summary collapse

OVERRIDE_FIELDS_ON_FB_UPDATE =
[:contact_id, :person_id, :request_id, :invitation_id, :photo_url, :name, :username]
MAX_CHARACTERS =
63206

Instance Method Summary collapse

Instance Method Details

#create_post_params(post) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/services/facebook.rb', line 23

def create_post_params(post)
  message = post.message.plain_text_without_markdown
  if post.photos.any?
    message += " " + short_post_url(post, protocol: AppConfig.pod_uri.scheme,
                                          host: AppConfig.pod_uri.authority)
  end

  {message: message,
   access_token: access_token,
   link: URI.extract(message, ['https', 'http']).first
  }
end

#delete_from_facebook(url, body) ⇒ Object


47
48
49
# File 'app/models/services/facebook.rb', line 47

def delete_from_facebook(url, body)
  Faraday.delete(url, body)
end

#delete_post(post) ⇒ Object


40
41
42
43
44
45
# File 'app/models/services/facebook.rb', line 40

def delete_post(post)
  if post.present? && post.facebook_id.present?
    Rails.logger.debug("event=delete_from_service type=facebook sender_id=#{self.user_id}")
    delete_from_facebook("https://graph.facebook.com/#{post.facebook_id}/", {:access_token => self.access_token})
  end
end

#post(post, url = '') ⇒ Object


11
12
13
14
15
16
17
# File 'app/models/services/facebook.rb', line 11

def post(post, url='')
  Rails.logger.debug("event=post_to_service type=facebook sender_id=#{self.user_id}")
  response = post_to_facebook("https://graph.facebook.com/me/feed", create_post_params(post).to_param)
  response = JSON.parse response.body
  post.facebook_id = response["id"]
  post.save
end

#post_to_facebook(url, body) ⇒ Object


19
20
21
# File 'app/models/services/facebook.rb', line 19

def post_to_facebook(url, body)
  Faraday.post(url, body)
end

#profile_photo_urlObject


36
37
38
# File 'app/models/services/facebook.rb', line 36

def profile_photo_url
 "https://graph.facebook.com/#{self.uid}/picture?type=large&access_token=#{URI.escape(self.access_token)}"
end

#providerObject


7
8
9
# File 'app/models/services/facebook.rb', line 7

def provider
  "facebook"
end