Module: Nwsdk::Helpers
Constant Summary collapse
- MULTIPART_BOUNDARY =
%r{\Amultipart/mixed; boundary="(?<msg_boundary>.+)"\z}- MULTIPART_PROLOGUE =
%r{\AThis is a message with multiple parts in MIME format.}- MULTIPART_END =
%r{\A--\z}- ATTACHMENT_FILENAME =
%r{\Aattachment; filename="(?<filename>.+)"\z}
Instance Method Summary collapse
- #count_results(result) ⇒ Object
- #decode_value(field) ⇒ Object
- #each_multipart_response_entity(data, boundary = nil) ⇒ Object
- #format_timestamp(time) ⇒ Object
- #get_boundary(header_val) ⇒ Object
- #get_sessionids(restq) ⇒ Object
- #response_successful?(response) ⇒ Boolean
Instance Method Details
#count_results(result) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/nwsdk/helpers.rb', line 43 def count_results(result) fields=result['results'].fetch('fields',[]) fields.reduce({}) do |memo,field| val=decode_value(field) memo[val]=field['count'] memo end end |
#decode_value(field) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/nwsdk/helpers.rb', line 8 def decode_value(field) case field['format'] when 1,2,3,4,5,6,7,8,9,34 field['value'].to_i when 10,11 field['value'].to_f when 32 t=Time.at(field['value'].to_i) t + t.gmtoff when 33 Nwsdk::Constants::NW_VARIANT_DAYS[field['value'].to_i] when 128,129 IPAddr.new(field['value']) else field['value'] end end |
#each_multipart_response_entity(data, boundary = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/nwsdk/helpers.rb', line 52 def each_multipart_response_entity(data, boundary=nil) data.force_encoding('BINARY').split('--'+ boundary).each do |entity| cleaned=entity.strip next if (cleaned.match(MULTIPART_PROLOGUE) || cleaned.match(MULTIPART_END)) header_lines,sep,entity_data=cleaned.partition(%r{\r\n\r\n}) headers=Hash[header_lines.split(%r{\r\n}).map {|l| l.split(%r{: })}] filename=headers['Content-Disposition'].match(ATTACHMENT_FILENAME)[:filename] yield file_hash={ filename: filename, type: headers['Content-Type'], size: headers['Content-Length'].to_i, data: entity_data } end end |
#format_timestamp(time) ⇒ Object
25 26 27 |
# File 'lib/nwsdk/helpers.rb', line 25 def (time) time.getutc.strftime(Nwsdk::Constants::NW_TIME_FORMAT) end |
#get_boundary(header_val) ⇒ Object
39 40 41 |
# File 'lib/nwsdk/helpers.rb', line 39 def get_boundary(header_val) header_val.match(MULTIPART_BOUNDARY)[:msg_boundary] end |
#get_sessionids(restq) ⇒ Object
33 34 35 36 37 |
# File 'lib/nwsdk/helpers.rb', line 33 def get_sessionids(restq) session_query=Nwsdk::Query.new(keys: ['sessionid'], condition: restq.condition, endpoint: restq.endpoint) result=session_query.request result.map {|r| r['sessionid']} end |
#response_successful?(response) ⇒ Boolean
29 30 31 |
# File 'lib/nwsdk/helpers.rb', line 29 def response_successful?(response) response.code==200 && response.net_http_res.content_type == 'application/json' end |