Class: Sonycam::API

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

Instance Method Summary collapse

Constructor Details

#initialize(api_url) ⇒ API

Returns a new instance of API.



6
7
8
9
# File 'lib/sonycam/api.rb', line 6

def initialize api_url
  @uri = URI(api_url)
  @http = Net::HTTP.start(@uri.host, @uri.port)
end

Instance Method Details

#request(method, *params, **options) ⇒ Object

id property of JSON-RPC seems to be totally useless over HTTP since it’s request-response pattern.



12
13
14
15
16
17
18
19
20
21
# File 'lib/sonycam/api.rb', line 12

def request method, *params, **options
  json = {method: method, params: params, id: 1, version: '1.0'}.merge!(options).to_json
  ret = JSON.parse(@http.request_post(@uri.path, json).body)
  if ret['error']
    error_code, error_message = ret['error']
    raise Sonycam::Error.make(error_code), error_message
  else
    ret['result']
  end
end