Class: Transcriptic::API
- Inherits:
-
Object
- Object
- Transcriptic::API
- Defined in:
- lib/transcriptic/api.rb,
lib/transcriptic/api/errors.rb,
lib/transcriptic/api/version.rb
Defined Under Namespace
Modules: Errors
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
- #escape(string) ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
- #request(params, &block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/transcriptic/api.rb', line 15 def initialize(={}) @api_key = .delete(:api_key) || ENV['TRANSCRIPTIC_API_KEY'] user_pass = ":#{@api_key}" = { :headers => {}, :host => 'secure.transcriptic.com', :nonblock => false, :scheme => 'https' }.merge() [:headers] = { 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip', 'Accept-Language' => 'en-US, en;q=0.8', 'Authorization' => "Token token=\"#{@api_key}\"", 'User-Agent' => "transcriptic/#{Transcriptic::API::VERSION}", 'X-Transcriptic-API-Version' => '1', 'X-Ruby-Version' => RUBY_VERSION, 'X-Ruby-Platform' => RUBY_PLATFORM }.merge([:headers]) @connection = Excon.new("#{[:scheme]}://#{[:host]}", ) end |
Instance Method Details
#escape(string) ⇒ Object
77 78 79 |
# File 'lib/transcriptic/api.rb', line 77 def escape(string) CGI.escape(string).gsub('.', '%2E') end |
#request(params, &block) ⇒ Object
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 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/transcriptic/api.rb', line 37 def request(params, &block) begin response = @connection.request(params, &block) rescue Excon::Errors::SocketError => error raise error rescue Excon::Errors::Error => error klass = case error.response.status when 401 then Transcriptic::API::Errors::Unauthorized when 402 then Transcriptic::API::Errors::VerificationRequired when 403 then Transcriptic::API::Errors::Forbidden when 404 then Transcriptic::API::Errors::NotFound when 408 then Transcriptic::API::Errors::Timeout when 422 then Transcriptic::API::Errors::RequestFailed when 423 then Transcriptic::API::Errors::Locked when /50./ then Transcriptic::API::Errors::RequestFailed else Transcriptic::API::Errors::ErrorWithResponse end reerror = klass.new(error., error.response) reerror.set_backtrace(error.backtrace) raise(reerror) end if response.body && !response.body.empty? if response.headers['Content-Encoding'] == 'gzip' response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read end begin response.body = Transcriptic::API::OkJson.decode(response.body) rescue # leave non-JSON body as is end end # reset (non-persistent) connection @connection.reset response end |