awis-sdk-ruby 1.0.0 → 1.1.0

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.
@@ -1,7 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  module API
3
5
  class UrlInfo < Base
4
- DEFAULT_RESPONSE_GROUP = %w(related_links categories rank rank_by_country usage_stats adult_content speed language owned_domains links_in_count site_data).freeze
6
+ DEFAULT_RESPONSE_GROUP = %w[
7
+ related_links categories rank rank_by_country usage_stats
8
+ adult_content speed language owned_domains links_in_count site_data
9
+ ].freeze
5
10
 
6
11
  def load_request_uri(arguments = {})
7
12
  validation_arguments!(arguments)
@@ -10,6 +15,7 @@ module Awis
10
15
  end
11
16
 
12
17
  private
18
+
13
19
  def validation_arguments!(arguments)
14
20
  before_validation_arguments(arguments)
15
21
 
@@ -19,14 +25,14 @@ module Awis
19
25
 
20
26
  def params
21
27
  {
22
- "Action" => action_name,
23
- "Url" => arguments[:url],
24
- "ResponseGroup" => response_groups,
28
+ 'Action' => action_name,
29
+ 'Url' => arguments[:url],
30
+ 'ResponseGroup' => response_groups
25
31
  }
26
32
  end
27
33
 
28
34
  def response_groups
29
- arguments[:response_group].sort.map { |group| camelize(group) }.join(",")
35
+ arguments[:response_group].sort.map { |group| camelize(group) }.join(',')
30
36
  end
31
37
  end
32
38
  end
@@ -1,30 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  class Client
3
5
  def initialize
4
- raise CertificateError.new("Amazon access certificate is missing!") if Awis.config.access_key_id.nil? || Awis.config.secret_access_key.nil?
6
+ raise CertificateError, 'Amazon access certificate is missing!' if Awis.config.access_key_id.nil? || Awis.config.secret_access_key.nil?
5
7
  end
6
8
 
7
9
  def url_info(args)
8
- parse_response_with_request("UrlInfo", args)
10
+ parse_response_with_request('UrlInfo', args)
9
11
  end
10
12
 
11
13
  def traffic_history(args)
12
- parse_response_with_request("TrafficHistory", args)
14
+ parse_response_with_request('TrafficHistory', args)
13
15
  end
14
16
 
15
17
  def sites_linking_in(args)
16
- parse_response_with_request("SitesLinkingIn", args)
18
+ parse_response_with_request('SitesLinkingIn', args)
17
19
  end
18
20
 
19
21
  def category_browse(args)
20
- parse_response_with_request("CategoryBrowse", args)
22
+ parse_response_with_request('CategoryBrowse', args)
21
23
  end
22
24
 
23
25
  def category_listings(args)
24
- parse_response_with_request("CategoryListings", args)
26
+ parse_response_with_request('CategoryListings', args)
25
27
  end
26
28
 
27
29
  private
30
+
28
31
  def parse_response_with_request(kclass, args)
29
32
  case kclass
30
33
  when 'UrlInfo'
@@ -1,4 +1,6 @@
1
- require "singleton"
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
2
4
 
3
5
  module Awis
4
6
  class Config
@@ -1,12 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi'
4
+ require 'aws-sigv4'
5
+ require 'net/https'
6
+
1
7
  module Awis
2
8
  class Connection
3
- include Awis::Utils::Request
4
-
5
9
  attr_accessor :debug, :protocol
6
- attr_writer :params
10
+ attr_reader :params, :secret_access_key, :access_key_id
7
11
 
8
- def initialize
9
- raise CertificateError.new("Amazon access certificate is missing!") if Awis.config.access_key_id.nil? || Awis.config.secret_access_key.nil?
12
+ HEADERS = {
13
+ 'Content-Type' => 'application/xml',
14
+ 'Accept' => 'application/xml',
15
+ 'User-Agent' => "awis-sdk-ruby v#{Awis::VERSION}"
16
+ }.freeze
17
+
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)
21
+ raise CertificateError, 'Amazon access certificate is missing!' if @secret_access_key.nil? || @access_key_id.nil?
10
22
 
