sem4r 0.1.2 → 0.1.3
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.
- data/Gemfile +13 -2
- data/Gemfile.lock +13 -7
- data/README.rdoc +44 -7
- data/Rakefile +2 -122
- data/VERSION.yml +1 -1
- data/bin/sem +1 -0
- data/examples_sem4r/01_get_account.rb +1 -0
- data/examples_sem4r/02_get_info.rb +1 -0
- data/examples_sem4r/03_list_ad.rb +1 -0
- data/examples_sem4r/04_list_keywords.rb +1 -0
- data/examples_sem4r/05_request_report.rb +25 -14
- data/examples_sem4r/{05_request_report_2010.rb → 06_request_report_definition.rb} +2 -4
- data/examples_sem4r/{06_create_campaigns.rb → 07_create_campaigns.rb} +1 -0
- data/examples_sem4r/{07_create_campaigns_block.rb → 08_create_campaigns_block.rb} +1 -0
- data/examples_sem4r/{07_create_campaigns_simple.rb → 09_create_campaigns_simple.rb} +1 -0
- data/examples_sem4r/{08_ad_params.rb → 10_ad_params.rb} +1 -0
- data/examples_sem4r/{09_targeting_idea.rb → 11_targeting_idea.rb} +1 -0
- data/examples_sem4r/{10_get_location.rb → 12_get_location.rb} +1 -0
- data/examples_sem4r/{11_submit_bulk_job.rb → 13_submit_bulk_job.rb} +1 -1
- data/examples_sem4r/{12_list_bulk_job.rb → 14_list_bulk_job.rb} +3 -0
- data/examples_sem4r/30_prune_empty_adgroup.rb +1 -0
- data/examples_sem4r/31_empty_accounts.rb +1 -0
- data/examples_sem4r/example_helper.rb +2 -1
- data/lib/sem4r/ad_group/ad_group.rb +22 -7
- data/lib/sem4r/ad_group/ad_group_bids.rb +1 -1
- data/lib/sem4r/ad_group/ad_group_service.rb +12 -5
- data/lib/sem4r/ad_group_ad/ad_group_ad.rb +1 -0
- data/lib/sem4r/ad_group_ad/ad_group_ad_operations.rb +1 -0
- data/lib/sem4r/ad_group_ad/ad_group_ad_service.rb +14 -9
- data/lib/sem4r/ad_group_ad/ad_group_mobile_ad.rb +16 -25
- data/lib/sem4r/ad_group_ad/ad_group_text_ad.rb +18 -14
- data/lib/sem4r/ad_group_criterion/ad_group_criterion.rb +27 -19
- data/lib/sem4r/ad_group_criterion/ad_group_criterion_bids.rb +2 -1
- data/lib/sem4r/ad_group_criterion/ad_group_criterion_operations.rb +1 -0
- data/lib/sem4r/ad_group_criterion/ad_group_criterion_service.rb +9 -4
- data/lib/sem4r/ad_group_criterion/criterion.rb +1 -0
- data/lib/sem4r/ad_group_criterion/criterion_keyword.rb +1 -0
- data/lib/sem4r/ad_group_criterion/criterion_placement.rb +1 -0
- data/lib/sem4r/ad_param/ad_param.rb +24 -8
- data/lib/sem4r/ad_param/ad_param_service.rb +8 -5
- data/lib/sem4r/adwords.rb +232 -76
- data/lib/sem4r/base.rb +1 -1
- data/lib/sem4r/bulk_mutate_job/bulk_mutate_job.rb +41 -35
- data/lib/sem4r/bulk_mutate_job/bulk_mutate_job_account_extension.rb +49 -15
- data/lib/sem4r/bulk_mutate_job/bulk_mutate_job_selector.rb +29 -20
- data/lib/sem4r/bulk_mutate_job/bulk_mutate_job_service.rb +13 -14
- data/lib/sem4r/bulk_mutate_job/job_operations.rb +1 -1
- data/lib/sem4r/campaign/campaign.rb +1 -0
- data/lib/sem4r/campaign/campaign_account_extension.rb +2 -1
- data/lib/sem4r/campaign/campaign_service.rb +9 -5
- data/lib/sem4r/credentials.rb +11 -5
- data/lib/sem4r/geo_location/address.rb +56 -0
- data/lib/sem4r/geo_location/geo_location_account_extension.rb +27 -6
- data/lib/sem4r/geo_location/geo_location_selector.rb +57 -0
- data/lib/sem4r/geo_location/geo_location_service.rb +9 -16
- data/lib/sem4r/info/info_account_extension.rb +3 -2
- data/lib/sem4r/info/info_selector.rb +2 -1
- data/lib/sem4r/info/info_service.rb +10 -3
- data/lib/sem4r/operation.rb +39 -20
- data/lib/sem4r/report_definition/report_definition.rb +37 -19
- data/lib/sem4r/report_definition/report_definition_account_extension.rb +66 -16
- data/lib/sem4r/report_definition/report_definition_operation.rb +2 -2
- data/lib/sem4r/report_definition/report_definition_selector.rb +1 -1
- data/lib/sem4r/report_definition/report_definition_service.rb +11 -7
- data/lib/sem4r/report_definition/report_field.rb +3 -2
- data/lib/sem4r/sem4r_templates.rb +77 -0
- data/lib/sem4r/{services/service.rb → service.rb} +23 -23
- data/lib/sem4r/targeting_idea/targeting_idea.rb +4 -4
- data/lib/sem4r/targeting_idea/targeting_idea_account_extension.rb +1 -1
- data/lib/sem4r/targeting_idea/targeting_idea_selector.rb +6 -6
- data/lib/sem4r/targeting_idea/targeting_idea_service.rb +7 -5
- data/lib/sem4r/v13_account/account_account_extension.rb +7 -10
- data/lib/sem4r/v13_account/account_service.rb +10 -4
- data/lib/sem4r/v13_account/billing_address.rb +2 -2
- data/lib/sem4r/v13_report/report_service.rb +11 -9
- data/lib/sem4r.rb +46 -31
- data/lib/{sem4r/cli → sem4r_cli}/cli_helpers.rb +3 -2
- data/lib/{sem4r/cli/cli_command.rb → sem4r_cli/cli_mini_framework.rb} +77 -32
- data/lib/{sem4r/cli/cli_common_args.rb → sem4r_cli/cli_sem.rb} +177 -157
- data/lib/{sem4r/cli/commands/cli_list_ads.rb → sem4r_cli/commands/cli_ads.rb} +7 -9
- data/lib/sem4r_cli/commands/cli_campaign.rb +69 -0
- data/lib/{sem4r/cli/commands/cli_list_client.rb → sem4r_cli/commands/cli_clients.rb} +2 -2
- data/lib/{sem4r/cli → sem4r_cli}/commands/cli_ideas.rb +35 -27
- data/lib/{sem4r/cli → sem4r_cli}/commands/cli_info.rb +3 -4
- data/lib/{sem4r/cli/commands/cli_repdef.rb → sem4r_cli/commands/cli_job.rb} +57 -55
- data/lib/{sem4r/cli/commands/cli_list_keywords.rb → sem4r_cli/commands/cli_keywords.rb} +14 -13
- data/lib/sem4r_cli/commands/cli_profile.rb +138 -0
- data/lib/{sem4r/cli/commands/cli_report.rb → sem4r_cli/commands/cli_repdef.rb} +67 -20
- data/lib/{sem4r/cli/commands/cli_request_report.rb → sem4r_cli/commands/cli_report.rb} +82 -19
- data/lib/sem4r_cli.rb +5 -7
- data/lib/sem4r_soap/http_connector.rb +206 -0
- data/lib/{soap_helpers → sem4r_soap}/soap_attributes.rb +8 -3
- data/lib/{sem4r/services → sem4r_soap}/soap_dumper.rb +36 -20
- data/lib/{sem4r/services → sem4r_soap}/soap_error.rb +5 -2
- data/lib/sem4r_soap/soap_response.rb +75 -0
- data/lib/sem4r_soap/soap_service.rb +137 -0
- data/lib/{sem4r/cli/cli_sem.rb → sem4r_soap/soap_service_v13.rb} +27 -28
- data/lib/sem4r_soap/soap_service_v2010.rb +80 -0
- data/lib/sem4r_soap.rb +17 -0
- data/sem4r.gemspec +93 -58
- data/spec/build_fixtures.rb +49 -42
- data/spec/fixtures/password.example.yml +3 -0
- data/spec/fixtures/sem4r.example.yml +6 -0
- data/spec/fixtures/{services/error.xml → soap_error.xml} +0 -0
- data/spec/fixtures/soap_error2.xml +29 -0
- data/spec/helpers/dump_interceptor.rb +90 -0
- data/spec/helpers/fixtures_bulk_mutate_job.rb +48 -0
- data/spec/helpers/fixtures_geo_location.rb +45 -0
- data/{lib/sem4r/campaign_criterion/campaign_criterion_service.rb → spec/helpers/fixtures_info.rb} +10 -6
- data/spec/helpers/fixtures_report_definition.rb +65 -0
- data/spec/{rspec_hash.rb → helpers/rspec_hash.rb} +1 -0
- data/spec/{rspec_matchers.rb → helpers/rspec_matchers.rb} +46 -19
- data/spec/{rspec_sem4r_helper.rb → helpers/rspec_sem4r_helper.rb} +19 -16
- data/spec/{sem4r_stubs.rb → helpers/sem4r_stubs.rb} +5 -4
- data/spec/rspec_helper.rb +4 -4
- data/spec/sem4r/account_spec.rb +2 -3
- data/spec/sem4r/ad_group/ad_group_service_spec.rb +1 -1
- data/spec/sem4r/ad_group/ad_group_spec.rb +1 -1
- data/spec/sem4r/ad_group_ad/ad_group_ad_operation_spec.rb +3 -3
- data/spec/sem4r/adwords_spec.rb +62 -39
- data/{lib/sem4r/cli/commands/cli_list_campaign.rb → spec/sem4r/bulk_mutate_job/bulk_mutate_job_selector_spec.rb} +14 -11
- data/spec/sem4r/bulk_mutate_job/bulk_mutate_job_service_spec.rb +2 -3
- data/spec/sem4r/bulk_mutate_job/bulk_mutate_job_spec.rb +23 -17
- data/spec/sem4r/bulk_mutate_job/fixtures/get-list_job-req.xml +35 -0
- data/spec/sem4r/bulk_mutate_job/fixtures/get-list_job-res.xml +417 -0
- data/spec/sem4r/bulk_mutate_job/fixtures/mutate-add_job-req.xml +106 -0
- data/spec/sem4r/bulk_mutate_job/fixtures/mutate-add_job-res.xml +48 -0
- data/spec/sem4r/bulk_mutate_job/job_operation_spec.rb +5 -7
- data/spec/sem4r/campaign/campaign_service_spec.rb +2 -2
- data/spec/sem4r/credentials_spec.rb +10 -8
- data/spec/sem4r/geo_location/address_spec.rb +62 -0
- data/spec/sem4r/geo_location/fixtures/get-req.xml +42 -0
- data/spec/sem4r/geo_location/fixtures/get-res.xml +60 -0
- data/spec/sem4r/info/fixtures/get-req.xml +34 -0
- data/spec/sem4r/info/fixtures/get-res.xml +27 -0
- data/spec/sem4r/nokogiri_parsing_spec.rb +1 -0
- data/spec/sem4r/operation_spec.rb +72 -0
- data/spec/sem4r/report_definition/fixtures/get-list-repdef-req.xml +22 -0
- data/spec/sem4r/report_definition/fixtures/get-list-repdef-res.xml +73 -0
- data/spec/sem4r/report_definition/fixtures/getReportFields-req.xml +24 -0
- data/spec/sem4r/report_definition/fixtures/getReportFields-res.xml +1199 -0
- data/spec/sem4r/report_definition/fixtures/mutate-add-report-req.xml +63 -0
- data/spec/sem4r/report_definition/fixtures/mutate-add-report-res.xml +68 -0
- data/spec/sem4r/report_definition/report_definition_service_spec.rb +5 -5
- data/spec/sem4r/report_definition/report_definition_spec.rb +28 -43
- data/{lib/sem4r/cli/commands/cli_list_report.rb → spec/sem4r/report_definition/report_field_spec.rb} +12 -10
- data/spec/sem4r/rexml_parsing_spec.rb +1 -0
- data/spec/sem4r/{services/service_spec.rb → service_spec.rb} +1 -1
- data/spec/sem4r/targeting_idea/targeting_idea_selector_spec.rb +1 -1
- data/spec/sem4r/targeting_idea/targeting_idea_service_spec.rb +1 -1
- data/spec/sem4r/targeting_idea/targeting_idea_spec.rb +1 -1
- data/spec/sem4r/v13_account/account_service_spec.rb +2 -2
- data/spec/sem4r/v13_report/report_service_spec.rb +2 -2
- data/spec/{sem4r/cli → sem4r_cli}/cli_spec.rb +22 -27
- data/spec/{soap_helpers → sem4r_soap}/soap_attributes_spec.rb +3 -3
- data/spec/{sem4r/services/soap_message_v13_spec.rb → sem4r_soap/soap_response_spec.rb} +10 -15
- data/spec/{sem4r/services/soap_call_spec.rb → sem4r_soap/soap_service_spec.rb} +35 -54
- data/tasks/jeweler.rake +66 -0
- data/tasks/rspec.rake +21 -0
- data/tasks/sem4r.rake +25 -0
- data/tasks/yard.rake +31 -0
- metadata +173 -76
- data/lib/sem4r/ad_extension_override/ad_extension_override_service.rb +0 -30
- data/lib/sem4r/api_counters.rb +0 -8
- data/lib/sem4r/campaign_target/campaign_target_service.rb +0 -30
- data/lib/sem4r/cli/commands/cli_download_report.rb +0 -82
- data/lib/sem4r/services/http_connector.rb +0 -93
- data/lib/sem4r/services/soap_call.rb +0 -122
- data/lib/sem4r/services/soap_connector.rb +0 -139
- data/lib/sem4r/services/soap_message_v13.rb +0 -135
- data/lib/sem4r/services/soap_message_v2010.rb +0 -184
- data/lib/sem4r/v13_traffic_estimator/traffic_estimator_service.rb +0 -30
- data/spec/aggregates_rspec_helper.rb +0 -59
- data/spec/sem4r/report_definition/fixtures/mutate_add-req.xml +0 -24
- data/spec/sem4r/report_definition/fixtures/mutate_add-req_orig.xml +0 -45
data/lib/sem4r.rb
CHANGED
|
@@ -22,25 +22,40 @@
|
|
|
22
22
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
23
|
# -------------------------------------------------------------------
|
|
24
24
|
|
|
25
|
+
#
|
|
26
|
+
# stdlib
|
|
27
|
+
#
|
|
25
28
|
require 'yaml'
|
|
26
29
|
require 'pathname'
|
|
30
|
+
require 'ostruct'
|
|
31
|
+
require 'uri'
|
|
32
|
+
require 'pp'
|
|
33
|
+
require 'logger'
|
|
34
|
+
require 'fileutils'
|
|
35
|
+
# require 'rexml/document'
|
|
27
36
|
|
|
37
|
+
#
|
|
28
38
|
# use bundler
|
|
39
|
+
#
|
|
29
40
|
require "rubygems"
|
|
30
|
-
|
|
41
|
+
|
|
42
|
+
begin
|
|
43
|
+
require "bundler/setup"
|
|
44
|
+
rescue LoadError
|
|
45
|
+
end
|
|
31
46
|
|
|
32
47
|
require 'builder'
|
|
33
|
-
# require 'rexml/document'
|
|
34
48
|
require 'nokogiri'
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
require '
|
|
49
|
+
|
|
50
|
+
if RUBY_PLATFORM =~ /java/
|
|
51
|
+
gem 'jruby-openssl'
|
|
52
|
+
require 'openssl'
|
|
53
|
+
end
|
|
39
54
|
|
|
40
55
|
module Sem4r #:nodoc:
|
|
41
56
|
def self.version
|
|
42
|
-
cwd
|
|
43
|
-
yaml
|
|
57
|
+
cwd = Pathname(__FILE__).dirname.expand_path.to_s
|
|
58
|
+
yaml = YAML.load_file(cwd + '/../VERSION.yml')
|
|
44
59
|
major = (yaml['major'] || yaml[:major]).to_i
|
|
45
60
|
minor = (yaml['minor'] || yaml[:minor]).to_i
|
|
46
61
|
patch = (yaml['patch'] || yaml[:patch]).to_i
|
|
@@ -56,32 +71,19 @@ require 'sem4r/extensions.rb'
|
|
|
56
71
|
require 'sem4r/adwords'
|
|
57
72
|
require 'sem4r/credentials'
|
|
58
73
|
require 'sem4r/sem4r_error'
|
|
59
|
-
require '
|
|
74
|
+
require 'sem4r_soap/soap_attributes'
|
|
60
75
|
|
|
61
76
|
require 'sem4r/base'
|
|
62
77
|
require 'sem4r/account'
|
|
63
78
|
|
|
64
79
|
require 'sem4r/bulk_mutate_job/bulk_mutate_job_account_extension'
|
|
65
80
|
require 'sem4r/campaign/campaign_account_extension'
|
|
66
|
-
require 'sem4r/geo_location/geo_location_account_extension'
|
|
67
81
|
require 'sem4r/info/info_account_extension'
|
|
68
82
|
require 'sem4r/report_definition/report_definition_account_extension'
|
|
69
83
|
require 'sem4r/targeting_idea/targeting_idea_account_extension'
|
|
70
84
|
|
|
71
|
-
require '
|
|
72
|
-
require 'sem4r/
|
|
73
|
-
|
|
74
|
-
#
|
|
75
|
-
# soap connection layer
|
|
76
|
-
#
|
|
77
|
-
require 'sem4r/services/soap_error'
|
|
78
|
-
require 'sem4r/services/http_connector'
|
|
79
|
-
require 'sem4r/services/soap_dumper'
|
|
80
|
-
require 'sem4r/services/soap_connector'
|
|
81
|
-
require 'sem4r/services/soap_message_v13'
|
|
82
|
-
require 'sem4r/services/soap_message_v2010'
|
|
83
|
-
require 'sem4r/services/soap_call'
|
|
84
|
-
require 'sem4r/services/service'
|
|
85
|
+
require 'sem4r_soap'
|
|
86
|
+
require 'sem4r/service'
|
|
85
87
|
|
|
86
88
|
#
|
|
87
89
|
# common
|
|
@@ -134,7 +136,22 @@ require 'sem4r/campaign/campaign'
|
|
|
134
136
|
#
|
|
135
137
|
# info
|
|
136
138
|
#
|
|
137
|
-
require'sem4r/info/info_selector'
|
|
139
|
+
require 'sem4r/info/info_selector'
|
|
140
|
+
|
|
141
|
+
#
|
|
142
|
+
# geo_location
|
|
143
|
+
#
|
|
144
|
+
require 'sem4r/geo_location/geo_location_account_extension'
|
|
145
|
+
require 'sem4r/geo_location/geo_location_selector'
|
|
146
|
+
require 'sem4r/geo_location/address'
|
|
147
|
+
|
|
148
|
+
#
|
|
149
|
+
# report_definition
|
|
150
|
+
#
|
|
151
|
+
require 'sem4r/report_definition/report_definition'
|
|
152
|
+
require 'sem4r/report_definition/report_field'
|
|
153
|
+
require 'sem4r/report_definition/report_definition_selector'
|
|
154
|
+
require 'sem4r/report_definition/report_definition_operation'
|
|
138
155
|
|
|
139
156
|
#
|
|
140
157
|
# targeting_idea
|
|
@@ -146,18 +163,16 @@ require 'sem4r/targeting_idea/targeting_idea_selector'
|
|
|
146
163
|
# v13_account
|
|
147
164
|
#
|
|
148
165
|
require 'sem4r/v13_account/billing_address'
|
|
166
|
+
require 'sem4r/v13_account/account_account_extension'
|
|
149
167
|
|
|
150
168
|
#
|
|
151
169
|
# v13_report
|
|
152
170
|
#
|
|
153
171
|
require 'sem4r/v13_report/report'
|
|
154
172
|
require 'sem4r/v13_report/report_job'
|
|
155
|
-
|
|
173
|
+
require 'sem4r/v13_report/report_account_extension'
|
|
156
174
|
|
|
157
175
|
#
|
|
158
|
-
# report_definition
|
|
159
176
|
#
|
|
160
|
-
|
|
161
|
-
require 'sem4r/
|
|
162
|
-
require 'sem4r/report_definition/report_definition_selector'
|
|
163
|
-
require 'sem4r/report_definition/report_definition_operation'
|
|
177
|
+
#
|
|
178
|
+
require 'sem4r/sem4r_templates'
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
|
-
module
|
|
2
|
+
module Sem4rCli
|
|
3
|
+
|
|
3
4
|
# From http://gist.github.com/72234
|
|
4
5
|
#
|
|
5
6
|
# mysql-style output for an array of ActiveRecord objects
|
|
@@ -21,7 +22,7 @@ module Sem4r
|
|
|
21
22
|
# +------+-----------+--------------------------------+
|
|
22
23
|
# 5 rows in set
|
|
23
24
|
#
|
|
24
|
-
def self.report(items, *fields)
|
|
25
|
+
def self.report(items, *fields) #:nodoc: all
|
|
25
26
|
# find max length for each field; start with the field names themselves
|
|
26
27
|
max_len = Hash[*fields.map {|f| [f, f.to_s.length]}.flatten]
|
|
27
28
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
|
-
#
|
|
2
|
+
# -------------------------------------------------------------------
|
|
3
3
|
# Copyright (c) 2009-2010 Sem4r sem4ruby@gmail.com
|
|
4
|
-
#
|
|
4
|
+
#
|
|
5
5
|
# Permission is hereby granted, free of charge, to any person obtaining
|
|
6
6
|
# a copy of this software and associated documentation files (the
|
|
7
7
|
# "Software"), to deal in the Software without restriction, including
|
|
@@ -9,10 +9,10 @@
|
|
|
9
9
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
10
|
# permit persons to whom the Software is furnished to do so, subject to
|
|
11
11
|
# the following conditions:
|
|
12
|
-
#
|
|
12
|
+
#
|
|
13
13
|
# The above copyright notice and this permission notice shall be
|
|
14
14
|
# included in all copies or substantial portions of the Software.
|
|
15
|
-
#
|
|
15
|
+
#
|
|
16
16
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
17
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
18
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
@@ -20,20 +20,50 @@
|
|
|
20
20
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
21
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
22
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
-
#
|
|
24
|
-
# -------------------------------------------------------------------------
|
|
23
|
+
# -------------------------------------------------------------------
|
|
25
24
|
|
|
26
|
-
module
|
|
25
|
+
module Sem4rCli
|
|
27
26
|
|
|
27
|
+
class Cli
|
|
28
|
+
def self.run
|
|
29
|
+
cli = self.new
|
|
30
|
+
cli.parse_and_run(ARGV)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
#
|
|
35
|
+
# Base class for generic command
|
|
36
|
+
# @example
|
|
37
|
+
# class MyCommand < CliCommand
|
|
38
|
+
# def self.command; "mycommand"; end
|
|
39
|
+
# def self.description; "mycomand description"; end
|
|
40
|
+
#
|
|
41
|
+
# def initialize(common_args)
|
|
42
|
+
# ...
|
|
43
|
+
# end
|
|
44
|
+
#
|
|
45
|
+
# def parse_and_run(args)
|
|
46
|
+
# ...
|
|
47
|
+
# if run_successful then true else false end
|
|
48
|
+
# end
|
|
49
|
+
# end
|
|
50
|
+
#
|
|
28
51
|
class CliCommand
|
|
29
52
|
|
|
30
53
|
class << self
|
|
31
54
|
|
|
55
|
+
#
|
|
56
|
+
# @private
|
|
57
|
+
# Add class to the list of command.
|
|
58
|
+
#
|
|
32
59
|
def inherited(base)
|
|
33
60
|
@command_list ||= []
|
|
34
61
|
@command_list << base
|
|
35
62
|
end
|
|
36
63
|
|
|
64
|
+
#
|
|
65
|
+
# All definited commands
|
|
66
|
+
#
|
|
37
67
|
def commands
|
|
38
68
|
return @commands if @commands
|
|
39
69
|
@commands = {}
|
|
@@ -41,19 +71,38 @@ module Sem4r
|
|
|
41
71
|
@commands
|
|
42
72
|
end
|
|
43
73
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
74
|
+
#
|
|
75
|
+
# Finds the name of command into args.
|
|
76
|
+
# If ARGV is <opt1> <opt2> command_name <rest of cli>
|
|
77
|
+
# @return[ ar1, name, ar2 ] where
|
|
78
|
+
# ar1 is = <opt1> <opt2>
|
|
79
|
+
# name is command_name
|
|
80
|
+
# ar2 is = <rest of cli>
|
|
81
|
+
#
|
|
82
|
+
# def split_args(argv)
|
|
83
|
+
# idx = argv.find_index { |e| commands.key?(e) }
|
|
84
|
+
# if idx
|
|
85
|
+
# common_args = argv[0, idx]
|
|
86
|
+
# command = argv[idx]
|
|
87
|
+
# command_args = argv[idx+1..-1]
|
|
88
|
+
# [common_args, command, command_args]
|
|
89
|
+
# else
|
|
90
|
+
# [argv, nil, nil]
|
|
91
|
+
# end
|
|
92
|
+
# end
|
|
55
93
|
|
|
56
|
-
|
|
94
|
+
#
|
|
95
|
+
# Helper method to define new command
|
|
96
|
+
#
|
|
97
|
+
# @example
|
|
98
|
+
# CliMyCommand = CliCommand.define_command("mycommand", "my command description") do |cli|
|
|
99
|
+
# ...
|
|
100
|
+
# <might access to cli methods as common options>
|
|
101
|
+
# ...
|
|
102
|
+
# if run_successful then true else false end
|
|
103
|
+
# end
|
|
104
|
+
#
|
|
105
|
+
def define_command(command_name, description, &block)
|
|
57
106
|
|
|
58
107
|
unless block_given?
|
|
59
108
|
raise "define_command: missing block"
|
|
@@ -65,7 +114,7 @@ module Sem4r
|
|
|
65
114
|
end
|
|
66
115
|
|
|
67
116
|
def opt_parser(options)
|
|
68
|
-
opt_parser
|
|
117
|
+
opt_parser = OptionParser.new
|
|
69
118
|
opt_parser.banner= "#{self.class.description}"
|
|
70
119
|
opt_parser.on("-h", "--help", "show this message") do
|
|
71
120
|
puts opt_parser
|
|
@@ -75,28 +124,24 @@ module Sem4r
|
|
|
75
124
|
|
|
76
125
|
define_method("parse_and_run") do |argv|
|
|
77
126
|
options = OpenStruct.new
|
|
78
|
-
opt_parser(options).parse(
|
|
127
|
+
opt_parser(options).parse(argv)
|
|
79
128
|
return false if options.exit
|
|
80
|
-
|
|
81
|
-
unless account
|
|
82
|
-
puts "select an account!"
|
|
83
|
-
false
|
|
84
|
-
else
|
|
85
|
-
return block.call(account)
|
|
86
|
-
end
|
|
129
|
+
block.call(@common_args)
|
|
87
130
|
end
|
|
88
131
|
end
|
|
89
132
|
|
|
90
|
-
s
|
|
133
|
+
s = class << cls;
|
|
134
|
+
self;
|
|
135
|
+
end
|
|
91
136
|
s.class_eval do
|
|
92
137
|
define_method("command") { command_name }
|
|
93
|
-
define_method("description") {
|
|
138
|
+
define_method("description") { description }
|
|
94
139
|
end
|
|
95
140
|
cls
|
|
96
141
|
end
|
|
97
142
|
|
|
98
|
-
end
|
|
143
|
+
end # class << self
|
|
99
144
|
|
|
100
|
-
end
|
|
145
|
+
end # class CliCommand
|
|
101
146
|
|
|
102
147
|
end
|