awis-sdk-ruby 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f60e08f71c0092e6c7ff4185e1e64a6af7776d9
4
- data.tar.gz: cb3a28d169416fb66d36ff7d662295b777709de7
3
+ metadata.gz: 84250ba83bc8498d3506fdf84111da57be3f2fc4
4
+ data.tar.gz: cb4821875ce36ffbf6a880d0f71cae358d48f504
5
5
  SHA512:
6
- metadata.gz: b27b1755fc104f0fc196f1ed91c5e0d93408f3fbd62ccb39b3f7d02c135e2a34eca4db3d4a8a60ee819e0b9bf84aa48c8a392da7e5ed1c4d139e2f4474c0d553
7
- data.tar.gz: 9d9e2c0011a964374a10a9d89680b57aeebbe44bc5333980f4e40fe163af26c699caecdfbf493f1222aa7f136c59ae084407409ea6223752ed78862f0bfff388
6
+ metadata.gz: 6e33e6d07dfa95bd5b19277324391d0e25709a1cc5a0628035946aa532267ed9ba2c4679365191b8691fe1269f19cf8798d8f9e056b9e099e962d47e64539180
7
+ data.tar.gz: ac257c8001b304f1a74f0374bc384ec028e7fd3cdf9bef02fe1614b71837b378d5871b13ce5a76734b3db119fda51d520c001c239c04524dad9386b71d33326b
@@ -84,4 +84,4 @@ Style/NumericLiterals:
84
84
  Enabled: false
85
85
 
86
86
  Rails:
87
- Enabled: true
87
+ Enabled: false
data/README.md CHANGED
@@ -43,6 +43,7 @@ Returns object that contains attributes:
43
43
  * data_url
44
44
  * rank
45
45
  * asin
46
+ * xml
46
47
 
47
48
  Returns object that contains relationships:
48
49
 
@@ -175,4 +176,4 @@ Awis is using `nokogiri` to parse XML documents.
175
176
 
176
177
  ### Copyright
177
178
 
178
- Copyright (c) Encore Shao. See LICENSE for details.
179
+ Copyright (c) Encore Shao. See LICENSE for details.
@@ -18,5 +18,5 @@ module Awis
18
18
  SERVICE_PATH = 'api'
19
19
  SERVICE_NAME = 'awis'
20
20
  SERVICE_REGION = 'us-west-1'
21
- SERVICE_HOST = "#{SERVICE_NAME}.amazonaws.com"
21
+ SERVICE_HOST = "#{SERVICE_NAME}.#{SERVICE_REGION}.amazonaws.com"
22
22
  end
@@ -28,7 +28,7 @@ module Awis
28
28
 
29
29
  @arguments = arguments
30
30
  @arguments[:range] = arguments.fetch(:range, 31)
31
- @arguments[:start] = arguments.fetch(:start) { Time.zone.now - (3600 * 24 * @arguments[:range].to_i) }
31
+ @arguments[:start] = arguments.fetch(:start) { Time.now - (3600 * 24 * @arguments[:range].to_i) }
32
32
  end
33
33
 
34
34
  def start_date
@@ -10,14 +10,14 @@ module Awis
10
10
  attr_reader :params, :secret_access_key, :access_key_id
11
11
 
12
12
  HEADERS = {
13
- 'Content-Type' => 'application/xml',
14
- 'Accept' => 'application/xml',
15
- 'User-Agent' => "awis-sdk-ruby v#{Awis::VERSION}"
13
+ 'accept' => 'application/xml',
14
+ 'content-type' => 'application/xml',
15
+ 'user-agent' => "awis-sdk-ruby v#{Awis::VERSION}"
16
16
  }.freeze
17
17
 
18
18
  def initialize(options = {})
