Module: Docverter

Defined in:
lib/docverter/json.rb,
lib/docverter.rb,
lib/docverter/version.rb,
lib/docverter/conversion.rb

Overview

Defined Under Namespace

Modules: OkJson Classes: APIConnectionError, APIError, AuthenticationError, Conversion, InvalidConversionError

Constant Summary collapse

VERSION =
"1.0.1"
@@api_key =
nil
@@base_url =
"http://c.docverter.com"

Class Method Summary collapse

Class Method Details

.api_keyObject



15
16
17
# File 'lib/docverter.rb', line 15

def self.api_key
  @@api_key
end

.api_key=(key) ⇒ Object



19
20
21
# File 'lib/docverter.rb', line 19

def self.api_key=(key)
  @@api_key = key
end

.api_url(path = '') ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/docverter.rb', line 23

def self.api_url(path='')
  u = URI(@@base_url + path)
  key = self.api_key
  raise AuthenticationError.new('No API key provided. (HINT: set your API key using "Docverter.api_key = <API-KEY>". You can find your API in the Docverter web interface. See http://www.docverter.com/api.html for details, or email [email protected] if you have any questions.)') if key.nil? && @@base_url == 'https://api.docverter.com/v1'
  u.user = key if key
  u.password = '' if key
  u.to_s
end

.base_url=(url) ⇒ Object



32
33
34
# File 'lib/docverter.rb', line 32

def self.base_url=(url)
  @@base_url = url
end

.execute_request(opts) ⇒ Object



84
85
86
# File 'lib/docverter.rb', line 84

def self.execute_request(opts)
  RestClient::Request.execute(opts)
end

.handle_api_error(code, body) ⇒ Object

Raises:



88
89
90
91
# File 'lib/docverter.rb', line 88

def self.handle_api_error(code, body)
  obj = Docverter::OkJson.decode(body) rescue {'error' => body}
  raise APIError.new("Docverter API Error: #{obj['error']} (status: #{code})")
end

.handle_restclient_error(e) ⇒ Object

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/docverter.rb', line 93

def self.handle_restclient_error(e)
  case e
  when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
    message = "Could not connect to Docverter (#{@@api_base}).  Please check your internet connection and try again.  If this problem persists, you should let me know at [email protected]."
  when SocketError
    message = "Unexpected error communicating when trying to connect to Docverter.  HINT: You may be seeing this message because your DNS is not working.  To check, try running 'host docverter.com' from the command line."
  else
    message = "Unexpected error communicating with Docverter.  If this problem persists, let me know at [email protected]."
  end
  message += "\n\n(Network error: #{e.message})"
  raise APIConnectionError.new(message)
end

.request(method, url, params = {}, headers = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
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
# File 'lib/docverter.rb', line 41

def self.request(method, url, params={}, headers={})
  key = @@api_key

  url = self.api_url(url)

  headers = {
    :user_agent => "Docverter/v1 RubyBindings/#{Docverter::VERSION}",
    :content_type => "multipart/form-data"
  }.merge(headers)

  opts = {
    :method => method,
    :url => url,
    :headers => headers,
    :open_timeout => 30,
    :payload => params,
  }

  begin
    response = execute_request(opts)
  rescue SocketError => e
    self.handle_restclient_error(e)
  rescue NoMethodError => e
    # Work around RestClient bug
    if e.message =~ /\WRequestFailed\W/
      e = APIConnectionError.new('Unexpected HTTP response code')
      self.handle_restclient_error(e)
    else
      raise
    end
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      self.handle_api_error(rcode, rbody)
    else
      self.handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    self.handle_restclient_error(e)
  end

  response.body
end

.resetObject



36
37
38
39
# File 'lib/docverter.rb', line 36

def self.reset
  @@api_key = nil
  @@base_url = 'https://api.docverter.com/v1'
end