Class: Rdio::Call

Inherits:
Object show all
Defined in:
lib/rdio/call.rb

Instance Method Summary collapse

Instance Method Details

#main(args) ⇒ Object



77
78
79
80
81
82
# File 'lib/rdio/call.rb', line 77

def main(args)
  res = real_main args
  return res if res.is_a? Fixnum
  puts res
  return 0
end

#real_main(argv) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rdio/call.rb', line 5

def real_main(argv)

  # Check the command line for the key,secret,etc...
  args = []
  while true
    arg = argv.shift
    break if not arg
    case arg
    when '--consumer-key'
      key = argv.shift
    when '-h','--help'
      print_help
      return 0
    when '--consumer-secret'
      secret = argv.shift
    when '--authenticate'
      authenticate = true
    when '-i','--indent'
      indent = true
    else
      args << arg
    end
  end

  # Look in the environment if not key or consumer token were given on
  # the command line
  key = ENV['RDIO_KEY'] if not key
  secret = ENV['RDIO_SECRET'] if not secret

  if not key
    STDERR.puts 'consumer key required'
    return 1
  end

  if not secret
    STDERR.puts 'consumer secret required'
    return 1
  end

  if args.length < 1
    STDERR.puts 'method name required'
    return 1
  end

  base = Rdio::BaseApi.new key,secret
  method = args.shift
  rdio_args = {}
  args.each do |str|
    parts = str.split /\=/
    key = parts[0]
    val = parts.length == 1 ? true : parts[1]
    rdio_args[key] = val
  end

  res = base.call method,rdio_args,authenticate
  if indent
    begin
      require 'rubygems'
      require 'json'
      json = JSON.parse res
      return JSON.pretty_generate json
    rescue Exception => e
      STDERR.puts e
      STDERR.puts 'You must install the json gem to use the indent \'feature\''
      return res
    end
  else
    return res
  end

end