Sha256: 63a83cab764a9d00564b5c76da7d1d874cc8d8cbe8c2c8f632183e267a24683a

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Scouter
  class Buffer < Scouter::Base::Object
    END_POINT = 'https://api.bufferapp.com/1'.freeze

    # get Buffer Share Count
    # @param [String or Array] urls
    def self.get_count(urls)
      urls = check_and_trans_url(urls)
      results, errors = {}, []
      urls.each_with_index do |u, idx|
        sleep(WAIT_SEC) if idx != 0
        res, error = get_and_parse_response(u)
        errors << error && next if error
        results.merge!(res)
      end
      res_hash = Hashie::Mash.new(results)
      return [res_hash, errors]
    end

    private

    # Get and parse response data
    # @return [String] urls URL list
    # @return [String] service name
    def self.get_and_parse_response(url)
      html  = get_response(api_url(url))
      res   = parse_response(html, url)
      return [res, nil]
    rescue => e
      message = "#{e.message}, url: #{url}"
      return [nil, message]
    end

    # build Feedly API URL
    # @param [String] url
    # @return [String] API url
    def self.api_url(url)
      "#{END_POINT}/links/shares.json?url=#{url}"
    end

    # Parse json data for response
    # @param [Hash] json
    # @return [Hash] url & count
    def self.parse_response(json, url)
      res = JSON.parse(json)
      { url => { self.service_name => res['shares'] } }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scouter-0.0.3 lib/scouter/buffer.rb
scouter-0.0.2 lib/scouter/buffer.rb
scouter-0.0.1 lib/scouter/buffer.rb