Class: Burghers::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/burghers/client.rb

Constant Summary collapse

URI =
"http://api.opencalais.com/tag/rs/enrich"

Instance Method Summary collapse

Constructor Details

#initialize(license) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/burghers/client.rb', line 7

def initialize(license)
  @license = license
end

Instance Method Details

#enrich(content, content_type = nil, headers = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/burghers/client.rb', line 11

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?
    if html?(content)
      content_type = "text/html"
    else
      content_type = "text/raw"
    end
  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