11
23
  setup_options!
12
24
  end
@@ -18,19 +30,81 @@ module Awis
18
30
  @open_timeout = Awis.config.open_timeout || 10
19
31
  end
20
32
 
21
- def params
22
- @params ||= {}
33
+ def get(params = {})
34
+ @params = params
35
+ handle_response(request).body.force_encoding(Encoding::UTF_8)
23
36
  end
24
37
 
25
- def setup_params(params)
26
- self.params = params
38
+ private
39
+
40
+ def handle_response(response)
41
+ case response
42
+ when Net::HTTPSuccess
43
+ response
44
+ else
45
+ handle_error_response(response)
46
+ end
27
47
  end
28
48
 
29
- def get(params = {})
30
- setup_params(params)
49
+ def handle_error_response(response)
50
+ case response.code.to_i
51
+ when 300...600
52
+ if response.body.nil?
53
+ raise ResponseError.new(nil, response)
54
+ else
55
+ error_message = MultiXml.parse(response.body).deep_find('ErrorCode')
56
+ raise ResponseError.new(error_message, response)
57
+ end
58
+ else
59
+ raise ResponseError.new("Unknown code: #{response.code}", response)
60
+ end
61
+ end
62
+
63
+ def request
64
+ req = Net::HTTP::Get.new(uri)
65
+ headers.each do |key, value|
66
+ req[key] = value
67
+ end
68
+ Net::HTTP.start(
69
+ uri.hostname,
70
+ uri.port,
71
+ use_ssl: uri.scheme == 'https',
72
+ ssl_timeout: @timeout,
73
+ open_timeout: @open_timeout
74
+ ) do |http|
75
+ http.request(req)
76
+ end
77
+ end
78
+
79
+ def uri
80
+ @uri ||= URI.parse("#{protocol}://#{Awis::SERVICE_HOST}/#{Awis::SERVICE_PATH}?" << query)
81
+ end
82
+
83
+ def headers
84
+ HEADERS.merge(auth_headers)
85
+ end
86
+
87
+ def auth_headers
88
+ signer.sign_request(
89
+ http_method: 'GET',
90
+ headers: HEADERS,
91
+ url: uri.to_s
92
+ ).headers
93
+ end
94
+
95
+ def signer
96
+ Aws::Sigv4::Signer.new(
97
+ service: Awis::SERVICE_NAME,
98
+ region: Awis::SERVICE_REGION,
99
+ access_key_id: access_key_id,
100
+ secret_access_key: secret_access_key
101
+ )
102
+ end
31
103
 
32
- response = handle_response(request)
33
- response.body.force_encoding(Encoding::UTF_8)
104
+ def query
105
+ params.map do |key, value|
106
+ "#{key}=#{CGI.escape(value.to_s)}"
107
+ end.sort!.join('&')
34
108
  end
35
109
  end
36
110
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
- # Awis exceptions can be cought by rescuing: Awis::StandardError
4
+ # Awis exceptions can be caught by rescuing: Awis::StandardError
3
5
 
4
6
  class StandardError < StandardError; end
5
7
  class ArgumentError < StandardError; end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Hash
2
4
  def array_slice_merge!(key, array, count)
3
5
  self[key] = array.each_slice(count).collect { |e| e.reduce({}, :merge) }
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  module Models
3
- autoload :Base, "awis/models/base"
4
- autoload :BaseEntity, "awis/models/base_entity"
5
- autoload :UrlInfo, "awis/models/url_info"
6
- autoload :TrafficHistory, "awis/models/traffic_history"
7
- autoload :SitesLinkingIn, "awis/models/sites_linking_in"
8
- autoload :CategoryListings, "awis/models/category_listings"
9
- autoload :CategoryBrowse, "awis/models/category_browse"
5
+ autoload :Base, 'awis/models/base'
6
+ autoload :BaseEntity, 'awis/models/base_entity'
7
+ autoload :UrlInfo, 'awis/models/url_info'
8
+ autoload :TrafficHistory, 'awis/models/traffic_history'
9
+ autoload :SitesLinkingIn, 'awis/models/sites_linking_in'
10
+ autoload :CategoryListings, 'awis/models/category_listings'
11
+ autoload :CategoryBrowse, 'awis/models/category_browse'
10
12
  end
