Module: RTFM

Defined in:
lib/crowdflower-rtfm.rb

Defined Under Namespace

Classes: APIConnectionError, APIError, AccountError, AuthenticationError, Error, InvalidRequestError, PaymentError, RateLimitError

Constant Summary collapse

VERSION =
File.read(File.join(File.dirname(__FILE__),"..","VERSION"))
@@api_key =
nil
@@api_base =
"https://rtfm.crowdflower.com/v1"

Class Method Summary collapse

Class Method Details

.api_baseObject



14
# File 'lib/crowdflower-rtfm.rb', line 14

def self.api_base; @@api_base; end

.api_base=(api_base) ⇒ Object



13
# File 'lib/crowdflower-rtfm.rb', line 13

def self.api_base=(api_base); @@api_base = api_base; end

.api_keyObject



12
# File 'lib/crowdflower-rtfm.rb', line 12

def self.api_key; @@api_key; end

.api_key=(api_key) ⇒ Object



11
# File 'lib/crowdflower-rtfm.rb', line 11

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

.api_url(url = '') ⇒ Object



15
# File 'lib/crowdflower-rtfm.rb', line 15

def self.api_url(url=''); @@api_base + url; end

.error(klass = Error, error, rcode, rbody, error_obj) ⇒ Object



94
# File 'lib/crowdflower-rtfm.rb', line 94

def self.error(klass = Error, error, rcode, rbody, error_obj); klass.new(error, rcode, rbody, error_obj); end

.handle_api_error(rcode, rbody) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/crowdflower-rtfm.rb', line 96

def self.handle_api_error(rcode, rbody)
  begin
    error_obj = MultiJson.load(rbody, :symbolize_keys => true)
    error = (error_obj && error_obj[:error]) or raise Error.new # escape from parsing
  rescue MultiJson::DecodeError, Error
    raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
  end

  case rcode
  when 503
    raise error(RateLimitError, error, rcode, rbody, error_obj)
  when 400, 404 then
    raise error(InvalidRequestError, error, rcode, rbody, error_obj)
  when 401
    raise error(AuthenticationError, error, rcode, rbody, error_obj)
  when 402
    raise error(PaymentError, error, rcode, rbody, error_obj)
  when 403
    raise error(AccountError, error, rcode, rbody, error_obj)
  else
    raise error(APIError, error, rcode, rbody, error_obj)
  end
end

.handle_restclient_error(e) ⇒ Object

Raises:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/crowdflower-rtfm.rb', line 120

def self.handle_restclient_error(e)
  case e
  when RestClient::ServerBrokeConnection, RestClient::RequestFailed
    message = "Could not connect to RTFM (#{@@api_base}).  Please check your internet connection and try again.  If this problem persists, you should check RTFM's service status at https://twitter.com/cfstatus, or let us know at [email protected]."
  when RestClient::SSLCertificateNotVerified
    message = "Could not verify RTFM's SSL certificate.  Please make sure that your network is not intercepting certificates. If this problem persists, let us know at [email protected]."
  when SocketError
    message = "Unexpected error communicating when trying to connect to RTFM.  HINT: You may be seeing this message because your DNS is not working.  To check, try running 'host stripe.com' from the command line."
  else
    message = "Unexpected error communicating with RTFM.  If this problem persists, let us know at [email protected]"
  end
  message += "\n\n(Network error: #{e.message})"
  raise APIConnectionError.new(message)
end

.moderate_image(url, metadata = nil) ⇒ Object



17
18
19
20
21
# File 'lib/crowdflower-rtfm.rb', line 17

def self.moderate_image(url,  = nil)
  params = {:url => url}
  params.merge!(:metadata => ) if 
  request("/images", :post, params)
end

.request(url, method = :get, params = nil, api_key = nil, headers = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'lib/crowdflower-rtfm.rb', line 27

def self.request(url, method = :get, params = nil, api_key = nil, headers = {})
  api_key ||= @@api_key
  raise AuthenticationError.new("Please provide an API key (RTFM.api_key = <API-KEY>, your API key can be found by clicking \"APISettings\" @ http://crowdflower.com/rtfm)") unless api_key
  
  #RestClient appends get parameters by looking for params in headers... lame
  if method == :get && params
    headers.merge!(:params => params)
    params = nil      
  end
    
  opts = {
    :user => api_key,
    :timeout => 30,
    :url => api_url(url),
    :method => method,
    :headers => {
      :user_agent => "RTFM/v1 RubyGem/#{VERSION}",
      :accept => "application/json"
    }.merge(headers),
    :payload => params
  }
  
  response = RestClient::Request.execute(opts)
  MultiJson.load(response.body, :symbolize_keys => true)
rescue SocketError => e
  self.handle_restclient_error(e)
rescue RestClient::ExceptionWithResponse => e
  rcode = e.http_code
  rbody = e.http_body
  if rcode && rbody 
    self.handle_api_error(rcode, rbody)
  else
    self.handle_restclient_error(e)
  end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
  self.handle_restclient_error(e)
rescue MultiJson::DecodeError
  raise APIError.new("Invalid response body: #{response.body.inspect}. (HTTP response code of #{response.code})", response.code, response.body)
end

.retrieve_image(id) ⇒ Object



23
24
25
# File 'lib/crowdflower-rtfm.rb', line 23

def self.retrieve_image(id)
  request("/images/#{id}")
end