Sha256: 0b57f62c18bcc9c5aee56dd1a1a2f724a2cd9e852776ef3c0c171a88794960fc

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'json'

module Binnacle
  class Resource
    attr_reader :route
    attr_writer :connection

    def post_asynch
      Thread.new do
        response = response_from_post(@connection, self.route, self.to_json)

        if response.status == 401
          Binnacle.binnacle_logger.error("Error communicating with Binnacle: #{response.body}")
        end
      end
    end

    def post
      response = response_from_post(@connection, self.route, self.to_json)

      if response.status == 401
        Binnacle.binnacle_logger.error("Error communicating with Binnacle: #{response.body}")
      else
        JSON.parse(response.body)
      end
    end

    def self.get(connection, path, query_params)
      response = connection.get do |req|
        req.url path
        req.headers['Content-Type'] = 'application/json'
        query_params.each do |n,v|
          req.params[n] = v if v
        end
      end

      if response.status == 401
        Binnacle.binnacle_logger.error("Error communicating with Binnacle: #{response.body}")
      else
        JSON.parse(response.body).map { |r| self.from_hash(r) }
      end
    end

    protected

    def response_from_post(connection, route, body)
      connection.post do |req|
        req.url route
        req.headers['Content-Type'] = 'application/json'
        req.body = body
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
binnacle-0.5.0 lib/binnacle/resource.rb
binnacle-0.4.9 lib/binnacle/resource.rb
binnacle-0.4.8 lib/binnacle/resource.rb