naver-searchad-api 0.0.4 → 0.0.6

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
  SHA1:
3
- metadata.gz: f8a2c4603328a963fa278a5f8d94482d003c112f
4
- data.tar.gz: a25819a0b5d0ece492c6b5be61b52f86995401d7
3
+ metadata.gz: d573b3637886b895a912c269f9a1f4278418abd0
4
+ data.tar.gz: 49e9456fd4cf5796c7a28250a6fed8497aaa007e
5
5
  SHA512:
6
- metadata.gz: 177d37cd338d82d3a8aa915aef548660502ffe047c099d0df7a41c537ebea85936bc8d50d4765232668f5c29cd28a807d1bcde544e1a2a47e45a05acda7ed952
7
- data.tar.gz: 05307676ee81d332f74e33739ef2de713e182a0abd1c8400101724d1080ebf8d95fa4ac82f87a2e565f5cf211fb63fd7c2858b49df3a7522dfd765b61f15343f
6
+ metadata.gz: 274955fab04a7135c408a446193371107396b22e0c6ffaad18fa9bb6153bb011a897562adfa5ecb93f28e5f3301e27eb169ade5b8f244c95a6691585f7f107a8
7
+ data.tar.gz: f135af9db867879e88223646a8fda75ec3160bd176df3df69989fe85fa92263225c75334d13ac9346b64b4160914e09aa508d25d0cab97665473dea7fbd3a611
data/.gitignore CHANGED
@@ -11,3 +11,4 @@
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
13
  .DS_Store
14
+ .gem
data/README.md CHANGED
@@ -40,11 +40,17 @@ To use an API, instantiate the service. For example to use the Campaign API:
40
40
 
