Class: Cornerstore::Search

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/cornerstore/api/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keywords = nil) ⇒ Search

Returns a new instance of Search.



7
8
9
# File 'lib/cornerstore/api/search.rb', line 7

def initialize(keywords = nil)
  @keywords, @scores, @products = keywords, Array.new, Cornerstore::Product::Resource.new
end

Instance Attribute Details

#keywordsObject

Returns the value of attribute keywords.



4
5
6
# File 'lib/cornerstore/api/search.rb', line 4

def keywords
  @keywords
end

#productsObject Also known as: results

Returns the value of attribute products.



4
5
6
# File 'lib/cornerstore/api/search.rb', line 4

def products
  @products
end

#scoresObject

Returns the value of attribute scores.



4
5
6
# File 'lib/cornerstore/api/search.rb', line 4

def scores
  @scores
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/cornerstore/api/search.rb', line 19

def persisted?
  false
end

#results?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/cornerstore/api/search.rb', line 11

def results?
  !@products.empty?
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cornerstore/api/search.rb', line 23

def run
  return false unless @keywords

  RestClient.get("#{Cornerstore.root_url}/products/search?keywords=#{URI::encode(@keywords)}", Cornerstore.headers) do |response, request, result, &block|
    if response.code == 200
      data = ActiveSupport::JSON.decode(response)
      @scores   = data['scores']
      @products = Cornerstore::Product::Resource.new(nil, data['products'])
      return true
    end
  end
end

#to_keyObject



15
16
17
# File 'lib/cornerstore/api/search.rb', line 15

def to_key
  ['']
end