Class: Pushbullet_CLI::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/pb/cli/utils.rb

Class Method Summary collapse

Class Method Details

.get_configObject

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( options )
  args = {}

  unless options[:title].nil?
    unless options[:title].empty?
      args["title"] = options[:title]
    end
  end

  unless options[:device].nil?
    unless options[:device].empty?
      args["device_iden"] = options[:device]
    end
  end

  unless options[:url].nil?
    unless options[:url].empty?
      args["url"] = options[: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( options )
  token = ""
  unless options[:token].nil?
    token = options[: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


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

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.message
    exit 1
  end
end