41
41
  ```ruby
42
42
  require 'naver/searchad/api/campaign/service'
43
+ require 'naver/searchad/api/auth'
43
44
 
44
45
  Campaign = Naver::Searchad::Api::Campaign # Alias the module
45
46
  campaign = Campaign::Service.new
46
47
  campaign.authorization = Naver::Searchad::Api::Auth.get_application_default # See Below Authorization
47
48
 
49
+ # Read all campaigns (with camelCase response)
50
+ campaign.list_campaigns(options: { decode_snake_case: false }) do |res, err|
51
+ puts res
52
+ end
53
+
48
54
  # Read campaigns by ids
49
55
  campaign.list_campaigns_by_ids(['campaign_id_1', 'campaign_id_2']) do |res, err|
50
56
  puts res
@@ -10,6 +10,11 @@ module Naver
10
10
  super('https://api.naver.com/', 'ncc/')
11
11
  end
12
12
 
13
+ def list_adgroups(options: nil, &block)
14
+ command = make_command(:get, 'adgroups', options)
15
+ execute_command(command, &block)
16
+ end
17
+
13
18
  def list_adgroups_by_ids(adgroup_ids, options: nil, &block)
14
19
  command = make_command(:get, 'adgroups', options)
15
20
  command.query['ids'] = adgroup_ids.join(',')
@@ -0,0 +1,21 @@
1
+ require_relative '../core/base_service'
2
+
3
+ module Naver
4
+ module Searchad
5
+ module Api
6
+ module BusinessChannel
7
+ class Service < Naver::Searchad::Api::Core::BaseService
8
+
9
+ def initialize
10
+ super('https://api.naver.com/', 'ncc/')
11
+ end
12
+
13
+ def list_channels(options: nil, &block)
14
+ command = make_command(:get, 'channels', options)
15
+ execute_command(command, &block)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -10,6 +10,11 @@ module Naver
10
10
  super('https://api.naver.com/', 'ncc/')
11
11
  end
12
12
 
13
+ def list_campaigns(options: nil, &block)
14
+ command = make_command(:get, 'campaigns', options)
15
+ execute_command(command, &block)
16
+ end
17
+
13
18
  def list_campaigns_by_ids(campaign_ids, options: nil, &block)
14
19
  command = make_command(:get, 'campaigns/', options)
15
20
  command.query['ids'] = campaign_ids.join(',')
@@ -18,6 +18,13 @@ module Naver
18
18
  }
19
19
 
20
20
  attr_accessor :request_object
21
+ attr_accessor :decode_snake_case
22
+
23
+ def initialize(method, url, body: nil, decode_snake_case: true)
24
+ super(method, url, body: body)
25
+
26
+ @decode_snake_case = decode_snake_case
27
+ end
21
28
 
22
29
  def prepare!
23
30
  if request_object
@@ -32,7 +39,7 @@ module Naver
32
39
  return nil unless content_type.start_with?(JSON_CONTENT_TYPE)
33
40
 
34
41
  decoded_response = JSON.parse(body)
35
- deep_snake_case_params!(decoded_response)
42
+ deep_snake_case_params!(decoded_response) if @decode_snake_case
36
43
  if decoded_response.kind_of?(Hash)
37
44
  OpenStruct.new(decoded_response)
38
45
  elsif decoded_response.kind_of?(Array)
@@ -4,6 +4,7 @@ require 'httpclient'
4
4
  require_relative '../options'
5
5
  require_relative '../version'
6
6
  require_relative 'api_command'
7
+ require_relative 'download_command'
7
8
  require_relative 'logging'
8
9
 
9
10
  module Naver
@@ -42,6 +43,15 @@ module Naver
42
43
  def make_command(method, path, options = {})
43
44
  template = Addressable::Template.new(url + base_path + path)
44
45
  command = ApiCommand.new(method, template)
46
+ command.decode_snake_case = options.fetch(:decode_snake_case, true) if options
47
+ command.options = request_options.merge(options)
48
+ apply_command_defaults(command)
49
+ command
50
+ end
51
+
52
+ def make_download_command(method, path, options = {})
53
+ template = Addressable::Template.new(url + base_path + path)
54
+ command = DownloadCommand.new(method, template)
45
55
  command.options = request_options.merge(options)
46
56
  apply_command_defaults(command)
47
57
  command
@@ -0,0 +1,79 @@
1
+ require_relative 'api_command'
2
+ require 'addressable/uri'
3
+
4
+ module Naver
5
+ module Searchad
6
+ module Api
7
+ module Core
8
+ class DownloadCommand < ApiCommand
9
+ OK_STATUSES = [200, 201, 206]
10
+
11
+ attr_accessor :download_dest
12
+
13
+ def prepare!
14
+ @state = :start
15
+ @download_url = nil
16
+ @offset = 0
17
+
18
+ if download_dest.is_a?(String)
19
+ @download_io = File.open(download_dest, 'wb')
20
+ @close_io_on_finish = true
21
+ else
22
+ @download_io = StringIO.new('', 'wb')
23
+ @close_io_on_finish = false
24
+ end
25
+
26
+ super
27
+ end
28
+
29
+ def release!
30
+ @download_io.close if @close_io_on_finish
31
+ end
32
+
33
+ def _execute(client)
34
+ request_header = {}
35
+ apply_request_options(request_header)
36
+ download_offset = nil
37
+
38
+ http_res = client.get(url.to_s,
39
+ query: nil,
40
+ header: request_header,
41
+ follow_redirect: true) do |res, chunk|
42
+ status = res.http_header.status_code.to_i
43
+ next unless OK_STATUSES.include?(status)
44
+
45
+ download_offset ||= (status == 206 ? @offset : 0)
46
+ download_offset += chunk.bytesize
47
+
48
+ if download_offset - chunk.bytesize == @offset
49
+ next_chunk = chunk
50
+ else
51
+ chunk_index = @offset - (download_offset - chunk.bytesize)
52
+ next_chunk = chunk.byteslice(chunk_index..-1)
53
+ next if next_chunk.nil?
54
+ end
55
+
56
+ @download_io.write(next_chunk)
57
+ @offset += next_chunk.bytesize
58
+ end
59
+
60
+ @download_io.flush
61
+
62
+ if @close_io_on_finish
63
+ result = nil
64
+ else
65
+ result = @download_io
66
+ end
67
+ check_status(http_res.status.to_i, http_res.header, http_res.body)
68
+ logger.debug("DownloadCommand: Success")
69
+ success(result)
70
+ rescue => e
71
+ @download_io.flush
72
+ logger.debug("DownloadCommand: Error - #{e.inspect}")
73
+ error(e)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -32,25 +32,20 @@ module Naver
32
32
  def execute(client, &block)
33
33
  prepare!
34
34
 
35
- logger.debug("Executing HTTP #{method} #{url}")
36
- request_header = {}
37
- apply_request_options(request_header)
38
-
39
- http_res = client.request(method.to_s.upcase,
40
- url.to_s,
41
- query: nil,
42
- body: body,
43
- header: request_header,
44
- follow_redirect: true)
45
-
46
- logger.debug("Returned status(#{http_res.status}) and #{http_res.inspect}")
47
- response = process_response(http_res.status.to_i, http_res.header, http_res.body)
35
+ _execute(client).tap do |result|
36
+ if block_given?
37
+ yield result, nil
38
+ end
39
+ end
48
40
 
49
- logger.debug("Success - #{response}")
50
- success(response, &block)
51
41
  rescue => e
52
- logger.debug("Error - #{e.inspect}")
53
- error(e, &block)
42
+ if block_given?
43
+ yield nil, e
44
+ else
45
+ raise e
46
+ end
47
+ ensure
48
+ release!
54
49
  end
55
50
 
56
51
  def prepare!
@@ -68,6 +63,31 @@ module Naver
68
63
  @body = '' unless body
69
64
  end
70
65
 
66
+ def release!
67
+ end
68
+
69
+ def _execute(client)
70
+ logger.debug("Executing HTTP #{method} #{url}")
71
+ request_header = {}
72
+ apply_request_options(request_header)
73
+
74
+ http_res = client.request(method.to_s.upcase,
75
+ url.to_s,
76
+ query: nil,
77
+ body: body,
78
+ header: request_header,
79
+ follow_redirect: true)
80
+
81
+ logger.debug("Returned status(#{http_res.status}) and #{http_res.inspect}")
82
+ response = process_response(http_res.status.to_i, http_res.header, http_res.body)
83
+
84
+ logger.debug("Success - #{response}")
85
+ success(response)
86
+ rescue => e
87
+ logger.debug("Error - #{e.inspect}")
88
+ error(e)
89
+ end
90
+
71
91
  def process_response(status, header, body)
72
92
  check_status(status, header, body)
73
93
  decode_response_body(header['Content-Type'].first, body)
@@ -0,0 +1,21 @@
1
+ require_relative '../core/base_service'
2
+
3
+ module Naver
4
+ module Searchad
5
+ module Api
6
+ module Label
7
+ class Service < Naver::Searchad::Api::Core::BaseService
8
+
9
+ def initialize
10
+ super('https://api.naver.com/', 'ncc/')
11
+ end
12
+
13
+ def list_labels(options: nil, &block)
14
+ command = make_command(:get, 'labels', options)
15
+ execute_command(command, &block)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require_relative '../core/base_service'
2
+
3
+ module Naver
4
+ module Searchad
5
+ module Api
6
+ module RelatedKeywordStat
7
+ class Service < Naver::Searchad::Api::Core::BaseService
8
+
9
+ def initialize
10
+ super('https://api.naver.com/', '')
11
+ end
12
+
13
+ def list_stats(options: nil, &block)
14
+ command = make_command(:get, 'keywordstool', options)
15
+ execute_command(command, &block)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,57 @@
1
+ require_relative '../core/base_service'
2
+ require 'addressable/uri'
3
+
4
+ module Naver
5
+ module Searchad
6
+ module Api
7
+ module StatReport
8
+ class Service < Naver::Searchad::Api::Core::BaseService
9
+
10
+ def initialize
11
+ super('https://api.naver.com/', '')
12
+ end
13
+
14
+ def download_report(download_url, file_path, options: {}, &block)
15
+ uri = Addressable::URI.parse(download_url)
16
+
17
+ command = make_download_command(:get, uri.path, options)
18
+ command.query['authtoken'] = uri.query_values['authtoken']
19
+ command.download_dest = file_path
20
+ execute_command(command, &block)
21
+ end
22
+
23
+ def get_stat_report(report_job_id, options: {}, &block)
24
+ command = make_command(:get, '/stat-reports/{report_job_id}', options)
25
+ command.params['report_job_id'] = report_job_id
26
+ execute_command(command, &block)
27
+ end
28
+
29
+ def list_stat_reports(options: {}, &block)
30
+ command = make_command(:get, '/stat-reports', options)
31
+ execute_command(command, &block)
32
+ end
33
+
34
+ def create_stat_report(type, date, options: {}, &block)
35
+ command = make_command(:post, '/stat-reports', options)
36
+ command.request_object = {
37
+ 'reportTp' => type,
38
+ 'statDt' => date
39
+ }
40
+ execute_command(command, &block)
41
+ end
42
+
43
+ def delete_stat_reports(options: {}, &block)
44
+ command = make_command(:delete, '/stat-reports', options)
45
+ execute_command(command, &block)
46
+ end
47
+
48
+ def delete_stat_report(report_job_id, options: {}, &block)
49
+ command = make_command(:delete, '/stat-reports/{report_job_id}', options)
50
+ command.params['report_job_id'] = report_job_id
51
+ execute_command(command, &block)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,40 @@
1
+ require_relative '../core/base_service'
2
+
3
+ module Naver
4
+ module Searchad
5
+ module Api
6
+ module Stat
7
+ class Service < Naver::Searchad::Api::Core::BaseService
8
+
9
+ def initialize
10
+ super('https://api.naver.com/', '')
11
+ end
12
+
13
+ def get_stat_by_id(id, fields, time_range, options: {}, &block)
14
+ command = make_command(:get, 'stats', options)
15
+ command.query['id'] = id
16
+ command.query['fields'] = fields.to_json
17
+ command.query['timeRange'] = time_range.to_json
18
+ command.query['datePreset'] = options[:date_preset] if options[:date_preset]
19
+ command.query['timeIncrement'] = options[:time_increment] if options[:time_increment]
20
+ command.query['breakdown'] = options[:breakdown] if options[:breakdown]
21
+
22
+ execute_command(command, &block)
23
+ end
24
+
25
+ def get_stat_by_ids(ids, fields, time_range, options: {}, &block)
26
+ command = make_command(:get, 'stats', options)
27
+ command.query['ids'] = ids
28
+ command.query['fields'] = fields.to_json
29
+ command.query['timeRange'] = time_range.to_json
30
+ command.query['datePreset'] = options[:date_preset] if options[:date_preset]
31
+ command.query['timeIncrement'] = options[:time_increment] if options[:time_increment]
32
+ command.query['breakdown'] = options[:breakdown] if options[:breakdown]
33
+
34
+ execute_command(command, &block)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,7 +1,7 @@
1
1
  module Naver
2
2
  module Searchad
3
3
  module Api
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.6'
5
5
 
6
6
  OS_VERSION = begin
7
7
  if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naver-searchad-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Min Kim
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-02 00:00:00.000000000 Z
11
+ date: 2017-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -136,13 +136,19 @@ files:
136
136
  - lib/naver/searchad/api/ad/service.rb
137
137
  - lib/naver/searchad/api/adgroup/service.rb
138
138
  - lib/naver/searchad/api/auth.rb
139
+ - lib/naver/searchad/api/business-channel/service.rb
139
140
  - lib/naver/searchad/api/campaign/service.rb
140
141
  - lib/naver/searchad/api/core/api_command.rb
141
142
  - lib/naver/searchad/api/core/base_service.rb
143
+ - lib/naver/searchad/api/core/download_command.rb
142
144
  - lib/naver/searchad/api/core/http_command.rb
143
145
  - lib/naver/searchad/api/core/logging.rb
144
146
  - lib/naver/searchad/api/errors.rb
147
+ - lib/naver/searchad/api/label/service.rb
145
148
  - lib/naver/searchad/api/options.rb
149
+ - lib/naver/searchad/api/related-keyword-stat/service.rb
150
+ - lib/naver/searchad/api/stat-report/service.rb
151
+ - lib/naver/searchad/api/stat/service.rb
146
152
  - lib/naver/searchad/api/version.rb
147
153
  - naver-searchad-api.gemspec
148
154
  homepage: https://github.com/forward3d/naver-searchad-api