Sha256: 1f7ae17839c5cebd6abdd6314fd8d0346ea031e628f992f51be90dcd76402525

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Foureyes
  class Resource
    include Enumerable

    CONNECTION = GraphQL::Client::HTTP.new('https://backend-staging.foureyes.io/api')
    SCHEMA = GraphQL::Client.load_schema(CONNECTION)
    CLIENT = GraphQL::Client.new(schema: SCHEMA, execute: CONNECTION)

    attr_reader :id, :account_name, :dealer_code, :created_at, :updated_at

    def initialize(attributes = {})
      attributes.map do |k, v|
        instance_variable_set "@#{k}".to_sym, v
        self.class_eval{ attr_reader "k".to_sym}
      end

      self
    end

    def self.class_name
      self.name.constantize
    end

    def self.execute_request(args = {})
      arguments = { api_key: Foureyes.api_key }
      arguments.merge!(args) if args

      CLIENT.query("#{self.class_name}".constantize::QUERY, variables: arguments)
    end

    def self.list
      response = self.execute_request
      edges = response.data.viewer.accounts.edges
      edges.map do |edge|
        attrs = edge.node.to_h
        instance = new attrs
       end
    end

    def self.retrieve(id)
      response = self.execute_request({id: id})
      attrs = response.data.viewer.accounts.edges.first.node.to_h
      instance = new attrs
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
foureyes-0.1.0 lib/foureyes/resource.rb