11
13
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  module Models
3
5
  class Base
@@ -15,11 +17,11 @@ module Awis
15
17
  self.class.name.split(/\:\:/)[-1]
16
18
  end
17
19
 
18
- def relationship_collections(_object, items, items_count, kclass)
20
+ def relationship_collections(item_object, items, items_count, kclass)
19
21
  return if items.empty?
20
22
 
21
23
  all_items = {}.array_slice_merge!(:item, items, items_count)
22
- all_items.map { |item| _object << kclass.new(item) }
24
+ all_items.map { |item| item_object << kclass.new(item) }
23
25
  end
24
26
 
25
27
  def success?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  module Models
3
5
  class BaseEntity
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  module Models
3
5
  class CategoryBrowse < Base
@@ -12,6 +14,11 @@ module Awis
12
14
  setup_data! loading_response(response)
13
15
  end
14
16
 
17
+ # rubocop:disable Metrics/AbcSize
18
+ # rubocop:disable Metrics/CyclomaticComplexity
19
+ # rubocop:disable Metrics/PerceivedComplexity
20
+ # rubocop:disable Metrics/MethodLength
21
+ # rubocop:disable Metrics/BlockLength
15
22
  def setup_data!(response)
16
23
  categories = []
17
24
  language_categories = []
@@ -20,8 +27,8 @@ module Awis
20
27
 
21
28
  response.each_node do |node, path|
22
29
  text = node.inner_xml
23
- text = text.to_i if text.to_i.to_s === text
24
- text = nil if (text.class == String && text.empty?)
30
+ text = text.to_i if text.to_i.to_s == text
31
+ text = nil if text.class == String && text.empty?
25
32
 
26
33
  if node.name == 'aws:RequestId'
27
34
  @request_id ||= text
@@ -67,6 +74,11 @@ module Awis
67
74
  relationship_collections(@related_categories, related_categories, 4, RelatedCategory)
68
75
  relationship_collections(@letter_bars, letter_bars, 4, LetterBar)
69
76
  end
77
+ # rubocop:enable Metrics/AbcSize
78
+ # rubocop:enable Metrics/CyclomaticComplexity
79
+ # rubocop:enable Metrics/PerceivedComplexity
80
+ # rubocop:enable Metrics/MethodLength
81
+ # rubocop:enable Metrics/BlockLength
70
82
 
71
83
  def base_node_name
72
84
  "#{root_node_name}/aws:CategoryBrowse"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  module Models
3
5
  class CategoryListings < Base
@@ -13,8 +15,8 @@ module Awis
13
15
 
14
16
  response.each_node do |node, path|
15
17
  text = node.inner_xml
16
- text = text.to_i if text.to_i.to_s === text
17
- text = nil if (text.class == String && text.empty?)
18
+ text = text.to_i if text.to_i.to_s == text
19
+ text = nil if text.class == String && text.empty?
18
20
 
19
21
  if node.name == 'aws:RequestId'
20
22
  @request_id ||= text
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  module Models
3
5
  class SitesLinkingIn < Base
@@ -11,9 +13,9 @@ module Awis
11
13
  def setup_data!(response)
12
14
  sites = []
13
15
 
14
- response.each_node do |node, path|
16
+ response.each_node do |node, _path|
15
17
  text = node.inner_xml
16
- text = nil if (text.class == String && text.empty?)
18
+ text = nil if text.class == String && text.empty?
17
19
 
18
20
  if node.name == 'aws:RequestId'
19
21
  @request_id ||= text
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  module Models
3
5
  class TrafficHistory < Base
@@ -8,13 +10,16 @@ module Awis
8
10
  setup_data! loading_response(response)
9
11
  end
10
12
 
13
+ # rubocop:disable Metrics/AbcSize
14
+ # rubocop:disable Metrics/CyclomaticComplexity
15
+ # rubocop:disable Metrics/PerceivedComplexity
11
16
  def setup_data!(response)
