class FansWatch::FbApi

Service for all FB API calls

Constants

API_VER
FB_API_URL
FB_TOKEN_URL
FB_URL

Public Class Methods

access_token() click to toggle source
# File lib/fanswatch/fb_api.rb, line 21
def self.access_token
  return @access_token if @access_token

  access_token_response =
    HTTP.get(FB_TOKEN_URL,
             params: { client_id: ENV['FB_CLIENT_ID'],
                       client_secret: ENV['FB_CLIENT_SECRET'],
                       grant_type: 'client_credentials' })
  @access_token = access_token_response.parse['access_token']
end
config() click to toggle source
# File lib/fanswatch/fb_api.rb, line 36
def self.config
  return @config if @config
  @config = { client_id:     ENV['FB_CLIENT_ID'],
              client_secret: ENV['FB_CLIENT_SECRET'] }
end
config=(credentials) click to toggle source
# File lib/fanswatch/fb_api.rb, line 32
def self.config=(credentials)
  @config ? @config.update(credentials) : @config = credentials
end
new(client_id:, client_secret:) click to toggle source
# File lib/fanswatch/fb_api.rb, line 12
def initialize(client_id:, client_secret:)
  access_token_response = 
    HTTP.get(FB_TOKEN_URL,
                                     params: { client_id: ENV['FB_CLIENT_ID'],
                                                                             client_secret: ENV['FB_CLIENT_SECRET'],
                                                                             grant_type: 'client_credentials' })
  @access_token = JSON.load(access_token_response.to_s)['access_token'] 
end
page_feed(page_id) click to toggle source

Get the collection of fans page's postings' (message, created_time and id)

ex : {
        "message"=> "日前金管會確認 Apple Pay、Samsung Pay 等行動支付服務將可進入台灣市場運作後,Google 所提出 Android Pay 也確認將可進入台灣市場。而從相關說法透露,Android Pay 最快將會在近期內於台灣市場推行,但具體時間依然要等 Google 說明。",
        "created_time"=>"2016-11-22T05:58:05+0000",
        "id"=>"159425621565_10153840498126566"
      },

                           .
                           .
                           .

     {
        "message"=>"橘子集團發表新的揪團通訊 App:  BeanGo! 記者會",
        "created_time"=>"2016-11-22T05:38:12+0000",
        "id"=>"159425621565_10153840469861566"
      }
# File lib/fanswatch/fb_api.rb, line 79
def self.page_feed(page_id)
  feed_response = 
    HTTP.get(URI.join(fb_resource_url(page_id), 'feed'),
             params: { access_token: access_token })
  JSON.load(feed_response.to_s)['data']
end
page_id(url) click to toggle source

Get page_id by url

ex:  http "https://graph.facebook.com/v2.8/cyberbuzz? 
       access_token=1311663858901254|UhNPeFDGUdXZVJwagwNxBK49t-4"

     {
         "id": "159425621565",
         "name": "Inside 硬塞的網路趨勢觀察"
     }
# File lib/fanswatch/fb_api.rb, line 50
def self.page_id(url)
  fb_page_name = url.split('/')[3]
  response = fb_resource(fb_page_name)
  return response['id']
end
page_info(page_id) click to toggle source

Get the fans page's name and id

ex: @id="159425621565",
    @name="Inside 硬塞的網路趨勢觀察"
# File lib/fanswatch/fb_api.rb, line 59
def self.page_info(page_id)
  fb_resource(page_id)
end
posting(posting_id) click to toggle source

Get the single posting's basic information by posting id

which we can get from page_feed
ex : {
        "created_time"=>"2016-11-22T03:44:08+0000",
        "message"=>"網路中立性原則要求網路服務供應商及政府應平等處理所有網路上的資料,不差別對待或依不同用戶、內容、網站、平台、應用、接取裝置類型或通訊模式而差別收費。而這也是早逝的 RSS 共同開發者與 Reddit 創辦人 Aaron Swartz 長期以來所不斷捍衛的信念和目標。",
        "id"=>"159425621565_10153840361851566"
      }
# File lib/fanswatch/fb_api.rb, line 93
def self.posting(posting_id) 
  fb_resource(posting_id) 
end
posting_attachments(posting_id) click to toggle source

Get the attachment of a posting

ex : {
        "description"=>"美國下一任總統川普正式任命兩名顧問,以幫助他完成在聯邦通信委員會監督(FCC)的過渡。這兩名顧問分別是 Jeff Eisenach 和 Mark Jamison,均是網路中立性原則的激烈反對者。",
        "media"=>
         {
            "image"=>
             {
               "height"=>720,
               "src"=>"https://external.xx.fbcdn.net/safe_image.php?d=AQBO6Ai9foDoVI1f&w=720&h=720&url=https%3A%2F%2Fwww.inside.com.tw%2Fwp-content%2Fuploads%2F2016%2F11%2F24343769071_d409f67726_k.jpg&cfs=1&sx=450&sy=0&sw=1365&sh=1365",
               "width"=>720
             }
         },
        "target"=>
         {
            "url"=>"https://www.facebook.com/l.php?u=https%3A%2F%2Fwww.inside.com.tw%2F2016%2F11%2F22%2Fdonald-j-trump-net-neutrality&h=ATO8VyRbirwfQAQraYgt8e1aQEoG6oAneIqomLzhe0gGVg_0iE5TAeJhKyhjRzwuCtvKy2mDXL6iSKtwmB6ABCLbyE8&s=1&enc=AZMg3ju-UJWf_VvBESGjeaIUYH7vIVJLQaULvMxBrH0BI7tKrX3KXKyvL-oU5dMdUzANLjxddUlsQOX7Auz-sChD"
         },
         "title"=>"川普的 FCC 團隊,可能會終結網路中立性原則",
         "type"=>"share",
         "url"=>"https://www.facebook.com/l.php?u=https%3A%2F%2Fwww.inside.com.tw%2F2016%2F11%2F22%2Fdonald-j-trump-net-neutrality&h=ATO8VyRbirwfQAQraYgt8e1aQEoG6oAneIqomLzhe0gGVg_0iE5TAeJhKyhjRzwuCtvKy2mDXL6iSKtwmB6ABCLbyE8&s=1&enc=AZMg3ju-UJWf_VvBESGjeaIUYH7vIVJLQaULvMxBrH0BI7tKrX3KXKyvL-oU5dMdUzANLjxddUlsQOX7Auz-sChD"
      }
# File lib/fanswatch/fb_api.rb, line 118
def self.posting_attachments(posting_id)
  attachments_response = 
    HTTP.get(URI.join(fb_resource_url(posting_id), 'attachments'), 
             params: { access_token: access_token })
    JSON.load(attachments_response.to_s)['data'].first
end

Private Class Methods

fb_resource(id) click to toggle source
# File lib/fanswatch/fb_api.rb, line 131
def self.fb_resource(id)
  response = HTTP.get(
    fb_resource_url(id),
    params: {access_token: access_token })
  JSON.load(response.to_s)
end
fb_resource_url(id) click to toggle source
# File lib/fanswatch/fb_api.rb, line 127
def self.fb_resource_url(id) 
  URI.join(FB_API_URL, "/#{id}/") 
end