Class: ViewText
- Inherits:
-
Object
- Object
- ViewText
- Defined in:
- lib/viewtext/viewtext.rb
Instance Method Summary collapse
- #connection(url, opts = {}) ⇒ Object
- #fetch ⇒ Object
- #fetch_data ⇒ Object
-
#initialize(target_url) ⇒ ViewText
constructor
A new instance of ViewText.
- #viewtext_url ⇒ Object
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 |
#fetch ⇒ Object
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_data ⇒ Object
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_url ⇒ Object
17 18 19 |
# File 'lib/viewtext/viewtext.rb', line 17 def viewtext_url "http://viewtext.org/api/text?url=#{@target_url}&format=json" end |