Class: PLOS::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, base_url = "http://api.plos.org") ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/plos/client.rb', line 9

def initialize(api_key, base_url="http://api.plos.org")
  self.api_key = api_key
  self.base_url = base_url
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



6
7
8
# File 'lib/plos/client.rb', line 6

def api_key
  @api_key
end

#base_urlObject

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

Instance Method Details

#all(start = 0, rows = 50) ⇒ Object



14
15
16
# File 'lib/plos/client.rb', line 14

def all(start=0, rows=50)
  search("*:*", start, rows)
end

#execute(url, params = {}) ⇒ Object



49
50
51
# File 'lib/plos/client.rb', line 49

def execute(url, params={})
  Nokogiri::XML(RestClient.post( "#{self.base_url}#{url}", { :api_key => self.api_key }.merge(params) ) )
end

#search(query, start = 0, rows = 50) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/plos/client.rb', line 18

def search(query, start=0, rows=50)
  result = PLOS::ArticleSet.new
  doc = execute( search_url, { :q => query, :rows => rows, :start => start } )
  if doc && doc.root
    doc.root.xpath('./*').each do |child|
      if child.name == "lst"
        child.xpath('./*').each do |int|
          case int.attr("name")
          when "status"
            result.status = int.text
          when "QTime"
            result.time = int.text.to_i
          end
        end
      elsif child.name == "result"
        result.num_found = child.attr("numFound").to_i if child.attr("numFound")
        result.start = child.attr("start").to_i if child.attr("start")
        result.max_score = child.attr("maxScore").to_f if child.attr("maxScore")
        child.xpath('./*').each do |doc|
          result << PLOS::ArticleRef.new(self, doc)
        end
      end
    end
  end
  result
end

#search_urlObject



45
46
47
# File 'lib/plos/client.rb', line 45

def search_url
  "/search"
end