Top Level Namespace
Defined Under Namespace
Modules: Ugc
Constant Summary collapse
- SKIP_ATTRS =
%w(metadata uri type)- INDEX_COL_WIDTH =
2
Instance Method Summary collapse
- #col_overhead ⇒ Object
-
#collection_metadata(collection, headers = nil) ⇒ Object
return hash { column_name: { max_size: 12, size: 12 } }.
- #format_collection(collection, headers = nil) ⇒ Object
- #format_entity(entity) ⇒ Object
- #format_response(response) ⇒ Object
- #multipart_payload(payload, file) ⇒ Object
- #multipart_upload(resource, payload, file, method = :post, additional_headers = {}) ⇒ Object
-
#parse_data(input) ⇒ Object
returns a json string.
- #parse_sql(query) ⇒ Object
- #perform_substitutions(string) ⇒ Object
- #puts_curl(command, resource, payload = nil, file = nil) ⇒ Object
- #replacement_for(replacement_parm) ⇒ Object
- #save_response(response) ⇒ Object
- #show_profile(name) ⇒ Object
Instance Method Details
#col_overhead ⇒ Object
12 13 14 |
# File 'lib/ugc/helpers/format.rb', line 12 def col_overhead $settings.table_border? ? 3 : 1 end |
#collection_metadata(collection, headers = nil) ⇒ Object
return hash { column_name: { max_size: 12, size: 12 } }
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ugc/helpers/format.rb', line 72 def (collection, headers=nil) result = {} collection.each do |entity| if entity.is_a? Array headers.each_with_index do |header, index| col = result[header] ||= {} size = entity[index].to_s.size col[:max_size] = col[:max_size] ? [col[:max_size], size].max : size end else entity.reject{|k,v| headers ? !headers.include?(k) : SKIP_ATTRS.include?(k)}.each do |k,v| col = result[k] ||= {} size = v.to_s.size col[:max_size] = col[:max_size] ? [col[:max_size], size].max : size end end end total_size = result.inject(0) do |total, (col,)| [:max_size] = [col.size, [:max_size]].max total + [:max_size] end terminal_columns = HighLine.new.output_cols overhead = (result.keys.size + 2) * col_overhead + INDEX_COL_WIDTH if total_size + overhead < terminal_columns result.each {|col,| [:size] = [:max_size]} else col_size = (terminal_columns - overhead) / result.keys.size result.each {|col,| [:size] = col_size} end result end |
#format_collection(collection, headers = nil) ⇒ Object
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 |
# File 'lib/ugc/helpers/format.rb', line 16 def format_collection(collection, headers=nil) if collection && collection.size > 0 save_headers = headers ? headers.clone : nil save_response collection.response = collection, headers table border: $settings.table_border? do row header: true do headers ||= .keys column '#', width: INDEX_COL_WIDTH headers.each {|header| column header, width: [header][:size] } end collection.each_with_index do |entity, index| row do column index+1 if entity.is_a? Array entity.each {|v| column v } else headers.each {|header| column entity[header]} end end end end if collection.cursor && agree('Next Page? (Y/N)') {|q| q.default = 'Y'} format_collection(collection.next_page, save_headers) end else puts "0 results" end end |
#format_entity(entity) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ugc/helpers/format.rb', line 46 def format_entity(entity) if entity name_cols = value_cols = 0 entity.data.reject{|k,v| SKIP_ATTRS.include? k}.each do |k,v| name_cols = [name_cols, k.size].max value_cols = [value_cols, v.to_s.size].max end table border: $settings.table_border? do row header: true do name_width = [name_cols, 20].min column 'name', width: name_width column 'value', width: [value_cols, HighLine.new.output_cols - name_width - (3 * col_overhead)].min end entity.data.reject{|k,v| SKIP_ATTRS.include? k}.each do |k,v| row do column(k) column(v) end end end else puts "no data" end end |
#format_response(response) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/ugc/helpers/format.rb', line 4 def format_response(response) if response.multiple_entities? && response.collection.size > 1 format_collection response.collection else format_entity response.entity end end |
#multipart_payload(payload, file) ⇒ Object
1 2 3 4 5 6 7 8 |
# File 'lib/ugc/helpers/rest.rb', line 1 def multipart_payload(payload, file) payload = payload.is_a?(Hash) ? payload : MultiJson.load(payload) filename = 'file' filename, file = file.split '=' if file.is_a?(String) file = file[1..-1] if file.start_with? '@' # be kind to curl users payload[filename] = file.is_a?(File) ? file : File.new(file, 'rb') payload end |
#multipart_upload(resource, payload, file, method = :post, additional_headers = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ugc/helpers/rest.rb', line 10 def multipart_upload(resource, payload, file, method=:post, additional_headers = {}) payload = multipart_payload payload, file payload[:multipart] = true headers = (resource.[:headers] || {}).merge(additional_headers) response = RestClient::Request.execute(resource..merge( :method => method, :url => resource.url, :payload => payload, :headers => headers)) resource.instance_variable_set :@response, response response.resource = resource response end |
#parse_data(input) ⇒ Object
returns a json string
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ugc/helpers/parse.rb', line 31 def parse_data(input) return unless input # must be wrapped in {} input = "{#{input}}" unless input.start_with? '{' or input.start_with? '[' # must be a json string or 1.9 hash format begin MultiJson.dump(eval(input)) rescue SyntaxError input end end |
#parse_sql(query) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ugc/helpers/parse.rb', line 3 def parse_sql(query) result = {} keywords = %w(select from where limit) current = nil query.downcase.split(/[\s,*]/).each do |ea| next if ea == '' || (current == 'select' && ea == 'distinct') if keywords.include? ea current = ea elsif current if current == 'select' and ea.start_with? "{" current = nil next end if result[current] if result[current].is_a? Array result[current] << ea else result[current] = [result[current]] << ea end else result[current] = (current == 'select') ? [ea] : ea end end end result end |
#perform_substitutions(string) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/ugc/helpers/history.rb', line 19 def perform_substitutions(string) string = string.clone string.scan(/@[0-9]+/).uniq.each do |e| string.gsub! e, replacement_for(e) end string end |
#puts_curl(command, resource, payload = nil, file = nil) ⇒ Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ugc/helpers/curl.rb', line 1 def puts_curl(command, resource, payload=nil, file=nil) headers = resource.[:headers] || {} req = RestClient::Request.new(resource..merge( :method => :get, :url => resource.url, :headers => headers)) headers.delete(:content_type) if file curl_headers = req.make_headers headers curl_headers = curl_headers.map {|e| %Q[-H "#{e.join(': ')}"] }.join(' ') curl = "curl -X #{command.upcase} -i #{curl_headers}" if file payload = multipart_payload payload || {}, file payload = payload.map { |k,v| File.file?(v) ? %Q[-F "#{k}=@#{File.absolute_path(v)}"] : %Q[-F "#{k}=#{v}"] }.join ' ' curl = "#{curl} #{payload}" else curl = %Q[#{curl} -d '#{payload}'] if (payload) end puts "#{curl} '#{URI::encode resource.url}'" end |
#replacement_for(replacement_parm) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/ugc/helpers/history.rb', line 9 def replacement_for(replacement_parm) unless @last_response blob = $settings.load_profile_blob 'last_response' @last_response = Marshal.load blob end index = replacement_parm[1..-1].to_i raise "no data for replacement param: #{replacement_parm}" unless @last_response[index-1] @last_response[index-1] end |
#save_response(response) ⇒ Object
1 2 3 4 5 6 7 |
# File 'lib/ugc/helpers/history.rb', line 1 def save_response(response) if response && response.multiple_entities? && response.collection.size > 1 paths = response.entities.collect {|e| e['metadata']['path'][1..-1] } rescue [] blob = Marshal.dump paths $settings.save_profile_blob 'last_response', blob end end |
#show_profile(name) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/ugc/commands/profile.rb', line 25 def show_profile(name) profile = $settings.profile(name) print $settings.active_profile_name == name ? ' *' : ' ' puts name $settings.profile(name).each_pair do |k,v| puts " #{k}: #{v}" end end |