Class: Ubersmith::Client

Inherits:
Object
  • Object
show all
Includes:
UrlMaker
Defined in:
lib/ubersmith/client.rb

Constant Summary collapse

BASE_URL =
"https://ubersmith.twistage.com/api/2.0/"

Instance Method Summary collapse

Methods included from UrlMaker

#_escape, #append_url_param, #build_query_string, #build_url

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



26
27
28
29
# File 'lib/ubersmith/client.rb', line 26

def initialize(options)
  @config_file_path = options[:config_file_path]
  @debug = options[:debug]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ubersmith/client.rb', line 31

def method_missing(method, *args)
  components = method.to_s.split('_', 2)
  raise "invalid method: #{method}" if components.length < 2
  raise "invalid category: #{components[0]}" unless %w(uber client device order sales support).include?(components[0])
  options = args[0] if args
  options ||= {}
  cache = options.delete(:cache)
  cache = false if cache.nil?
  ret = call_api(build_url(BASE_URL, { 
                           :method => components[0] + '.' + components[1],
                         }.merge(options)), :cache => cache)
  ret = JSON.parse(ret)
  if !ret.respond_to?(:[]) || ret['status'] != true
    raise ret['error_message'] if ret['error_message'].to_s =~ /invalid method specified/
    puts "Ubersmith error: #{ret['error_message']}"
    return {}
  end
  ret['data']
end