Class: Limesurvey::API

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

Instance Method Summary collapse

Constructor Details

#initialize(service_url) ⇒ API

Returns a new instance of API.



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

def initialize(service_url)
  @uri = URI.parse(service_url)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Raises:



10
11
12
13
14
15
# File 'lib/limesurvey.rb', line 10

def method_missing(name, *args)
  post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
  resp = JSON.parse( http_post_request(post_body) )
  raise JSONRPCError, resp['error'] if resp['error']
  resp['result']
end

Instance Method Details

#http_post_request(post_body) ⇒ Object



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

def http_post_request(post_body)
  http  = Net::HTTP.new(@uri.host, @uri.port)
  request = Net::HTTP::Post.new(@uri.request_uri)
  request.content_type = 'application/json'
  request.body = post_body
  http.request(request).body
end