12
17
  datas = []
13
18
 
14
19
  response.each_node do |node, path|
15
20
  text = node.inner_xml
16
- text = text.to_i if text.to_i.to_s === text
17
- text = nil if (text.class == String && text.empty?)
21
+ text = text.to_i if text.to_i.to_s == text
22
+ text = nil if text.class == String && text.empty?
18
23
 
19
24
  if node.name == 'aws:RequestId'
20
25
  @request_id ||= text
@@ -41,6 +46,9 @@ module Awis
41
46
 
42
47
  relationship_collections(@historical_data, datas, 5, HistoricalData)
43
48
  end
49
+ # rubocop:enable Metrics/AbcSize
50
+ # rubocop:enable Metrics/CyclomaticComplexity
51
+ # rubocop:enable Metrics/PerceivedComplexity
44
52
 
45
53
  def base_node_name
46
54
  "#{root_node_name}/aws:TrafficHistory/aws:HistoricalData/aws:Data"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Awis
2
4
  module Models
3
5
  class UrlInfo < Base
@@ -24,8 +26,8 @@ module Awis
24
26
 
25
27
  response.each_node do |node, path|
26
28
  text = node.inner_xml
27
- text = text.to_i if text.to_i.to_s === text && node.name != 'aws:Delta'
28
- text = nil if (text.class == String && text.empty?)
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?
29
31
 
30
32
  if node.name == 'aws:RequestId'
31
33
  @request_id ||= text
@@ -75,7 +77,7 @@ module Awis
75
77
  related_related_links << { data_url: text }
76
78
  elsif node.name == 'aws:NavigableUrl' && path == "#{related_links_node_name}/aws:NavigableUrl"
77
79
  related_related_links << { navigable_url: text }
78
- elsif node.name == 'aws:Title' && path == "#{related_links_node_name}/aws:Title"
80
+ elsif node.name == 'aws:Title' && path == "#{related_links_node_name}/aws:Title"
79
81
  related_related_links << { title: text }
80
82
  elsif node.name == 'aws:Title' && path == "#{categories_node_name}/aws:Title"
81
83
  category_data << { title: text }
@@ -166,11 +168,11 @@ module Awis
166
168
  owned_domains_relationship_collections(@owned_domains, owned_domain_objects, 2, OwnedDomain)
167
169
  end
168
170
 
169
- def owned_domains_relationship_collections(_object, items, items_count, kclass)
171
+ def owned_domains_relationship_collections(item_object, items, items_count, kclass)
170
172
  return if items.empty?
171
173
 
172
174
  all_items = {}.array_slice_merge!(:item, items, items_count)
173
- all_items.map { |item| _object << kclass.new(item) }
175
+ all_items.map { |item| item_object << kclass.new(item) }
174
176
  end
175
177
  end
176
178
 
@@ -188,7 +190,7 @@ module Awis
188
190
  end
189
191
 
190
192
  def phone_number_collections(phone_numbers)
191
- return @phone_numbers = [] if phone_numbers.nil? || phone_numbers.empty?
193
+ return @phone_numbers = [] if phone_numbers.blank?
192
194
 
193
195
  phone_numbers.map { |item| @phone_numbers << PhoneNumber.new(item) }
194
196
  end
@@ -215,7 +217,7 @@ module Awis
215
217
  :reach_page_views_per_user_value, :reach_page_views_per_user_delta
216
218
 
217
219
  def range_type
218
- return 'month' unless time_range_months.blank?
220
+ return 'month' if time_range_months.present?
219
221
 
220
222
  'day'
221
223
  end
@@ -1,9 +1,10 @@
1
- require "awis/utils/extra"
2
- require "awis/utils/variable"
3
- require "awis/utils/request"
1
+ # frozen_string_literal: true
2
+
3
+ require 'awis/utils/extra'
4
+ require 'awis/utils/variable'
4
5
 
5
6
  module Awis
6
7
  module Utils
7
- autoload :XML, "awis/utils/xml"
8
+ autoload :XML, 'awis/utils/xml'
8
9
  end
9
10
  end