Class: Pushbullet_CLI::Utils
- Inherits:
-
Object
- Object
- Pushbullet_CLI::Utils
- Defined in:
- lib/pb/cli/utils.rb
Class Method Summary collapse
-
.get_config ⇒ Object
end get_push_args.
-
.get_push_args(options) ⇒ Object
end print.
-
.get_token(options) ⇒ Object
end get_config.
- .parse_row(cols, row) ⇒ Object
- .print(args) ⇒ Object
-
.print_table(headers, rows) ⇒ Object
end send.
-
.send(url, access_token, method = "post", args = {}) ⇒ Object
end get_token.
Class Method Details
.get_config ⇒ Object
end get_push_args
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/pb/cli/utils.rb', line 63 def get_config conf = {} if File.exists?( File.join( ENV["HOME"], '.pb-cli/config.yml' ) ) config = YAML.load( File.open( File.join( ENV["HOME"], '.pb-cli/config.yml' ), File::RDONLY ).read ) conf.merge!( config ) if config.is_a?(Hash) end return conf end |
.get_push_args(options) ⇒ Object
end print
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 |
# File 'lib/pb/cli/utils.rb', line 34 def get_push_args( ) args = {} unless [:title].nil? unless [:title].empty? args["title"] = [:title] end end unless [:device].nil? unless [:device].empty? args["device_iden"] = [:device] end end unless [:url].nil? unless [:url].empty? args["url"] = [:url] args["type"] = "link" end end if args["type"].nil? args["type"] = "note" end return args end |
.get_token(options) ⇒ Object
end get_config
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/pb/cli/utils.rb', line 77 def get_token( ) token = "" unless [:token].nil? token = [:token] else config = Utils::get_config unless config["access_token"].nil? token = config["access_token"] end end unless token.empty? return token else $stderr.puts "Please initialize an Access Token with `pb init <ACCESS-TOKEN>` or run command with `--token=<ACCESS-TOKEN>`." exit 1 end end |
.parse_row(cols, row) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pb/cli/utils.rb', line 12 def parse_row( cols, row ) table = [] cols.each{ |col| if "created" == col || "modified" == col val = Time.at( row[col] ) else val = row[col] end table.push( val ) } return table end |
.print(args) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/pb/cli/utils.rb', line 25 def print( args ) if "json" == args[:format] puts JSON.generate( args[:rows] ) else table = args[:rows].map.with_index{ | row | parse_row( args[:cols], row ) } Utils:: print_table( args[:cols], table ) end end |
.print_table(headers, rows) ⇒ Object
end send
120 121 122 |
# File 'lib/pb/cli/utils.rb', line 120 def print_table( headers, rows ) puts Terminal::Table.new :headings => headers, :rows => rows end |
.send(url, access_token, method = "post", args = {}) ⇒ Object
end get_token
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/pb/cli/utils.rb', line 96 def send( url, access_token, method = "post", args = {} ) begin if "post" == method return JSON.parse( RestClient.post( url, args.to_json, :content_type => :json, :accept => :json, "Access-Token" => access_token ) ) elsif "get" == method return JSON.parse( RestClient.get( url, :content_type => :json, :accept => :json, "Access-Token" => access_token ) ) end rescue => e $stderr.puts e. exit 1 end end |