Class: HalOpenscience::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/hal_openscience/resources/base_resource.rb

Direct Known Subclasses

Author, Document

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ BaseResource

Returns a new instance of BaseResource.



24
25
26
# File 'lib/hal_openscience/resources/base_resource.rb', line 24

def initialize(attributes)
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/hal_openscience/resources/base_resource.rb', line 28

def method_missing(method_name, *args, &block)
  if @attributes.has_key? method_name.to_s
    @attributes[method_name.to_s]
  else
    raise ArgumentError.new("Method `#{method_name}` doesn't exist.")
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/hal_openscience/resources/base_resource.rb', line 3

def attributes
  @attributes
end

Class Method Details

.repository_urlObject

Raises:

  • (NotImplementedError)


5
6
7
# File 'lib/hal_openscience/resources/base_resource.rb', line 5

def self.repository_url
  raise NotImplementedError
end

.search(term, fields: [], limit: 30, offset: 0) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hal_openscience/resources/base_resource.rb', line 9

def self.search(term, fields: [], limit: 30, offset: 0)
  uri = URI.parse(self.repository_url)
  params = {
    q: term,
    rows: limit,
    start: offset,
    fl: fields.join(','),
    wt: 'json'
  }
  uri.query = URI.encode_www_form(params)
  body = Net::HTTP.get(uri)

  HalOpenscience::Response.new(body, self)
end