19
- @secret_access_key = options.fetch(:secret_access_key, Awis.config.access_key_id)
20
- @access_key_id = options.fetch(:access_key_id, Awis.config.secret_access_key)
19
+ @secret_access_key = options.fetch(:secret_access_key, Awis.config.secret_access_key)
20
+ @access_key_id = options.fetch(:access_key_id, Awis.config.access_key_id)
21
21
  raise CertificateError, 'Amazon access certificate is missing!' if @secret_access_key.nil? || @access_key_id.nil?
22
22
 
23
23
  setup_options!
@@ -32,7 +32,7 @@ module Awis
32
32
 
33
33
  def get(params = {})
34
34
  @params = params
35
- handle_response(request).body.force_encoding(Encoding::UTF_8)
35
+ handle_response(request).body
36
36
  end
37
37
 
38
38
  private
@@ -77,7 +77,7 @@ module Awis
77
77
  end
78
78
 
79
79
  def uri
80
- @uri ||= URI.parse("#{protocol}://#{Awis::SERVICE_HOST}/#{Awis::SERVICE_PATH}?" << query)
80
+ @uri ||= URI.parse("#{protocol}://#{Awis::SERVICE_HOST}/#{Awis::SERVICE_PATH}?#{query}")
81
81
  end
82
82
 
83
83
  def headers
@@ -3,17 +3,21 @@
3
3
  module Awis
4
4
  module Models
5
5
  class UrlInfo < Base
6
- attr_accessor :data_url, :rank, :asin, :contact_info, :content_data, :usage_statistics, :related_links, :categories
6
+ attr_accessor :data_url, :rank, :asin, :contact_info, :content_data, :usage_statistics, :related_links,
7
+ :categories, :xml, :contributing_subdomains, :rank_by_country
7
8
 
8
9
  def initialize(response)
9
10
  @usage_statistics = []
10
11
  @related_links = []
11
12
  @categories = []
13
+ @contributing_subdomains = []
14
+ @rank_by_country = []
12
15
 
13
16
  setup_data! loading_response(response)
14
17
  end
15
18
 
16
19
  def setup_data!(response)
20
+ @xml = response
17
21
  content_data = {
18
22
  owned_domains: []
19
23
  }
@@ -23,94 +27,122 @@ module Awis
23
27
  statistics = []
24
28
  related_related_links = []
25
29
  category_data = []
30
+ rank_by_country = []
31
+ contributing_subdomains = []
26
32
 
27
33
  response.each_node do |node, path|
28
34
  text = node.inner_xml
29
- text = text.to_i if text.to_i.to_s == text && node.name != 'aws:Delta'
30
- text = nil if text.class == String && text.empty?
35
+ candidate_text = text.delete(',')
36
+ value = if integer_value?(candidate_text) && node.name != 'aws:Delta'
37
+ candidate_text.to_i
38
+ elsif float_value?(candidate_text)
39
+ candidate_text.to_f
40
+ elsif text.is_a?(String) && text.empty?
41
+ nil
42
+ else
43
+ text
44
+ end
31
45
 
32
46
  if node.name == 'aws:RequestId'
33
- @request_id ||= text
47
+ @request_id ||= value
34
48
  elsif node.name == 'aws:StatusCode'
35
- @status_code ||= text
49
+ @status_code ||= value
36
50
  elsif node.name == 'aws:DataUrl' && path == "#{traffic_node_name}/aws:DataUrl"
37
- @data_url = text
51
+ @data_url = value
38
52
  elsif node.name == 'aws:Asin' && path == "#{traffic_node_name}/aws:Asin"
39
- @asin = text
53
+ @asin = value
40
54
  elsif node.name == 'aws:Rank' && path == "#{traffic_node_name}/aws:Rank"
41
- @rank = text
55
+ @rank = value
42
56
  elsif node.name == 'aws:DataUrl' && path == "#{content_node_name}/aws:DataUrl"
43
- content_data[:data_url] = text
57
+ content_data[:data_url] = value
44
58
  elsif node.name == 'aws:Title' && path == "#{content_node_name}/aws:SiteData/aws:Title"
45
- content_data[:site_title] = text
59
+ content_data[:site_title] = value
46
60
  elsif node.name == 'aws:Description'
