Class: Desuraify::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/desuraify/base.rb

Overview

base class to inherit, hopefully reducing duplicate code

Direct Known Subclasses

Company, Engine, Game, Member

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ Base



11
12
13
14
15
16
17
# File 'lib/desuraify/base.rb', line 11

def initialize(id, options={})
  @id = id
  @hydra = options[:hydra] || Desuraify.hydra
  @request_opts = options[:request_opts] || {}
  @callback = nil
  @error = nil
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



8
9
10
# File 'lib/desuraify/base.rb', line 8

def callback
  @callback
end

#errorObject (readonly)

Returns the value of attribute error.



9
10
11
# File 'lib/desuraify/base.rb', line 9

def error
  @error
end

#hydraObject (readonly)

Returns the value of attribute hydra.



7
8
9
# File 'lib/desuraify/base.rb', line 7

def hydra
  @hydra
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/desuraify/base.rb', line 6

def id
  @id
end

Instance Method Details

#enqueue_update(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/desuraify/base.rb', line 27

def enqueue_update(&block)
  @callback = block
  @error = nil

  request = Typhoeus::Request.new(url, @request_opts)

  request.on_complete do |response|
    result = nil

    begin
      result = handle_response(response)
    rescue Exception => e
      @error = e
    end

    update_callback(result)
  end

  hydra.queue(request)

  self
end

#parse_headers(headers) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/desuraify/base.rb', line 50

def parse_headers(headers)
  result = Hash.new

  headers.each do |header|
    next if header.text.strip.empty?

    attribute = header.text.strip.split(" ").join("_").downcase.intern

    case header.text.strip
    when /^Platforms?$/i

      result[:platforms] = header.parent.children.search('a').select do |platform|
        platform unless platform == header || platform.text.strip.empty?
      end.map! {|platform| platform.text.strip }.uniq

    when /^Engine$/i

      result[:engines] = header.parent.children.select do |engine|
        engine unless engine == header || engine.text.strip.empty?
      end.map do |engine|
        eng = Hash.new
        eng[:name] = engine.text.strip
        eng[:id] = engine.child.attribute('href').value.split('/').last rescue nil
        eng
      end.uniq

    when /^Developers?/i, /Publishers?$/i

      attribute = header.text.downcase
      attribute << "s" unless attribute[-1] == "s"

      result[attribute.intern] = header.parent.children.select do |entity|
        entity unless entity == header || entity.text.strip.empty?
      end.map do |entity|
        target = Hash.new
        target[:name] = entity.text.strip
        href = entity.child.attribute('href').value.split('/')
        target[:company] = !!href.find{|company| company.match(/^company$/i)}
        target[:id] = href.last
        target
      end.uniq

    when /Languages?/i, /^Genres?/i, /^Themes?$/i, "Players", /^Projects?$/

      attribute = header.text.downcase
      attribute << "s" unless attribute[-1] == "s"

      result[attribute.intern] = header.parent.children.select do |entity|
        entity unless entity == header || entity.text.strip.empty?
      end.map! {|entity| entity.text.strip }.uniq

    when /^This game is an expansion for\s+?/i

      match = header.text.strip.match(/^This game is an expansion for\s+?(.*)/i)
      expansion = Hash.new
      expansion[:title] = match[match.size-1]
      expansion[:boxshot] = header.parent.search('img').attribute('src').value.strip
      expansion[:id] = header.parent.search('a').attribute('href').value.split('/').last.strip
      result[:expansion] = expansion

    when "Boxshot"

      result[attribute] = header.parent.search('a').attribute('href').value.strip rescue nil

    when "Last Update"

      result[:updated] = header.next.next.text.strip rescue nil

    when "News", "Members", "Videos", "Games", "Images", "Engines"

      attribute = header.text.strip.split(" ").push("count").join("_").downcase.intern
      result[attribute] = header.next.next.text.strip.to_i rescue nil


    when "Visits", "Profile Visitors"

      result[:visits] = header.next.next.text.strip rescue nil

    when "Official Page", "Homepage"

      result[:official_page] = header.parent.search('a').attribute('href').value.strip rescue nil

    when "Offline Since"

      result[:offline] = header.next.next.text.strip rescue nil

    when "Activity Points"

      result[:activity_points] = header.next.next.text.strip.to_i rescue nil

    when "Company", "Office", "Time Online", "Site Visits", /^Gender/, /^Country/, "Established", "Watchers", "Rank", "License", "Release Date", "Phone"

      result[attribute] = header.next.next.text.strip rescue nil

    when "Address"

      addresses = Array.new

      header.parent.children.each do |child|
        next if child == header || child.text.strip.empty?
        break if child.text.strip == "Phone"
        addresses << child.text.strip
      end

      result[:address] = addresses.map { |address| address.split("\n") }.flatten

    end

  end

  result[:engines_count]  = result[:engines_count]  || 0
  result[:images_count]   = result[:images_count]   || 0
  result[:videos_count]   = result[:videos_count]   || 0 
  result[:games_count]    = result[:games_count]    || 0 
  result[:news_count]     = result[:news_count]     || 0 

  result
end

#parse_similar(doc, img_count = 0, vid_count = 0) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/desuraify/base.rb', line 169

def parse_similar(doc, img_count=0, vid_count=0)
  result = Hash.new

  result[:rating] = doc.at_css('.score').text.strip.to_f rescue nil
  result[:level]  = result[:rating]

  result[:videos] = doc.css('.videobox').search('a').map do |video|
    values = video.attribute('href').value.strip.split('/')
    values.pop
    "http://www.desura.com#{values.join('/')}"
  end rescue nil

  result[:videos] = rss_update(video_rss) if result[:videos].size < vid_count rescue nil

  result[:images] = doc.css('.mediaitem').search('a').select{|item| item if item.attribute('href').value.match(/^https?/i) }.map{|pic| pic.attribute('href').value.strip }
  result[:images] = rss_update(image_rss) if result[:images].size < img_count rescue nil

  result[:summary] = doc.at_css('.body.clear').search('p').map{ |paragraph| paragraph.text.strip }
  result[:page_title] = doc.css('title').text.strip
  result[:title] = doc.at_css('.title').css('h2').text.strip rescue nil

  result
end

#rss_update(url) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/desuraify/base.rb', line 197

def rss_update(url)
  response = Typhoeus::Request.get(url, @request_opts)
  if response.success?
    xml = Nokogiri::XML(response.body)
    data = xml.search('enclosure').map{|item| item.attribute('url').value.strip }

    block_given? ? (yield data) : data
  end
end

#to_sObject



193
194
195
# File 'lib/desuraify/base.rb', line 193

def to_s
  "#{@title}" rescue "#{self.class}::#{self.object_id}"
end

#updateObject



19
20
21
22
23
24
25
# File 'lib/desuraify/base.rb', line 19

def update
  resp = Typhoeus::Request.get(url, @request_opts)
  result = handle_response(resp)
  update_callback(result)

  self
end