Sha256: 419121e2f176c7f6db01b986c67a2dfc97f9f4c8bb791712c4713af1ef89bd3c
Contents?: true
Size: 1.67 KB
Versions: 2
Compression:
Stored size: 1.67 KB
Contents
require 'faraday' require 'faraday_middleware' require 'json' require 'active_support/core_ext/hash/indifferent_access' require 'dato/repo/field' require 'dato/repo/item_type' require 'dato/repo/menu_item' require 'dato/repo/site' require 'dato/repo/upload_request' require 'dato/repo/user' require 'dato/repo/item' module Dato class Client REPOS = { fields: Repo::Field, item_types: Repo::ItemType, menu_items: Repo::MenuItem, site: Repo::Site, upload_requests: Repo::UploadRequest, users: Repo::User, items: Repo::Item, } attr_reader :token, :domain, :schema def initialize(token, domain: 'http://site-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 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.adapter :net_http end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dato-0.1.1 | lib/dato/client.rb |
dato-0.1.0 | lib/dato/client.rb |