Class: GpisAppInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/gpis/gpis_app_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_name, docs_by_lang_hash) ⇒ GpisAppInfo

Returns a new instance of GpisAppInfo.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gpis/gpis_app_info.rb', line 13

def initialize(package_name, docs_by_lang_hash)
  @package_name  = package_name
  @documents     = docs_by_lang_hash
  @name          = Hash.new
  @category      = Hash.new
  @permissions   = Hash.new

  # language independent values
  first_document = @documents[@documents.keys.first]
  fill_icon_url(first_document)

  # language specific values
  @documents.each do |language, document|
    fill_name_for_lang(language, document)
    fill_category_for_lang(language, document)
    fill_permissions_for_lang(language, document)
  end
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



9
10
11
# File 'lib/gpis/gpis_app_info.rb', line 9

def category
  @category
end

#documentsObject

Returns the value of attribute documents.



8
9
10
# File 'lib/gpis/gpis_app_info.rb', line 8

def documents
  @documents
end

#icon_urlObject

single value



4
5
6
# File 'lib/gpis/gpis_app_info.rb', line 4

def icon_url
  @icon_url
end

#nameObject

hashes by language



7
8
9
# File 'lib/gpis/gpis_app_info.rb', line 7

def name
  @name
end

#permissionsObject

Returns the value of attribute permissions.



10
11
12
# File 'lib/gpis/gpis_app_info.rb', line 10

def permissions
  @permissions
end

Instance Method Details

#fill_category_for_lang(language, document) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
# File 'lib/gpis/gpis_app_info.rb', line 51

def fill_category_for_lang(language, document)
  begin
    @category[language] = document.css(".doc-metadata a[href^='/store/apps/category/']").inner_html
  rescue Exception => e
    raise GpisParseError.new("Gpis: Parsing Error : #{e.message} :#{e.backtrace.join("\n")}")
  end
  raise GpisParseError.new("Gpis: Parsing Error : category not found for language '#{language}'") if @category[language].nil?
end

#fill_icon_url(document) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
# File 'lib/gpis/gpis_app_info.rb', line 32

def fill_icon_url(document)
  begin
    @icon_url = document.at_css("div.doc-banner-icon > img")['src']
  rescue Exception => e
    raise GpisParseError.new("Gpis: Parsing Error : #{e.message} :#{e.backtrace.join("\n")}")
  end
  raise GpisParseError.new("Gpis: Parsing Error : icon not found") if @icon_url.nil?
end

#fill_name_for_lang(language, document) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
# File 'lib/gpis/gpis_app_info.rb', line 41

def fill_name_for_lang(language, document)
  begin
    @name[language] = document.at_css("dl.doc-metadata-list span[itemprop=name]")['content']
  rescue Exception => e
    raise GpisParseError.new("Gpis: Parsing Error : #{e.message} :#{e.backtrace.join("\n")}")
  end

  raise GpisParseError.new("Gpis: Parsing Error : name not found for language '#{language}'") if @name[language].nil?
end

#fill_permissions_for_lang(language, document) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/gpis/gpis_app_info.rb', line 92

def fill_permissions_for_lang(language, document)
  #permissions are divided into two sections: dangerous and safe. They are stored in two different divs.
  css_selector_for_dangerous = "doc-permissions-dangerous"
  css_selector_for_safe      = "doc-permissions-safe"
  fill_permissions_for_lang_based_on_danger_and_section_id(document, language, css_selector_for_dangerous, true)
  fill_permissions_for_lang_based_on_danger_and_section_id(document, language, css_selector_for_safe, false)
end

#fill_permissions_for_lang_based_on_danger_and_section_id(document, language, section_id, is_dangerous) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gpis/gpis_app_info.rb', line 61

def fill_permissions_for_lang_based_on_danger_and_section_id(document, language, section_id, is_dangerous)
  begin
    language_hash     = @permissions[language] ||= Hash.new
    full_css_selector = "##{section_id} li.doc-permission-group"
    document.css(full_css_selector).each do |group_element| # cycle through permission groups
      group_name    = group_element.css(".doc-permission-group-title").inner_html
      current_group = language_hash[group_name]
      #if the current_group hash hasn't yet been created, create it. All the other permissions for this group will then only be added to it, but the first one has to create it
      if current_group.nil?
        current_group               = Hash.new
        current_group[:name]        = group_name
        current_group[:permissions] = Array.new
        language_hash[group_name]   = current_group
      end
      permission_name         = group_element.css(".doc-permission-description").collect { |el| el.inner_html }
      permission_descriptions = group_element.css(".doc-permission-description-full").collect { |el| el.inner_html }
      permission_name.each_index do |i|
        permission               = Hash.new
        permission[:name]        = permission_name[i]
        permission[:description] = permission_descriptions[i]
        permission[:dangerous]   = is_dangerous
        current_group[:permissions].push(permission)
      end
    end
  rescue Exception => e
    raise GpisParseError.new("Gpis: Parsing Error : #{e.message} :#{e.backtrace.join("\n")}")
  end

end

#to_sObject



100
101
102
# File 'lib/gpis/gpis_app_info.rb', line 100

def to_s
  "{Gpis App Info: name = '#{self.name}', category='#{self.category}', icon_url='#{self.icon_url}', permissions = '#{self.permissions}'}"
end