Class: NewsMonitor::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/news_monitor/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



11
12
13
14
15
# File 'lib/news_monitor/api.rb', line 11

def initialize(options={})
  @auth = {}
  @auth[:username] = options.fetch :username, ENV['NEWSMONITOR_USERNAME']
  @auth[:password] = options.fetch :password, ENV['NEWSMONITOR_PASSWORD']
end

Instance Method Details

#find_article(article_id, options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/news_monitor/api.rb', line 26

def find_article(article_id, options={})
  options.merge! basic_auth: @auth
  response = get "/article/#{article_id}", options
  Article.parse(response)
end

#find_cluster(cluster_id, options = {}) ⇒ Object



32
33
34
35
36
# File 'lib/news_monitor/api.rb', line 32

def find_cluster(cluster_id, options={})
  options.merge! basic_auth: @auth
  response = get "/cluster/#{cluster_id}", options
  Cluster.parse response['results'].first
end

#search(query, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/news_monitor/api.rb', line 17

def search(query, options={})
  conditions = ERB::Util.url_encode(query)
  options.merge! basic_auth: @auth
  response = get "/search/#{conditions}", options
  response['results'].map! do |cluster|
    Cluster.parse(cluster)
  end
end