module Scribesend
Constants
- VERSION
Public Class Methods
api_base()
click to toggle source
# File lib/scribesend.rb, line 38 def self.api_base @api_base end
api_key()
click to toggle source
# File lib/scribesend.rb, line 34 def self.api_key @api_key end
api_key=(api_key)
click to toggle source
# File lib/scribesend.rb, line 30 def self.api_key=(api_key) @api_key = api_key end
api_url(url='', api_base_url=nil)
click to toggle source
# File lib/scribesend.rb, line 26 def self.api_url(url='', api_base_url=nil) (api_base_url || @api_base) + url end
request(method, url, api_key, params={}, headers={}, api_base_url=nil)
click to toggle source
# File lib/scribesend.rb, line 42 def self.request(method, url, api_key, params={}, headers={}, api_base_url=nil) api_base_url = api_base_url || @api_base unless api_key ||= @api_key raise Error.new("No API key provided. " \ "Set your API key using 'Scribesend.api_key = <API-KEY>'.") end params = Util.objects_to_ids(params) url = api_url(url, api_base_url) case method.to_s.downcase.to_sym when :get, :head, :delete # Make params into GET parameters url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any? payload = nil else if headers[:content_type] && (headers[:content_type] == "multipart/form-data" || headers[:content_type] == "application/json") payload = params else payload = uri_encode(params) end end if headers[:content_type] && headers[:content_type] == "application/json" content_type = headers[:content_type] else content_type = "application/x-www-form-urlencoded" end headers = { :user_agent => "Scribesend/v0 RubyClient/#{Scribesend::VERSION}", :authorization => "Bearer #{api_key}", :content_type => content_type }.update(headers) request_opts = { :method => method, :headers => headers, :url => url, :payload => payload, :verify_ssl => false, :open_timeout => 30, :timeout => 80, } begin response = execute_request(request_opts) rescue SocketError => e handle_restclient_error(e, api_base_url) rescue NoMethodError => e # Work around RestClient bug if e.message =~ /\WRequestFailed\W/ e = Error.new('Unexpected HTTP response code') handle_restclient_error(e, api_base_url) else raise end rescue RestClient::ExceptionWithResponse => e if rcode = e.http_code and rbody = e.http_body handle_api_error(rcode, rbody) else handle_restclient_error(e, api_base_url) end rescue RestClient::Exception, Errno::ECONNREFUSED => e handle_restclient_error(e, api_base_url) end [parse(response), api_key] end
Protected Class Methods
execute_request(opts)
click to toggle source
# File lib/scribesend.rb, line 120 def self.execute_request(opts) RestClient::Request.execute(opts) end
handle_api_error(rcode, rbody)
click to toggle source
# File lib/scribesend.rb, line 134 def self.handle_api_error(rcode, rbody) begin error_obj = JSON.parse(rbody) error_obj = Util.symbolize_names(error_obj) error = error_obj[:error][:message] or raise Error.new # escape from parsing rescue JSON::ParserError, Error raise Error.new("Invalid response object from API: #{rbody.inspect} " + "(HTTP response code was #{rcode})", rcode, rbody) end raise Error.new(error, rcode, rbody, error_obj) end
handle_restclient_error(e, api_base_url=nil)
click to toggle source
# File lib/scribesend.rb, line 147 def self.handle_restclient_error(e, api_base_url=nil) api_base_url = @api_base unless api_base_url connection_message = "Please check your internet connection and try again. " \ "If this problem persists, let us know at team@scribesend.com." case e when RestClient::RequestTimeout message = "Could not connect to Scribesend (#{api_base_url}). #{connection_message}" when RestClient::ServerBrokeConnection message = "The connection to the server (#{api_base_url}) broke before the " \ "request completed. #{connection_message}" when SocketError message = "Unexpected error communicating when trying to connect to Scribesend. " \ "You may be seeing this message because your DNS is not working. " \ "To check, try running 'host scribesend.com' from the command line." else message = "Unexpected error communicating with Scribesend. " \ "If this problem persists, let us know at team@scribesend.com." end raise Error.new(message + "\n\n(Network error: #{e.message})") end
parse(response)
click to toggle source
# File lib/scribesend.rb, line 124 def self.parse(response) begin response = JSON.parse(response.body) rescue JSON::ParserError raise Error.new('Unexpected API response', response.code, response.body) end Util.symbolize_names(response) end
uri_encode(params)
click to toggle source
# File lib/scribesend.rb, line 115 def self.uri_encode(params) Util.flatten_params(params). map { |k,v| "#{k}=#{Util.url_encode(v)}" }.join('&') end