moesif_rack 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 818bfecddcafd4b2b71a662e762cb5468b4bf54c7fc5397fb0812ad4274b82ac
4
- data.tar.gz: 96385b853df74e5a7ca95d669244fc8e744079fcfd6d109c1c7855663bd61ea8
3
+ metadata.gz: 0cc9c9466e539ecf7d672312b98ed2f228a6b850f7c7e315559f80e31ee937a7
4
+ data.tar.gz: ba2f944d8f3f19cb451c64113412780cd8141359f6473992b603fe9eddf43bac
5
5
  SHA512:
6
- metadata.gz: eb421c6472805827b2f7c23442c3047ba36c34172ef9af86d2c1266e3867c5aea201cb1900b9e11c16f6915773273a366d75e2b81a0bd02676f147f432de2988
7
- data.tar.gz: 557bea2a4a9e8c223bde8d2242d47c23ff69adf591ee2f55101b74f9910e788ec584aaf7b5e61942f485a2aa43bd423b6a4196094586424c217de0ded3fe36b9
6
+ metadata.gz: ecac64d6a6f05a347ccb9927630e3a48eda1ad75ec65d32dc23742483f311fa55b8a5d6720bfda6e7bff49146852410c80b4d6a4cae8b17917f16a4458630523
7
+ data.tar.gz: 630eb05009f358ee214a61e386f0e933222ebeced3ea38e158d70e3f1629351ef52ff885736f2dfb4e49dd41f8c6271ca8ca2731b17cfc363d46c52ab423b375
data/README.md CHANGED
@@ -22,7 +22,7 @@ gem install moesif_rack
22
22
  and if you have a `Gemfile` in your project, please add this line to
23
23
 
24
24
  ```
25
- gem 'moesif_rack', '~> 1.4.0'
25
+ gem 'moesif_rack', '~> 1.4.1'
26
26
 
27
27
  ```
28
28
 
