Class: KeenCli::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/keen-cli/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.collection_optionsObject



23
24
25
# File 'lib/keen-cli/cli.rb', line 23

def self.collection_options
  option :collection, :aliases => ['-c']
end

.data_optionsObject



19
20
21
# File 'lib/keen-cli/cli.rb', line 19

def self.data_options
  option :data, :aliases => ['-d']
end

.query_optionsObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/keen-cli/cli.rb', line 27

def self.query_options
  self.collection_options
  option :"analysis-type", :aliases => ['-a']
  option :"group-by", :aliases => ['-g']
  option :"target-property", :aliases => ['-y']
  option :interval, :aliases => ['-i']
  option :timeframe, :aliases => ['-t']
  option :filters, :aliases => ['-f']
  option :percentile
end

.shared_optionsObject



12
13
14
15
16
17
# File 'lib/keen-cli/cli.rb', line 12

def self.shared_options
  option :project, :aliases => ['-p']
  option :"master-key", :aliases => ['-k']
  option :"read-key", :aliases => ['-r']
  option :"write-key", :aliases => ['-w']
end

Instance Method Details

#events_addObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/keen-cli/cli.rb', line 127

def events_add

  events = []

  if $stdin.tty?
    events.push(options[:data] || {})
  else
    ARGV.clear
    ARGF.each_line do |line|
      events.push(line)
    end
  end

  events = events.map do |event|
    begin
      JSON.parse(event)
    rescue
      begin
        Utils.parse_data_as_querystring(event)
      rescue
        event
      end
    end
  end

  collection = Utils.get_collection_name(options)

  if events.length > 1

    Keen.publish_batch(collection => events).tap do |result|
      puts JSON.pretty_generate(result)
    end

  else

    Keen.publish(collection, events.first).tap do |result|
      puts JSON.pretty_generate(result)
    end

  end

end

#project_collectionsObject



62
63
64
65
66
67
# File 'lib/keen-cli/cli.rb', line 62

def project_collections
  Utils.process_options!(options)
  Keen.event_collections.tap do |collections|
    puts JSON.pretty_generate(collections)
  end
end

#project_openObject



73
74
75
76
77
78
# File 'lib/keen-cli/cli.rb', line 73

def project_open
  Utils.process_options!(options)
  "https://keen.io/project/#{Keen.project_id}".tap do |project_url|
    `open #{project_url}`
  end
end

#project_showObject



51
52
53
54
55
56
# File 'lib/keen-cli/cli.rb', line 51

def project_show
  Utils.process_options!(options)
  response = HTTP.get(
    "https://api.keen.io/3.0/projects/#{Keen.project_id}?api_key=#{Keen.master_key}")
  JSON.pretty_generate(JSON.parse(response.to_s)).tap do |s| puts s end
end

#project_workbenchObject



84
85
86
87
88
89
# File 'lib/keen-cli/cli.rb', line 84

def project_workbench
  Utils.process_options!(options)
  "https://keen.io/project/#{Keen.project_id}/workbench".tap do |project_url|
    `open #{project_url}`
  end
end

#queries_runObject



95
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/keen-cli/cli.rb', line 95

def queries_run

  Utils.process_options!(options)

  # convert dashes
  q_options = options.inject({}) do |memo, element|
    if ['analysis-type', 'group-by', 'target-property'].include?(element.first)
      memo[element.first.sub('-', '_')] = element.last
    else
      memo[element.first] = element.last
    end
    memo
  end

  collection = Utils.get_collection_name(options)

  Keen.send(q_options["analysis_type"], collection, q_options).tap do |result|
    if result.is_a?(Hash) || result.is_a?(Array)
      puts JSON.pretty_generate(result)
    else
      puts result
    end
  end
end

#versionObject



41
42
43
44
45
# File 'lib/keen-cli/cli.rb', line 41

def version
  "keen-cli version #{KeenCli::VERSION}".tap do |s|
    puts s
  end
end