Sha256: d7dfa1881603734dc94010ad5ed3fc335db2fc4e1ec13dcd45e8861379fdc374
Contents?: true
Size: 1.62 KB
Versions: 1
Compression:
Stored size: 1.62 KB
Contents
require 'faraday' require 'json' require 'hashie' module Wordsmith class Client def get(uri) response = connection.get(uri) parse_response(response) end def post(uri, data, proofread:) response = connection.post(uri, {data: data, proofread: proofread}.to_json) parse_response(response) end private def connection return @_connection if connection_valid? @_connection = initialize_connection end def initialize_connection Faraday.new( url: Wordsmith.configuration.url, headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{Wordsmith.configuration.token}", 'User-Agent' => Wordsmith.configuration.user_agent } ) end def connection_valid? return false unless @_connection url = @_connection.url_prefix.to_s authorization = @_connection.headers['Authorization'] url == Wordsmith.configuration.url && valid_token?(authorization) end def valid_token?(authorization) authorization == "Bearer #{Wordsmith.configuration.token}" end def parse_response(response) body = JSON.parse(response.body) Hashie.symbolize_keys!(body) case response.status when 200, 201 then body[:data] when 400 then raise(%Q(Bad Request: "#{body[:errors]}")) when 401 then raise('API authorization error.') when 404 then raise('Incorrect version set in wordsmith.rb') when 429 then raise(body[:error]) else raise('API error') end end end module_function def client @_client ||= Client.new end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
wordsmith-ruby-sdk-2.0.0 | lib/wordsmith/client.rb |