@@ -334,7 +334,7 @@ This method is a convenient helper that calls the Moesif API lib.
334
334
  For details, visit the [Ruby API Reference](https://www.moesif.com/docs/api?ruby#update-a-user).
335
335
 
336
336
  ```ruby
337
- metadata => {
337
+ metadata = {
338
338
  :email => 'john@acmeinc.com',
339
339
  :first_name => 'John',
340
340
  :last_name => 'Doe',
@@ -348,7 +348,7 @@ metadata => {
348
348
 
349
349
  # Campaign object is optional, but useful if you want to track ROI of acquisition channels
350
350
  # See https://www.moesif.com/docs/api#users for campaign schema
351
- campaign = CampaignModel.new()
351
+ campaign = MoesifApi::CampaignModel.new()
352
352
  campaign.utm_source = "google"
353
353
  campaign.utm_medium = "cpc"
354
354
  campaign.utm_campaign = "adwords"
@@ -357,7 +357,7 @@ campaign.utm_content = "landing"
357
357
 
358
358
  # Only user_id is required.
359
359
  # metadata can be any custom object
360
- user = UserModel.new()
360
+ user = MoesifApi::UserModel.new()
361
361
  user.user_id = "12345"
362
362
  user.company_id = "67890" # If set, associate user with a company object
363
363
  user.campaign = campaign
@@ -375,7 +375,7 @@ For details, visit the [Ruby API Reference](https://www.moesif.com/docs/api?ruby
375
375
  ```ruby
376
376
  users = []
377
377
 
378
- metadata => {
378
+ metadata = {
379
379
  :email => 'john@acmeinc.com',
380
380
  :first_name => 'John',
381
381
  :last_name => 'Doe',
@@ -389,7 +389,7 @@ metadata => {
389
389
 
390
390
  # Campaign object is optional, but useful if you want to track ROI of acquisition channels
391
391
  # See https://www.moesif.com/docs/api#users for campaign schema
392
- campaign = CampaignModel.new()
392
+ campaign = MoesifApi::CampaignModel.new()
393
393
  campaign.utm_source = "google"
394
394
  campaign.utm_medium = "cpc"
395
395
  campaign.utm_campaign = "adwords"
@@ -398,7 +398,7 @@ campaign.utm_content = "landing"
398
398
 
399
399
  # Only user_id is required.
400
400
  # metadata can be any custom object
401
- user = UserModel.new()
401
+ user = MoesifApi::UserModel.new()
402
402
  user.user_id = "12345"
403
403
  user.company_id = "67890" # If set, associate user with a company object
404
404
  user.campaign = campaign
@@ -419,7 +419,7 @@ This method is a convenient helper that calls the Moesif API lib.
419
419
  For details, visit the [Ruby API Reference](https://www.moesif.com/docs/api?ruby#update-a-company).
420
420
 
421
421
  ```ruby
422
- metadata => {
422
+ metadata = {
423
423
  :org_name => 'Acme, Inc',
424
424
  :plan_name => 'Free',
425
425
  :deal_stage => 'Lead',
@@ -432,7 +432,7 @@ metadata => {
432
432
 
433
433
  # Campaign object is optional, but useful if you want to track ROI of acquisition channels
434
434
  # See https://www.moesif.com/docs/api#update-a-company for campaign schema
435
- campaign = CampaignModel.new()
435
+ campaign = MoesifApi::CampaignModel.new()
436
436
  campaign.utm_source = "google"
437
437
  campaign.utm_medium = "cpc"
438
438
  campaign.utm_campaign = "adwords"
@@ -441,7 +441,7 @@ campaign.utm_content = "landing"
441
441
 
442
442
  # Only company_id is required.
443
443
  # metadata can be any custom object
444
- company = CompanyModel.new()
444
+ company = MoesifApi::CompanyModel.new()
445
445
  company.company_id = "67890"
446
446
  company.company_domain = "acmeinc.com" # If domain is set, Moesif will enrich your profiles with publicly available info
447
447
  company.campaign = campaign
@@ -459,7 +459,7 @@ For details, visit the [Ruby API Reference](https://www.moesif.com/docs/api?ruby
459
459
  ```ruby
460
460
  companies = []
461
461
 
462
- metadata => {
462
+ metadata = {
463
463
  :org_name => 'Acme, Inc',
464
464
  :plan_name => 'Free',
465
465
  :deal_stage => 'Lead',
@@ -472,7 +472,7 @@ metadata => {
472
472
 
473
473
  # Campaign object is optional, but useful if you want to track ROI of acquisition channels
474
474
  # See https://www.moesif.com/docs/api#update-a-company for campaign schema
475
- campaign = CampaignModel.new()
475
+ campaign = MoesifApi::CampaignModel.new()
476
476
  campaign.utm_source = "google"
477
477
  campaign.utm_medium = "cpc"
478
478
  campaign.utm_campaign = "adwords"
@@ -481,7 +481,7 @@ campaign.utm_content = "landing"
481
481
 
482
482
  # Only company_id is required.
483
483
  # metadata can be any custom object
484
- company = CompanyModel.new()
484
+ company = MoesifApi::CompanyModel.new()
485
485
  company.company_id = "67890"
486
486
  company.company_domain = "acmeinc.com" # If domain is set, Moesif will enrich your profiles with publicly available info
487
487
  company.campaign = campaign
@@ -6,6 +6,8 @@ require_relative './client_ip.rb'
6
6
  require_relative './app_config.rb'
7
7
  require_relative './update_user.rb'
8
8
  require_relative './update_company.rb'
9
+ require 'zlib'
10
+ require 'stringio'
9
11
 
10
12
  module MoesifRack
11
13
 
@@ -71,6 +73,39 @@ module MoesifRack
71
73
  CompanyHelper.new.update_companies_batch(@api_controller, @debug, company_profiles)
72
74
  end
73
75
 
76
+ def start_with_json(body)
77
+ body.start_with?('{') || body.start_with?('[')
78
+ end
79
+
80
+ def decompress_body(body)
81
+ Zlib::GzipReader.new(StringIO.new(body)).read
82
+ end
83
+
84
+ def transform_headers(headers)
85
+ Hash[headers.map { |k, v| [k.downcase, v]}]
86
+ end
87
+
88
+ def base64_encode_body(body)
89
+ return Base64.encode64(body), 'base64'
90
+ end
91
+
92
+ def parse_body(body, headers)
93
+ begin
94
+ if start_with_json(body)
95
+ parsed_body = JSON.parse(body)
96
+ transfer_encoding = 'json'
97
+ elsif headers.key?('content-encoding') && ((headers['content-encoding'].downcase).include? "gzip")
98
+ uncompressed_string = decompress_body(body)
99
+ parsed_body, transfer_encoding = base64_encode_body(uncompressed_string)
100
+ else
101
+ parsed_body, transfer_encoding = base64_encode_body(body)
102
+ end
103
+ rescue
104
+ parsed_body, transfer_encoding = base64_encode_body(body)
105
+ end
106
+ return parsed_body, transfer_encoding
107
+ end
108
+
74
109
  def call env
75
110
  start_time = Time.now.utc.iso8601
76
111
 
@@ -86,7 +121,7 @@ module MoesifRack
86
121
  complex_copy = env.dup
87
122
 
88
123
  req_headers = {}
89
- complex_copy.select {|k,v| k.start_with? 'HTTP_'}.each do |key, val|
124
+ complex_copy.select {|k,v| k.start_with?('HTTP_', 'CONTENT_') }.each do |key, val|
90
125
  new_key = key.sub(/^HTTP_/, '')
91
126
  new_key = new_key.sub('_', '-')
92
127
  req_headers[new_key] = val
@@ -99,12 +134,7 @@ module MoesifRack
99
134
 
100
135
  if @log_body
101
136
  if req_body_string && req_body_string.length != 0
102
- begin
103
- req_body = JSON.parse(req_body_string)
104
- rescue
105
- req_body = Base64.encode64(req_body_string)
106
- req_body_transfer_encoding = 'base64'
107
- end
137
+ req_body, req_body_transfer_encoding = parse_body(req_body_string, transform_headers(req_headers))
108
138
  end
109
139
  end
110
140
 
@@ -116,12 +146,7 @@ module MoesifRack
116
146
 
117
147
  if @log_body
118
148
  if rsp_body_string && rsp_body_string.length != 0
119
- begin
120
- rsp_body = JSON.parse(rsp_body_string)
121
- rescue
122
- rsp_body = Base64.encode64(rsp_body_string)
123
- rsp_body_transfer_encoding = 'base64'
124
- end
149
+ rsp_body, rsp_body_transfer_encoding = parse_body(rsp_body_string, transform_headers(rsp_headers))
125
150
  end
126
151
  end
127
152
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moesif_rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moesif, Inc
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-12-18 00:00:00.000000000 Z
12
+ date: 2020-05-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit