Class: RubyWebSearch::Bing::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-web-search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(google_raw_response = {}) ⇒ Response

Returns a new instance of Response.



509
510
511
# File 'lib/ruby-web-search.rb', line 509

def initialize(google_raw_response={})
  process(google_raw_response) unless google_raw_response.empty?
end

Instance Attribute Details

#estimated_result_countObject (readonly)

Returns the value of attribute estimated_result_count.



508
509
510
# File 'lib/ruby-web-search.rb', line 508

def estimated_result_count
  @estimated_result_count
end

#queryObject (readonly)

Returns the value of attribute query.



508
509
510
# File 'lib/ruby-web-search.rb', line 508

def query
  @query
end

#resultsObject (readonly)

Returns the value of attribute results.



508
509
510
# File 'lib/ruby-web-search.rb', line 508

def results
  @results
end

#sizeObject (readonly)

Returns the value of attribute size.



508
509
510
# File 'lib/ruby-web-search.rb', line 508

def size
  @size
end

#statusObject (readonly)

Returns the value of attribute status.



508
509
510
# File 'lib/ruby-web-search.rb', line 508

def status
  @status
end

Instance Method Details

#limit(req_size) ⇒ Object



534
535
536
537
# File 'lib/ruby-web-search.rb', line 534

def limit(req_size)
  @results = @results[0...req_size]
  self
end

#process(google_raw_response = {}) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/ruby-web-search.rb', line 513

def process(google_raw_response={})
  @query    ||= google_raw_response[:query]
  @size     ||= google_raw_response[:size]
  @results  ||= []
  @status     = 200
  if google_raw_response["SearchResponse"] && 
          google_raw_response["SearchResponse"]["Web"] &&
          google_raw_response["SearchResponse"]["Web"]["Results"] &&
          status && status == 200
    estimated_result_count ||= google_raw_response["SearchResponse"]["Web"]["Total"]
    @results  +=  google_raw_response["SearchResponse"]["Web"]["Results"].map do |r|
                  {
                    :title      => r["Title"],
                    :url        => r["Url"],
                    :cache_url  => r["CacheUrl"],
                    :content    => r["Description"],
                    :domain     => r["DisplayUrl"]
                  }
                end
  end

  def limit(req_size)
    @results = @results[0...req_size]
    self
  end

end