Class: ShareChecker::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/share_checker/provider.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(link, name = "noname", options = {}) ⇒ Provider

Returns a new instance of Provider.



9
10
11
12
13
# File 'lib/share_checker/provider.rb', line 9

def initialize(link, name = "noname", options = {})
  @link = link
  @name = name
  @options = options.symbolize_keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/share_checker/provider.rb', line 52

def method_missing(method_name, *args, &block)
  if @options.key?(method_name.to_sym)
    @options[method_name.to_sym]
  else
    super
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/share_checker/provider.rb', line 7

def name
  @name
end

Class Method Details

.get(url) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/share_checker/provider.rb', line 61

def get(url)
  c = Curl::Easy.new(url) do |curl| 
    curl.headers["User-Agent"] = ShareChecker.config.user_agent
    curl.timeout = ShareChecker.config.timeout
    curl.verbose = false
    curl.follow_location = true
    curl.max_redirects = 2
  end

  c.perform
  c.body_str
end

Instance Method Details

#checkObject



15
16
17
18
19
20
# File 'lib/share_checker/provider.rb', line 15

def check
  response = self.class.get([url, @link].join)
  return nil unless response
  
  parse(response)
end

#parse(response) ⇒ Object

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/share_checker/provider.rb', line 22

def parse(response)
  raise NotImplementedError, "Must be overwritten in subclasses"
end

#parse_json(body) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/share_checker/provider.rb', line 41

def parse_json(body)
  begin
    content = MultiJson.decode(body)
  rescue Exception => e
    puts "#{@name} error parse xml: #{body}, #{e.message}, #{@link}"
    content = nil
  end
  
  return content
end

#parse_xml(body) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/share_checker/provider.rb', line 30

def parse_xml(body)
  begin
    content = MultiXml.parse(body)
  rescue Exception => e
    puts "#{@name} error parse xml: #{body}, #{e.message}, #{@link}"
    content = nil
  end
  
  return content
end

#urlObject

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/share_checker/provider.rb', line 26

def url
  raise NotImplementedError, "Must be overwritten in subclasses"
end