Class: Kansou::AppStoreReview

Inherits:
Object
  • Object
show all
Defined in:
lib/kansou/app_store_review.rb

Instance Method Summary collapse

Constructor Details

#initialize(app_id) ⇒ AppStoreReview

Returns a new instance of AppStoreReview.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/kansou/app_store_review.rb', line 6

def initialize(app_id)
  return nil unless app_id

  @app_id = app_id

  @users = []
  @versions = []
  @dates = []
  @titles = []
  @stars = []
  @bodies = []
end

Instance Method Details

#download(page = 0) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/kansou/app_store_review.rb', line 29

def download(page = 0)
  base_url = 'https://itunes.apple.com'
  user_agent = 'iTunes/9.2 (Windows; Microsoft Windows 7 '\
                            + 'Home Premium Edition (Build 7600)) AppleWebKit/533.16'

  url = base_url + '/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id='\
    + @app_id.to_s + '&pageNumber=' + page.to_s + '&sortOrdering=4&type=Purple+Software'
  xml = open(url, 'User-Agent' => user_agent, 'X-Apple-Store-Front' => '143462-1').read
  xml
end

#fetch(page_amount = 1) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/kansou/app_store_review.rb', line 19

def fetch(page_amount = 1)
  reviews = []
  max_page = page_amount - 1
  (0..max_page).each do |page|
    xml = download(page)
    reviews.concat(parse(xml))
  end
  reviews
end

#get_version(text) ⇒ Object



119
120
121
122
123
# File 'lib/kansou/app_store_review.rb', line 119

def get_version(text)
  version = nil
  version = Regexp.last_match(1) if /Version([\d\.]+)/ =~ text
  version
end

#parse(xml) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kansou/app_store_review.rb', line 40

def parse(xml)
  document = Oga.parse_xml(xml)

  parse_author_info(document)
  parse_title(document)
  parse_star(document)
  parse_body(document)

  items = []
  count = @stars.size - 1
  (0..count).each do |key|
    item = {
      star: @stars[key],
      user: @users[key],
      date: @dates[key],
      title: @titles[key],
      body: @bodies[key],
      version: @versions[key],
      app_id: @app_id
    }
    items.push(item)
  end
  items
end

#parse_author_info(document) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kansou/app_store_review.rb', line 85

def parse_author_info(document)
  expression = 'TextView[topInset="0"][styleSet="basic13"]' +
    '[squishiness="1"][leftInset="0"][truncation="right"]' +
    '[textJust="left"][maxLines="1"]'
  document.css(expression).each do |elm|
    next unless elm.text =~ /by/

    tmp_array = elm.text.delete(' ').split("\n")
    info = []
    tmp_array.each do |v|
      info.push v if v != '' && v != '-' && v != 'by'
    end
    @users.push info[0]
    push_version(info)
    push_date(info)
  end
end

#parse_body(document) ⇒ Object



79
80
81
82
83
# File 'lib/kansou/app_store_review.rb', line 79

def parse_body(document)
  document.css('TextView[styleSet="normal11"]').each do |elm|
    @bodies.push(elm.text.gsub("\n", '<br />'))
  end
end

#parse_star(document) ⇒ Object



73
74
75
76
77
# File 'lib/kansou/app_store_review.rb', line 73

def parse_star(document)
  document.css('HBoxView[topInset="1"]').each do |elm|
    @stars.push elm.attribute('alt').value.gsub(/ stars*/, '')
  end
end

#parse_title(document) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/kansou/app_store_review.rb', line 65

def parse_title(document)
  document.css('TextView[styleSet="basic13"][textJust="left"][maxLines="1"]').each do |elm|
    elm.css('b').each do |e|
      @titles.push e.text
    end
  end
end

#push_date(info) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/kansou/app_store_review.rb', line 111

def push_date(info)
  if info[2]
    @dates.push(info[2])
  else
    @dates.push('')
  end
end

#push_version(info) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/kansou/app_store_review.rb', line 103

def push_version(info)
  if info[1]
    @versions.push(get_version(info[1]))
  else
    @versions.push('')
  end
end