47
- content_data[:site_description] = text
61
+ content_data[:site_description] = value
48
62
  elsif node.name == 'aws:MedianLoadTime'
49
- content_data[:speed_median_load_time] = text
63
+ content_data[:speed_median_load_time] = value
50
64
  elsif node.name == 'aws:Percentile'
51
- content_data[:speed_percentile] = text
65
+ content_data[:speed_percentile] = value
52
66
  elsif node.name == 'aws:AdultContent'
53
- content_data[:adult_content] = text
67
+ content_data[:adult_content] = value
54
68
  elsif node.name == 'aws:Locale'
55
- content_data[:language_locale] = text
69
+ content_data[:language_locale] = value
56
70
  elsif node.name == 'aws:LinksInCount'
57
- content_data[:links_in_count] = text
71
+ content_data[:links_in_count] = value
58
72
  elsif node.name == 'aws:Domain' && path == "#{content_node_name}/aws:OwnedDomains/aws:OwnedDomain/aws:Domain"
59
- content_data[:owned_domains] << { domain: text }
73
+ content_data[:owned_domains] << { domain: value }
60
74
  elsif node.name == 'aws:Title' && path == "#{content_node_name}/aws:OwnedDomains/aws:OwnedDomain/aws:Title"
61
- content_data[:owned_domains] << { title: text }
75
+ content_data[:owned_domains] << { title: value }
62
76
  elsif node.name == 'aws:OnlineSince'
63
- content_data[:online_since] = text
77
+ content_data[:online_since] = value
64
78
  elsif node.name == 'aws:DataUrl' && path == "#{root_node_name}/aws:ContactInfo/aws:DataUrl"
65
- contact_info[:data_url] = text
79
+ contact_info[:data_url] = value
66
80
  elsif node.name == 'aws:OwnerName'
67
- contact_info[:owner_name] = text
81
+ contact_info[:owner_name] = value
68
82
  elsif node.name == 'aws:Email'
69
- contact_info[:email] = text
83
+ contact_info[:email] = value
70
84
  elsif node.name == 'aws:PhysicalAddress'
71
- contact_info[:physical_address] = text
85
+ contact_info[:physical_address] = value
72
86
  elsif node.name == 'aws:CompanyStockTicker'
73
- contact_info[:company_stock_ticker] = text
87
+ contact_info[:company_stock_ticker] = value
74
88
  elsif node.name == 'aws:PhoneNumber'
75
- contact_info[:phone_numbers] << text
89
+ contact_info[:phone_numbers] << value
76
90
  elsif node.name == 'aws:DataUrl' && path == "#{related_links_node_name}/aws:DataUrl"
77
- related_related_links << { data_url: text }
91
+ related_related_links << { data_url: value }
78
92
  elsif node.name == 'aws:NavigableUrl' && path == "#{related_links_node_name}/aws:NavigableUrl"
79
- related_related_links << { navigable_url: text }
93
+ related_related_links << { navigable_url: value }
80
94
  elsif node.name == 'aws:Title' && path == "#{related_links_node_name}/aws:Title"
81
- related_related_links << { title: text }
95
+ related_related_links << { title: value }
82
96
  elsif node.name == 'aws:Title' && path == "#{categories_node_name}/aws:Title"
83
- category_data << { title: text }
97
+ category_data << { title: value }
84
98
  elsif node.name == 'aws:AbsolutePath' && path == "#{categories_node_name}/aws:AbsolutePath"
85
- category_data << { absolute_path: text }
99
+ category_data << { absolute_path: value }
86
100
  elsif node.name == 'aws:Months' && path == "#{statistic_node_name}/aws:TimeRange/aws:Months"
87
- statistics << { time_range_months: text }
101
+ statistics << { time_range_months: value }
88
102
  elsif node.name == 'aws:Days' && path == "#{statistic_node_name}/aws:TimeRange/aws:Days"
89
- statistics << { time_range_days: text }
103
+ statistics << { time_range_days: value }
90
104
  elsif node.name == 'aws:Value' && path == "#{statistic_node_name}/aws:Rank/aws:Value"
91
- statistics << { rank_value: text }
105
+ statistics << { rank_value: value }
92
106
  elsif node.name == 'aws:Delta' && path == "#{statistic_node_name}/aws:Rank/aws:Delta"
93
- statistics << { rank_delta: text }
107
+ statistics << { rank_delta: value }
94
108
  elsif node.name == 'aws:Value' && path == "#{statistic_node_name}/aws:Reach/aws:Rank/aws:Value"
95
- statistics << { reach_rank_value: text }
109
+ statistics << { reach_rank_value: value }
96
110
  elsif node.name == 'aws:Delta' && path == "#{statistic_node_name}/aws:Reach/aws:Rank/aws:Delta"
97
- statistics << { reach_rank_delta: text }
111
+ statistics << { reach_rank_delta: value }
98
112
  elsif node.name == 'aws:Value' && path == "#{statistic_node_name}/aws:Reach/aws:PerMillion/aws:Value"
99
- statistics << { reach_per_million_value: text }
113
+ statistics << { reach_per_million_value: value }
100
114
  elsif node.name == 'aws:Delta' && path == "#{statistic_node_name}/aws:Reach/aws:PerMillion/aws:Delta"
101
- statistics << { reach_per_million_delta: text }
115
+ statistics << { reach_per_million_delta: value }
102
116
  elsif node.name == 'aws:Value' && path == "#{statistic_node_name}/aws:PageViews/aws:PerMillion/aws:Value"
103
- statistics << { reach_page_views_per_million_value: text }
117
+ statistics << { page_views_per_million_value: value }
104
118
  elsif node.name == 'aws:Delta' && path == "#{statistic_node_name}/aws:PageViews/aws:PerMillion/aws:Delta"
105
- statistics << { reach_page_views_per_million_delta: text }
119
+ statistics << { page_views_per_million_delta: value }
106
120
  elsif node.name == 'aws:Value' && path == "#{statistic_node_name}/aws:PageViews/aws:Rank/aws:Value"
107
- statistics << { reach_page_views_rank_value: text }
121
+ statistics << { page_views_rank_value: value }
108
122
  elsif node.name == 'aws:Delta' && path == "#{statistic_node_name}/aws:PageViews/aws:Rank/aws:Delta"
109
- statistics << { reach_page_views_rank_delta: text }
123
+ statistics << { page_views_rank_delta: value }
110
124
  elsif node.name == 'aws:Value' && path == "#{statistic_node_name}/aws:PageViews/aws:PerUser/aws:Value"
111
- statistics << { reach_page_views_per_user_value: text }
125
+ statistics << { page_views_per_user_value: value }
112
126
  elsif node.name == 'aws:Delta' && path == "#{statistic_node_name}/aws:PageViews/aws:PerUser/aws:Delta"
113
- statistics << { reach_page_views_per_user_delta: text }
127
+ statistics << { page_views_per_user_delta: value }
128
+ elsif node.name == 'aws:Country' && path == rank_by_country_node_name
129
+ rank_by_country << { country_code: node.attributes['Code'] }
130
+ elsif node.name == 'aws:Rank' && path == "#{rank_by_country_node_name}/aws:Rank"
131
+ rank_by_country << { rank: value }
132
+ elsif node.name == 'aws:PageViews' && path == "#{rank_by_country_node_name}/aws:Contribution/aws:PageViews"
133
+ rank_by_country << { contribution_page_views: value }
134
+ elsif node.name == 'aws:Users' && path == "#{rank_by_country_node_name}/aws:Contribution/aws:Users"
135
+ rank_by_country << { contribution_users: value }
136
+ elsif node.name == 'aws:DataUrl' && path == "#{contributing_subdomains_node_name}/aws:DataUrl"
137
+ contributing_subdomains << { data_url: value }
138
+ elsif node.name == 'aws:Months' && path == "#{contributing_subdomains_node_name}/aws:TimeRange/aws:Months"
139
+ contributing_subdomains << { time_range_months: value }
140
+ elsif node.name == 'aws:Percentage' && path == "#{contributing_subdomains_node_name}/aws:Reach/aws:Percentage"
141
+ contributing_subdomains << { reach_percentage: value }
142
+ elsif node.name == 'aws:Percentage' && path == "#{contributing_subdomains_node_name}/aws:PageViews/aws:Percentage"
143
+ contributing_subdomains << { page_views_percentage: value }
144
+ elsif node.name == 'aws:PerUser' && path == "#{contributing_subdomains_node_name}/aws:PageViews/aws:PerUser"
145
+ contributing_subdomains << { page_views_per_user: value }
114
146
  end
