Class: SerpCheap::SearchResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/serpcheap/models.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search:, page:, organic:, knowledge_graph: nil, ads: nil, people_also_ask: nil, related_searches: nil, stats: nil) ⇒ SearchResponse

Returns a new instance of SearchResponse.



131
132
133
134
135
136
137
138
139
140
# File 'lib/serpcheap/models.rb', line 131

def initialize(search:, page:, organic:, knowledge_graph: nil, ads: nil, people_also_ask: nil, related_searches: nil, stats: nil)
  @search = search
  @page = page
  @organic = organic
  @knowledge_graph = knowledge_graph
  @ads = ads
  @people_also_ask = people_also_ask
  @related_searches = related_searches
  @stats = stats
end

Instance Attribute Details

#adsObject (readonly)

Returns the value of attribute ads.



129
130
131
# File 'lib/serpcheap/models.rb', line 129

def ads
  @ads
end

#knowledge_graphObject (readonly)

Returns the value of attribute knowledge_graph.



129
130
131
# File 'lib/serpcheap/models.rb', line 129

def knowledge_graph
  @knowledge_graph
end

#organicObject (readonly)

Returns the value of attribute organic.



129
130
131
# File 'lib/serpcheap/models.rb', line 129

def organic
  @organic
end

#pageObject (readonly)

Returns the value of attribute page.



129
130
131
# File 'lib/serpcheap/models.rb', line 129

def page
  @page
end

#people_also_askObject (readonly)

Returns the value of attribute people_also_ask.



129
130
131
# File 'lib/serpcheap/models.rb', line 129

def people_also_ask
  @people_also_ask
end

Returns the value of attribute related_searches.



129
130
131
# File 'lib/serpcheap/models.rb', line 129

def related_searches
  @related_searches
end

#searchObject (readonly)

Returns the value of attribute search.



129
130
131
# File 'lib/serpcheap/models.rb', line 129

def search
  @search
end

#statsObject (readonly)

Returns the value of attribute stats.



129
130
131
# File 'lib/serpcheap/models.rb', line 129

def stats
  @stats
end

Class Method Details

.from_hash(d) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/serpcheap/models.rb', line 142

def self.from_hash(d)
  kg = d["knowledgeGraph"]
  ads = d["ads"]
  rs = d["relatedSearches"]
  new(
    search: d["search"].to_s,
    page: (d["page"] || 1).to_i,
    organic: Array(d["organic"]).map { |o| OrganicResult.from_hash(o) },
    knowledge_graph: kg.is_a?(Hash) ? KnowledgeGraph.from_hash(kg) : nil,
    ads: ads.is_a?(Array) ? ads.map { |a| Ad.from_hash(a) } : nil,
    people_also_ask: d["peopleAlsoAsk"],
    related_searches: rs.is_a?(Array) ? rs.map { |r| RelatedSearch.from_hash(r) } : nil,
    stats: d["stats"].is_a?(Hash) ? SearchStats.from_hash(d["stats"]) : nil
  )
end