Class: InsoundApi::Results

Inherits:
Object
  • Object
show all
Defined in:
lib/insound_api/results.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Results

takes a nokogiri document



7
8
9
# File 'lib/insound_api/results.rb', line 7

def initialize(document)
  @doc = document
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



4
5
6
# File 'lib/insound_api/results.rb', line 4

def doc
  @doc
end

#warningsObject (readonly)

Returns the value of attribute warnings.



4
5
6
# File 'lib/insound_api/results.rb', line 4

def warnings
  @warnings
end

Class Method Details

.parse_int(selector, doc) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/insound_api/results.rb', line 47

def self.parse_int(selector, doc)
  nodes = doc.css(selector)
  if nodes.any?
    nodes.first.inner_html.strip.to_i
  else
    0
  end
end

.parse_str(selector, doc) ⇒ Object



60
61
62
63
64
65
# File 'lib/insound_api/results.rb', line 60

def self.parse_str(selector, doc)
  nodes = doc.css(selector)
  if nodes.any?
    nodes.first.inner_html.strip
  end
end

Instance Method Details

#artistsObject



23
24
25
# File 'lib/insound_api/results.rb', line 23

def artists
  @artists ||= parse_objects('artist_matches artist', Artist)
end

#artists_totalObject



27
28
29
# File 'lib/insound_api/results.rb', line 27

def artists_total
  @artists_total ||= parse_int('total_artist_results')
end

#parse_int(selector) ⇒ Object



43
44
45
# File 'lib/insound_api/results.rb', line 43

def parse_int(selector)
  Results.parse_int(selector, doc)
end

#parse_objects(selector, klass) ⇒ Object



37
38
39
40
# File 'lib/insound_api/results.rb', line 37

def parse_objects(selector, klass)
  nodes = doc.css(selector)
  nodes.map{|n| klass.new(n) }
end

#parse_str(selector) ⇒ Object



56
57
58
# File 'lib/insound_api/results.rb', line 56

def parse_str(selector)
  Results.parse_str(selector, doc)
end

#productsObject



15
16
17
# File 'lib/insound_api/results.rb', line 15

def products
  @products ||= parse_objects('product_matches product', Product)
end

#products_totalObject



19
20
21
# File 'lib/insound_api/results.rb', line 19

def products_total
  @products_total ||= parse_int('total_product_results')
end

#search_urlObject



11
12
13
# File 'lib/insound_api/results.rb', line 11

def search_url
  @search_url ||= parse_str('search_url')
end

#total_resultsObject



31
32
33
# File 'lib/insound_api/results.rb', line 31

def total_results
  @total_results ||= parse_int('total_results')
end