115
147
  end
116
148
 
@@ -120,6 +152,8 @@ module Awis
120
152
  relationship_collections(@usage_statistics, statistics, 13, UsageStatistic)
121
153
  relationship_collections(@related_links, related_related_links, 3, RelatedLink)
122
154
  relationship_collections(@categories, category_data, 3, CategoryData)
155
+ relationship_collections(@rank_by_country, rank_by_country, 4, RankByCountry)
156
+ relationship_collections(@contributing_subdomains, contributing_subdomains, 5, ContributingSubdomain)
123
157
  end
124
158
 
125
159
  def is_404?
@@ -155,6 +189,126 @@ module Awis
155
189
  def statistic_node_name
156
190
  "#{traffic_node_name}/aws:UsageStatistics/aws:UsageStatistic"
157
191
  end
192
+
193
+ def contributing_subdomains_node_name
194
+ "#{traffic_node_name}/aws:ContributingSubdomains/aws:ContributingSubdomain"
195
+ end
196
+
197
+ def rank_by_country_node_name
198
+ "#{traffic_node_name}/aws:RankByCountry/aws:Country"
199
+ end
200
+
201
+ def has_data?
202
+ !@rank.nil?
203
+ end
204
+
205
+ def geos_sorted
206
+ rank_by_country.select { |rbc| !rbc.rank.nil? && !rbc.contribution_page_views.nil? }.
207
+ sort_by { |rbc| - rbc.contribution_page_views.round }.
208
+ map { |rbc| { rbc.country_code => rbc.contribution_page_views.round } }
209
+ end
210
+
211
+ def geos_hash
212
+ @geos_hash ||= geos_sorted.reduce({}, :merge)
213
+ end
214
+
215
+ def domains
216
+ content_data.owned_domains.map(&:domain)
217
+ end
218
+
219
+ def contributing_hostnames
220
+ contributing_subdomains.map(&:data_url).reject { |hostname| hostname == 'OTHER' }
221
+ end
222
+
223
+ def pvs_per_user
224
+ usage_statistics.first.page_views_per_user_value
225
+ end
226
+
227
+ def pvs_rank
228
+ usage_statistics.first.page_views_rank_value
229
+ end
230
+
231
+ def get_pvs_per_mil
232
+ usage_statistics.first.page_views_per_million_value
233
+ end
234
+
235
+ def speed_percentile
236
+ content_data.speed_percentile
237
+ end
238
+
239
+ def get_median_load_time
240
+ content_data.speed_median_load_time
241
+ end
242
+
243
+ def daily_GDN_page_views
244
+ if rank
245
+ alexa_gdn.each do |max_pvs, gdn_range|
246
+ return gdn_range if rank > max_pvs
247
+ end
248
+ end
249
+ end
250
+
251
+ def speed_rating
252
+ if get_median_load_time
253
+ alexa_speed_rating.each do |max_load_time, rating|
254
+ return rating if get_median_load_time > max_load_time
255
+ end
256
+ end
257
+ 'AVERAGE ( < 5s)'
258
+ end
259
+
260
+ def alexa_speed_rating
261
+ {
262
+ 2200 => 'POOR ( > 5s)',
263
+ 1200 => 'AVERAGE ( < 5s)',
264
+ 0 => 'GOOD ( < 3s)'
265
+ }
266
+ end
267
+
268
+ def rank_page_view
269
+ {
270
+ 20000 => '< 1K',
271
+ 5000 => '1K - 10K',
272
+ 3000 => '10K - 100K',
273
+ 1900 => '100K - 500K',
274
+ 1300 => '500K - 1M',
275
+ 850 => '2M - 5M',
276
+ 550 => '5M - 10M',
277
+ 350 => '10M - 20M',
278
+ 200 => '20M - 50M',
279
+ 100 => '50M - 100M',
280
+ 28 => '100M+'
281
+ }
282
+ end
283
+
284
+ def alexa_gdn
285
+ {
286
+ 250000 => '< 1K',
287
+ 100000 => '1K - 10K',
288
+ 50000 => '10K - 100K',
289
+ 20000 => '100K - 500K',
290
+ 10000 => '500K - 1M',
291
+ 5000 => '1M - 2M',
292
+ 2000 => '2M - 5M',
293
+ 1000 => '5M - 10M',
294
+ 500 => '10M - 20M',
295
+ 150 => '20M - 50M',
296
+ 30 => '50M - 100M',
297
+ 0 => '100M+'
298
+ }
299
+ end
300
+
301
+ private
302
+
303
+ def integer_value?(text)
304
+ text.to_i.to_s == text
305
+ end
306
+
307
+ def float_value?(text)
308
+ !!Float(text.delete('%'))
309
+ rescue StandardError
310
+ false
311
+ end
158
312
  end
