Sha256: 183d00f3d3eee1707050d10cf976f4549cc092294586b4203ff462d01f882617

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'uri'
require 'json'

module Yahoo
  class AdsEstimates

    attr_reader :keyword
    attr_reader :budgets
    attr_reader :max_cpcs, :impressions, :clicks

    def initialize(_keyword, _budgets=[1.0, 50.0])
      @keyword = _keyword
      @budgets = _budgets

      @max_cpcs = []
      @impressions = []
      @clicks = []

      @budgets.each do |budget|
        begin
          json = query(budget)
          result = JSON.parse(json)
          @impressions << result['impressions'].to_f
          @clicks << result['clicks'].to_f
          @max_cpcs << result['maxBid'].to_f
        rescue
          RAILS_DEFAULT_LOGGER.info "Problem querying and parsing Yahoo, json: #{json}"
        end
      end
    end

    private

    def request_uri(_budget)
      # From: http://sem.smallbusiness.yahoo.com/searchenginemarketing/marketingcost.php
      _keyword_escaped = URI.escape(@keyword)
      "http://sem.smallbusiness.yahoo.com/inc/getKeywordForecast.php?format=json&budget=#{_budget}&keyword=#{_keyword_escaped}&target=none"
    end

    def query(_budget)
      open(request_uri(_budget)) { |f| return f.string }
      nil
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yahoo_ads_estimates-0.1.2 lib/yahoo/ads_estimates.rb