Class: ViewText

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

Instance Method Summary collapse

Constructor Details

#initialize(target_url) ⇒ ViewText

Returns a new instance of ViewText.



13
14
15
# File 'lib/viewtext/viewtext.rb', line 13

def initialize(target_url)
  @target_url = target_url
end

Instance Method Details

#connection(url, opts = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/viewtext/viewtext.rb', line 36

def connection(url, opts={})
  if ENV['http_proxy']
    opts.merge!({ :proxy => ENV['http_proxy'] })
  end

  conn = Faraday.new(url, opts)
end

#fetchObject



21
22
23
24
25
# File 'lib/viewtext/viewtext.rb', line 21

def fetch
  response = fetch_data
  body_json = JSON.parse(response.body)
  ViewTextResponse.new(body_json)
end

#fetch_dataObject



27
28
29
30
31
32
33
34
# File 'lib/viewtext/viewtext.rb', line 27

def fetch_data
  conn = connection(viewtext_url)
  begin
    data = conn.get
  rescue Faraday::Error::ParsingError => e
    return nil
  end
end

#viewtext_urlObject



17
18
19
# File 'lib/viewtext/viewtext.rb', line 17

def viewtext_url
  "http://viewtext.org/api/text?url=#{@target_url}&format=json"
end