Class: BtsyncApi::Api

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api

Returns a new instance of Api.



15
16
17
# File 'lib/btsync/api.rb', line 15

def initialize(options = {})
  self.settings = default_settings.merge(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/btsync/api.rb', line 29

def method_missing(method, *args, &block)
  begin
    if args.count == 1
      return execute(method, args[0])
    else
      return execute(method)
    end
  rescue BtsyncApi::ApiError => e
    if e.error_code != 1 # not found
      raise e
    end
  end
  super
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



13
14
15
# File 'lib/btsync/api.rb', line 13

def settings
  @settings
end

Instance Method Details

#execute(method, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/btsync/api.rb', line 19

def execute(method, options = {})
  params = {method: method.to_s}
  params.merge!(options)
  response = do_request(params)
  if response['error'] && response['error'] != 0
    raise BtsyncApi::ApiError.new(response['error']), response['message']
  end
  response
end