Sha256: 31085c16366b98544a3574834ef0eb202f888de82824a64665411b548c14c9ad
Contents?: true
Size: 1.37 KB
Versions: 1
Compression:
Stored size: 1.37 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) response = connection.post uri, {data: data}.to_json parse_response response end private def connection return @_connection if connection_valid? @_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 && 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 return body[:data] when 400 fail %Q(Bad Request: "#{body[:error]}") when 401 fail 'API authorization error' when 429 fail body[:error] else fail '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-1.0.4 | lib/wordsmith/client.rb |