Sha256: 300fcff83a1d5a578c826a2035c672d84edd817f791b1a640812b4e3505814f2

Contents?: true

Size: 1.49 KB

Versions: 8

Compression:

Stored size: 1.49 KB

Contents

class Apisync
  class Resource
    def initialize(name, options = {})
      @name = name
      @options = options
    end

    # Saves a resource.
    #
    # When the resource has an id in `data`, a `PUT` request is done. Otherwise
    # a `POST` takes place.
    #
    def save(data)
      data[:type] = @name.to_s.gsub("_", "-")
      headers = data.delete(:headers) || {}

      if data[:id].nil?
        post(data, headers: headers)
      else
        put(data, headers: headers)
      end
    end

    # Returns all resources that match the conditions passed in.
    #
    # 1. To find a resource by its id:
    #
    #   get(id: 'customer-id')
    #
    # 2. To find a resource by a column value
    #
    #   get(filters: {column_name: 'customer-id' }})
    #
    def get(conditions)
      client = Apisync::HttpClient.new(
        resource_name: @name,
        options: @options
      )
      client.get(
        id: conditions[:id],
        filters: conditions[:filters],
        headers: conditions[:headers] || {}
      )
    end

    private


    def post(data, headers: {})
      client = Apisync::HttpClient.new(
        resource_name: @name,
        options: @options
      )
      client.post(
        data: data,
        headers: headers
      )
    end

    def put(data, headers: {})
      client = Apisync::HttpClient.new(
        resource_name: @name,
        options: @options
      )
      client.put(
        id: data[:id],
        data: data,
        headers: headers
      )
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
apisync-0.3.0 lib/apisync/resource.rb
apisync-0.2.3 lib/apisync/resource.rb
apisync-0.2.2 lib/apisync/resource.rb
apisync-0.2.1 lib/apisync/resource.rb
apisync-0.2.0 lib/apisync/resource.rb
apisync-0.1.6 lib/apisync/resource.rb
apisync-0.1.5 lib/apisync/resource.rb
apisync-0.1.4 lib/apisync/resource.rb