159
313
 
160
314
  class ContentData < BaseEntity
@@ -190,7 +344,7 @@ module Awis
190
344
  end
191
345
 
192
346
  def phone_number_collections(phone_numbers)
193
- return @phone_numbers = [] if phone_numbers.blank?
347
+ return @phone_numbers = [] if phone_numbers.nil? || phone_numbers.empty?
194
348
 
195
349
  phone_numbers.map { |item| @phone_numbers << PhoneNumber.new(item) }
196
350
  end
@@ -212,12 +366,12 @@ module Awis
212
366
  attr_accessor :time_range_months, :time_range_days, :rank_value, :rank_delta,
213
367
  :reach_rank_value, :reach_rank_delta,
214
368
  :reach_per_million_value, :reach_per_million_delta,
215
- :reach_page_views_per_million_value, :reach_page_views_per_million_delta,
216
- :reach_page_views_rank_value, :reach_page_views_rank_delta,
217
- :reach_page_views_per_user_value, :reach_page_views_per_user_delta
369
+ :page_views_per_million_value, :page_views_per_million_delta,
370
+ :page_views_rank_value, :page_views_rank_delta,
371
+ :page_views_per_user_value, :page_views_per_user_delta
218
372
 
219
373
  def range_type
220
- return 'month' if time_range_months.present?
374
+ return 'month' unless time_range_months.nil? || time_range_months.empty?
221
375
 
222
376
  'day'
223
377
  end
@@ -226,5 +380,13 @@ module Awis
226
380
  (time_range_months || time_range_days)
227
381
  end
228
382
  end
383
+
384
+ class ContributingSubdomain < BaseEntity
385
+ attr_accessor :data_url, :time_range_months, :reach_percentage, :page_views_percentage, :page_views_per_user
386
+ end
387
+
388
+ class RankByCountry < BaseEntity
389
+ attr_accessor :country_code, :rank, :contribution_page_views, :contribution_users
390
+ end
229
391
  end
230
392
  end
@@ -3,6 +3,8 @@
3
3
  module Awis
4
4
  module Utils
5
5
  class XML
6
+ attr_reader :data
7
+
6
8
  def initialize(data)
7
9
  @data = data
8
10
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Awis
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awis-sdk-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Encore Shao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-02 00:00:00.000000000 Z
11
+ date: 2018-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sigv4