Sha256: c14b5b8425aa956ab7554e6e5e0b8f5b8aec59c5457a4001fcd2ff9dc4baaf9e
Contents?: true
Size: 1.58 KB
Versions: 2
Compression:
Stored size: 1.58 KB
Contents
require 'faraday' require 'faraday_middleware' require 'json' require 'active_support/core_ext/hash/indifferent_access' require 'dato/account/repo/account' require 'dato/account/repo/site' module Dato module Account class Client REPOS = { account: Repo::Account, sites: Repo::Site, } attr_reader :token, :domain, :schema def initialize(token, domain: 'https://account-api.datocms.com') @domain = domain @token = token end REPOS.each do |method_name, repo_klass| define_method method_name do instance_variable_set( "@#{method_name}", instance_variable_get("@#{method_name}") || repo_klass.new(self) ) end end def request(*args) begin connection.send(*args).body.with_indifferent_access rescue Faraday::ClientError => e puts e.response body = JSON.parse(e.response[:body]) puts JSON.pretty_generate(body) raise e end end private def connection options = { url: domain, headers: { 'Accept' => "application/json", 'Content-Type' => "application/json", 'Authorization' => "Bearer #{@token}" } } @connection ||= Faraday.new(options) do |c| c.request :json c.response :json, content_type: /\bjson$/ c.response :raise_error c.use FaradayMiddleware::FollowRedirects c.adapter :net_http end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dato-0.1.3 | lib/dato/account/client.rb |
dato-0.1.2 | lib/dato/account/client.rb |