Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
require 'httparty'

module ActionSprout::Facebook

  class Graph

    BASE_URL = 'https://graph.facebook.com'

    # Legacy default api version. In the future FACEBOOK_API_VERSION will be
    # required.
    API_VERSION = 'v9.0'

    attr_reader :access_token, :api_version, :http_service

    def self.default_instance
      new env_access_token
    end

    def self.env_access_token
      ENV.fetch('FACEBOOK_APP_ACCESS_TOKEN')
    end

    def self.env_api_version
      ENV.fetch('FACEBOOK_API_VERSION', API_VERSION)
    end

    def initialize(access_token, api_version: self.class.env_api_version, http_service: HTTPService.new)
      @access_token = access_token
      @api_version = api_version
      @http_service = http_service
    end

    def get(path, options = {})
      http_service.get url(path), merged_options(options), self
    end

    def post(path, options = {})
      http_service.post url(path), merged_options(options), self
    end

    private

    def url(path)
      path.starts_with?('https://graph.facebook.com/') ? path : build_url(path)
    end

    def build_url(path)
      [BASE_URL, api_version, path].join('/')
    end

    def merged_options(options)
      # options will override base_options
      base_options.deep_merge options
    end

    def base_options
      { query: { access_token: access_token }, format: :json }
    end

  end
end