docomoru 0.0.3 → 0.1.0

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: 1feaff8bd63031fba1d9f25bf7393dbbc18c601c
4
- data.tar.gz: cf783fdc98fc102b7841e203434493ca18c78bf1
3
+ metadata.gz: d04d819418e0183a2bde1d8d85ee096a91f6c1d6
4
+ data.tar.gz: f49da17d964b80a229105e960ed4e319729648c0
5
5
  SHA512:
6
- metadata.gz: 9166219c8b8c0eed5524d7f3bde099471f451b53e94a2e4b1dc7ab353293f75d8efda3970e44bb7c7d21ab18937834f0d0362127387e074b1eaaeedabbd59a12
7
- data.tar.gz: 0bd018033a62327c95a7b7122bd9d4515f6a91f295ac3441bbcbe4e4b75423bcf83e4c40d511e06856feeab09bb8c3ffc1d10cdba4be28cdce9895dae47abef1
6
+ metadata.gz: f8ec6d380bbd1cebe49f39366f4ff3035dda5ab3ae1ba0211e4e0258b02e24e57da31dee75aa2be7e80b6b4d1fb0d0546ee8f5d3dcec5eadb2d3c7de2bf6ee0c
7
+ data.tar.gz: 270de75d628caa1f2f35006c3da903b21370e6917e21b41eb51ad8d4ef9f9b28732ce41dd7201b9248fcbe33d8b08a39a9889371398bec6de77fe32e33bf111d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.1.0
2
+ - Add `docomoru` executable for CLI use
3
+
1
4
  ## 0.0.3
2
5
  - Make it compatible with Ruby 2.0
3
6
 
data/README.md CHANGED
@@ -1,8 +1,16 @@
1
1
  # Docomoru
2
2
  Client library for DoCoMo API written in Ruby.
3
3
 
4
- ## Usage
5
- Currenty docomoru is supporting [Dialogue API](https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_docs_id=3).
4
+ ## Install
5
+ Note: requires Ruby 2.0.0 or higher.
6
+
7
+ ```sh
8
+ gem install docomoru
9
+ ```
10
+
11
+ ## Library
12
+ Currenty docomoru is supporting [Dialogue API](https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_docs_id=3)
13
+ (You need DoCoMo API Key).
6
14
 
7
15
  ```rb
8
16
  client = Docomoru::Client.new(api_key: ENV["DOCOMO_API_KEY"])
@@ -23,3 +31,23 @@ response.body #=> {
23
31
  "context" => "dXIgT9u1_XKfO1QbaVkAGQ"
24
32
  }
25
33
  ```
