Class: MarketBot::Android::App

Inherits:
Object
  • Object
show all
Defined in:
lib/market_bot/android/app.rb

Constant Summary collapse

MARKET_ATTRIBUTES =
[:title, :rating, :updated, :current_version, :requires_android,
:category, :installs, :size, :price, :content_rating, :description,
:votes, :developer, :more_from_developer, :users_also_installed,
:related]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, options = {}) ⇒ App

Returns a new instance of App.



83
84
85
86
87
# File 'lib/market_bot/android/app.rb', line 83

def initialize(app_id, options={})
  @app_id = app_id
  @hydra = options[:hydra] || Typhoeus::Hydra.hydra
  @callback = nil
end

Instance Attribute Details

#app_idObject (readonly)

Returns the value of attribute app_id.



10
11
12
# File 'lib/market_bot/android/app.rb', line 10

def app_id
  @app_id
end

#callbackObject (readonly)

Returns the value of attribute callback.



13
14
15
# File 'lib/market_bot/android/app.rb', line 13

def callback
  @callback
end

#hydraObject (readonly)

Returns the value of attribute hydra.



12
13
14
# File 'lib/market_bot/android/app.rb', line 12

def hydra
  @hydra
end

Class Method Details

.parse(html) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/market_bot/android/app.rb', line 15

def self.parse(html)
  result = {}

  doc = Nokogiri::HTML(html)

  elements = doc.css('.doc-metadata').first.elements[2].elements
  elem_count = elements.count

  (3..(elem_count - 1)).select{ |n| n.odd? }.each do |i|
    field_name  = elements[i].text

    case field_name
    when 'Updated:'
      result[:updated] = elements[i + 1].text
    when 'Current Version:'
      result[:current_version] = elements[i + 1].text
    when 'Requires Android:'
      result[:requires_android] = elements[i + 1].text
    when 'Category:'
      result[:category] = elements[i + 1].text
    when 'Installs:'
      result[:installs] = elements[i + 1].children.first.text
    when 'Size:'
      result[:size] = elements[i + 1].text
    when 'Price:'
      result[:price] = elements[i + 1].text
    when 'Content Rating:'
      result[:content_rating] = elements[i + 1].text
    end
  end

  result[:description] = doc.css('#doc-original-text').first.text
  result[:title] = doc.title.gsub(/ - Android Market$/, '')

  rating_elem = doc.css('.average-rating-value')
  result[:rating] = rating_elem.first.text unless rating_elem.empty?

  votes_elem = doc.css('.votes')
  result[:votes] = doc.css('.votes').first.text unless votes_elem.empty?

  result[:developer] = doc.css('.doc-header-link').first.text

  result[:more_from_developer] = []
  result[:users_also_installed] = []
  result[:related] = []

  if similar_elem = doc.css('.doc-similar').first
    similar_elem.children.each do |similar_elem_child|
      assoc_app_type = similar_elem_child.attributes['data-analyticsid'].text

      next unless %w(more-from-developer users-also-installed related).include?(assoc_app_type)

      assoc_app_type = assoc_app_type.gsub('-', '_').to_sym
      result[assoc_app_type] ||= []

      similar_elem_child.css('.app-left-column-related-snippet-container').each do |app_elem|
        assoc_app = {}

        assoc_app[:app_id] = app_elem.attributes['data-docid'].text

        result[assoc_app_type] << assoc_app
      end
    end
  end

  result
end

Instance Method Details

#enqueue_update(&block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/market_bot/android/app.rb', line 101

def enqueue_update(&block)
  @callback = block

  request = Typhoeus::Request.new(market_url)

  request.on_complete do |response|
    result = App.parse(response.body)
    update_callback(result)
  end

  hydra.queue(request)

  self
end

#market_urlObject



89
90
91
# File 'lib/market_bot/android/app.rb', line 89

def market_url
  "https://market.android.com/details?id=#{@app_id}"
end

#updateObject



93
94
95
96
97
98
99
# File 'lib/market_bot/android/app.rb', line 93

def update
  resp = Typhoeus::Request.get(market_url)
  result = App.parse(resp.body)
  update_callback(result)

  self
end