Sha256: 7419afd5ce179abe7add7b415011231ae53dc417b68d06f4791323a88513ea9c

Contents?: true

Size: 997 Bytes

Versions: 1

Compression:

Stored size: 997 Bytes

Contents

require 'httparty'

module Burghers
  class Client
    URI = "http://api.opencalais.com/tag/rs/enrich"

    def initialize(license)
      @license = license
    end

    def enrich(content, content_type=nil, headers = nil)
      if content.start_with?('http://') or content.start_with?('https://')
        content = HTTParty.get(content).body
        content_type = "text/html"
      end

      if content_type.nil? and content.include?("<") and content.include(">")
        content_type = "text/html"
      elsif content_type.nil?
        content_type = "text/raw"
      end

      headers = {
        'accept' => 'application/json',
        'x-calais-licenseID' => @license,
        'content-type' => content_type
      }.merge(headers || {})

      response = HTTParty.post(URI, :body => content, :headers => headers)
      if response.code != 200
        raise "Got response code of #{response.code}: #{response}"
      end
      
      Response.new response.parsed_response
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
burghers-0.0.1 lib/burghers/client.rb