34
+
35
+ ## CLI
36
+ `docomoru` executable is bundled to call use Docomoru from CLI.
37
+
38
+ ```
39
+ $ docomoru <method> <arguments> [options]
40
+ | | |
41
+ | | `-- -a, --api-key (or use DOCOMO_API_KEY env)
42
+ | | -h, --help
43
+ | | --header
44
+ | | --no-body
45
+ | |
46
+ | `------------------------------ required arguments for the method
47
+ |
48
+ `----------------------------------------- method name
49
+
50
+ $ docomoru create_dialogue Gemになってみた感想はどうですか
51
+ ```
52
+
53
+ ![](images/cli.png)
data/bin/docomoru ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
3
+ require "docomoru"
4
+
5
+ builder = Docomoru::CommandBuilder.new(ARGV)
6
+ command = builder.call
7
+ command.call
data/docomoru.gemspec CHANGED
@@ -19,6 +19,9 @@ Gem::Specification.new do |spec|
19
19
  spec.add_dependency "activesupport"
20
20
  spec.add_dependency "faraday"
21
21
  spec.add_dependency "faraday_middleware"
22
+ spec.add_dependency "json"
23
+ spec.add_dependency "rack"
24
+ spec.add_dependency "slop"
22
25
  spec.add_development_dependency "bundler", "~> 1.7"
23
26
  spec.add_development_dependency "pry"
24
27
  spec.add_development_dependency "rake", "~> 10.0"
data/images/cli.png ADDED
Binary file
@@ -0,0 +1,131 @@
1
+ require "json"
2
+ require "slop"
3
+
4
+ module Docomoru
5
+ class Arguments
6
+ def initialize(argv)
7
+ @argv = argv
8
+ end
9
+
10
+ def api_key
11
+ slop_options["api-key"] || ENV["DOCOMO_API_KEY"]
12
+ end
13
+
14
+ def arguments
15
+ parsed_argv_data[:arguments]
16
+ end
17
+
18
+ def error_message
19
+ slop_options.to_s
20
+ end
21
+
22
+ def headers
23
+ parsed_argv_data[:headers]
24
+ end
25
+
26
+ def method_name
27
+ ARGV[0]
28
+ end
29
+
30
+ def params
31
+ params_from_stdin.merge(parsed_argv_data[:params])
32
+ end
33
+
34
+ def show_body
35
+ !slop_options["no-body"]
36
+ end
37
+
38
+ def show_header
39
+ slop_options["header"]
40
+ end
41
+
42
+ def valid?
43
+ has_valid_slop_options? && has_valid_method_name? && has_valid_arguments? &&
44
+ !has_invalid_json_input? && has_api_key?
45
+ end
46
+
47
+ private
48
+
49
+ def has_api_key?
50
+ !!api_key
51
+ end
52
+
53
+ def has_input_from_stdin?
54
+ has_pipe_input? || has_redirect_input?
55
+ end
56
+
57
+ def has_invalid_json_input?
58
+ params_from_stdin
59
+ false
60
+ rescue JSON::ParserError
61
+ true
62
+ end
63
+
64
+ def has_pipe_input?
65
+ File.pipe?(STDIN)
66
+ end
67
+
68
+ def has_redirect_input?
69
+ File.select([STDIN], [], [], 0) != nil
70
+ end
71
+
72
+ def has_valid_arguments?
73
+ -(Client.instance_method(method_name).arity) - 1 == arguments.length
74
+ end
75
+
76
+ def has_valid_method_name?
77
+ !method_name.nil? && Client.instance_methods.include?(method_name.to_sym)
78
+ end
79
+
80
+ def has_valid_slop_options?
81
+ !slop_options["help"]
82
+ rescue
83
+ false
84
+ end
85
+
86
+ def params_from_stdin
87
+ @params_from_stdin ||= begin
88
+ if has_input_from_stdin?
89
+ JSON.parse(STDIN.read)
90
+ else
91
+ {}
92
+ end
93
+ end
94
+ end
95
+
96
+ def parsed_argv_data
97
+ @parsed_argv_data ||= begin
98
+ params = {}
99
+ headers = {}
100
+ arguments = []
101
+ ARGV[1..-1].each do |section|
102
+ case
103
+ when /(?<key>.+):(?<value>[^=]+)/ =~ section
104
+ headers[key] = value
105
+ when /(?<key>.+):=(?<value>.+)/ =~ section
106
+ params[key] = JSON.parse(%<{"key":#{value}}>)["key"]
107
+ when /(?<key>.+)=(?<value>.+)/ =~ section
108
+ params[key] = value
109
+ else
110
+ arguments << section
111
+ end
112
+ end
113
+ {
114
+ arguments: arguments,
115
+ headers: headers,
116
+ params: params,
117
+ }
118
+ end
119
+ end
120
+
121
+ def slop_options
122
+ @slop_options ||= Slop.parse!(@argv) do
123
+ banner "Usage: docomoru <method> [arguments] [headers|params] [options]"
124
+ on "a", "api-key=", "Pass API Key or use DOCOMO_API_KEY instead"
125
+ on "h", "help", "Display help message"
126
+ on "header", "Show response header"
127
+ on "no-body", "Hide response body"
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,25 @@
1
+ require "docomoru/arguments"
2
+ require "docomoru/commands/error"
3
+ require "docomoru/commands/request"
4
+
5
+ module Docomoru
6
+ class CommandBuilder
7
+ def initialize(argv)
8
+ @argv = argv
9
+ end
10
+
11
+ def call
12
+ if arguments.valid?
13
+ Commands::Request.new(arguments)
14
+ else
15
+ Commands::Error.new(arguments)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def arguments
22
+ @arguments ||= Arguments.new(@argv)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module Docomoru
2
+ module Commands
3
+ class Base
4
+ def initialize(arguments)
5
+ @arguments = arguments
6
+ end
7
+
8
+ def call
9
+ raise NotImplementedError
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require "docomoru/commands/base"
2
+
3
+ module Docomoru
4
+ module Commands
5
+ class Error < Base
6
+ def call
7
+ abort(@arguments.error_message)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ require "docomoru/client"
2
+ require "docomoru/commands/base"
3
+ require "docomoru/response_renderer"
4
+
5
+ module Docomoru
6
+ module Commands
7
+ class Request < Base
8
+ def call
9
+ response = client.send(
10
+ @arguments.method_name,
11
+ *@arguments.arguments,
12
+ @arguments.params,
13
+ @arguments.headers,
14
+ )
15
+ print ResponseRenderer.new(
16
+ response,
17
+ show_body: @arguments.show_body,
18
+ show_header: @arguments.show_header,
19
+ )
20
+ end
21
+
22
+ private
23
+
24
+ def client
25
+ Client.new(api_key: @arguments.api_key)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,4 +1,5 @@
1
1
  require "active_support/core_ext/string/inflections"
2
+ require "rack/utils"
2
3
 
3
4
  module Docomoru
4
5
  class Response
@@ -21,5 +22,9 @@ module Docomoru
21
22
  def status
22
23
  @raw_status
23
24
  end
25
+
26
+ def status_message
27
+ Rack::Utils::HTTP_STATUS_CODES[status]
28
+ end
24
29
  end
25
30
  end
@@ -0,0 +1,54 @@
1
+ require "active_support/core_ext/string/strip"
2
+ require "json"
3
+
4
+ module Docomoru
5
+ class ResponseRenderer
6
+ def initialize(response, show_header: nil, show_body: nil)
7
+ @response = response
8
+ @show_body = show_body
9
+ @show_header = show_header
10
+ end
11
+
12
+ def to_s
13
+ template % {
14
+ status: status,
15
+ headers: headers,
16
+ body: body,
17
+ }
18
+ end
19
+
20
+ private
21
+
22
+ def body
23
+ JSON.pretty_generate(@response.body) + "\n"
24
+ end
25
+
26
+ def headers
27
+ @response.headers.sort_by do |key, value|
28
+ key
29
+ end.map do |key, value|
30
+ "%{key}: %{value}" % {
31
+ key: key.split("-").map(&:camelize).join("-"),
32
+ value: value,
33
+ }
34
+ end.join("\n")
35
+ end
36
+
37
+ def status
38
+ "#{@response.status} #{@response.status_message}"
39
+ end
40
+
41
+ def template
42
+ str = ""
43
+ str << <<-EOS.strip_heredoc if @show_header
44
+ HTTP/1.1 %{status}
45
+ %{headers}
46
+ EOS
47
+ if @show_body
48
+ str << "\n" if @show_header
49
+ str << "%{body}"
50
+ end
51
+ str
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,3 @@
1
1
  module Docomoru
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/docomoru.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require "docomoru/client"
2
+ require "docomoru/command_builder"
2
3
  require "docomoru/version"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docomoru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -52,6 +52,48 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: slop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
55
97
  - !ruby/object:Gem::Dependency
56
98
  name: bundler
57
99
  requirement: !ruby/object:Gem::Requirement
@@ -97,7 +139,8 @@ dependencies:
97
139
  description:
98
140
  email:
99
141
  - r7kamura@gmail.com
100
- executables: []
142
+ executables:
143
+ - docomoru
101
144
  extensions: []
102
145
  extra_rdoc_files: []
103
146
  files:
@@ -107,11 +150,19 @@ files:
107
150
  - LICENSE.txt
108
151
  - README.md
109
152
  - Rakefile
153
+ - bin/docomoru
110
154
  - docomoru.gemspec
155
+ - images/cli.png
111
156
  - lib/docomoru.rb
157
+ - lib/docomoru/arguments.rb
112
158
  - lib/docomoru/client.rb
159
+ - lib/docomoru/command_builder.rb
160
+ - lib/docomoru/commands/base.rb
161
+ - lib/docomoru/commands/error.rb
162
+ - lib/docomoru/commands/request.rb
113
163
  - lib/docomoru/dialogue_methods.rb
114
164
  - lib/docomoru/response.rb
165
+ - lib/docomoru/response_renderer.rb
115
166
  - lib/docomoru/version.rb
116
167
  homepage: https://github.com/r7kamura/docomoru
117
168
  licenses: