herdic 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db085b78a8df105d3eb982a5b834d7304c1a8260
4
+ data.tar.gz: 5c30a1b5ec2a9f5eaa5d40f011fd84276942392a
5
+ SHA512:
6
+ metadata.gz: 2ddd65a6cbc49daa112c8cb94afd8be6b94aefd1d6b3788f3e8ea4615a9eea5f409f9a094751a5e964e52a8758410e9f077050332bf14f41099639e44f913f32
7
+ data.tar.gz: 5d2cfe473cbef993e072f529a14bd00cfcf39569a7bf3e4f2de55bea794adb219ad5095806c56cdd9a1c01f6ea6f4a02d33a2b113f7b542e82c4b19fbc4fdc38
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Yuki Iwanaga
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ Herdic
2
+ ======
3
+
4
+ **Herdic is a command line HTTP client intended to create and test API documentation with ease.**
5
+
6
+
7
+ License
8
+ -------
9
+
10
+ This project is copyright by [Creasty](http://www.creasty.com), released under the MIT lisence.
11
+ See `LICENSE` file for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+
data/bin/herdic ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'herdic'
4
+
5
+
6
+ options = ARGV.getopts 'e', 'c:', 'use-ssl', 'html'
7
+ file = ARGV[0]
8
+
9
+ raise 'No file given' if !file || file.empty?
10
+
11
+ Herdic.setup
12
+
13
+ client = Herdic::Client.new file, options
14
+ client.run_all
data/herdic.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'herdic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'herdic'
8
+ spec.version = Herdic::VERSION
9
+ spec.authors = ['Yuki Iwanaga']
10
+ spec.email = ['yuki@creasty.com']
11
+ spec.summary = 'A command line HTTP client intended to create and test API documentation with ease'
12
+ spec.description = 'A command line HTTP client intended to create and test API documentation with ease'
13
+ spec.homepage = 'https://github.com/creasty/herdic'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'activesupport', '~> 4.1', '>= 3.2'
22
+ spec.add_dependency 'awesome_print', '~> 1.2'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.6'
25
+ spec.add_development_dependency 'rake', '~> 10.3'
26
+ end
data/lib/herdic.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'herdic/version'
2
+ require 'herdic/util'
3
+ require 'herdic/store'
4
+ require 'herdic/loader'
5
+ require 'herdic/printer'
6
+ require 'herdic/client'
7
+
8
+ require 'fileutils'
9
+
10
+
11
+ module Herdic
12
+
13
+ class Configuration
14
+
15
+ attr_accessor :pwd,
16
+ :herdic_path,
17
+ :store_path,
18
+ :edit_request_file,
19
+ :config_filename
20
+
21
+ def initialize
22
+ @pwd = File.expand_path '.'
23
+ @herdic_path = File.expand_path '~/.herdic'
24
+ @store_path = File.join @herdic_path, 'store'
25
+ @edit_request_file = File.join @herdic_path, 'edit_request.yml'
26
+ @config_filename = 'api_config.yml'
27
+ end
28
+
29
+ end
30
+
31
+ class << self
32
+
33
+ def setup
34
+ @config ||= Configuration.new
35
+
36
+ yield @config if block_given?
37
+
38
+ ensure_directory!
39
+ end
40
+
41
+ def ensure_directory!
42
+ FileUtils.mkdir_p self.herdic_path
43
+ FileUtils.mkdir_p self.store_path
44
+ end
45
+
46
+ def method_missing(method_name, *args, &block)
47
+ if @config.respond_to? method_name
48
+ @config.send method_name, *args, &block
49
+ else
50
+ super
51
+ end
52
+ end
53
+
54
+ def respond_to?(method_name, include_private = false)
55
+ @config.respond_to? method_name
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,133 @@
1
+ require 'active_support/core_ext'
2
+ require 'erb'
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'open-uri'
6
+ require 'optparse'
7
+ require 'psych'
8
+ require 'uri'
9
+
10
+
11
+ module Herdic
12
+ class Client
13
+
14
+ attr_reader :registry, :config
15
+
16
+ def initialize(file, options)
17
+ @file, @options = file, options
18
+
19
+ @config_file = @options['c'] || Util.find_files_upward(Herdic.config_filename, Herdic.pwd, 1)[0]
20
+ @context = binding
21
+
22
+ @store = Store.new @config_file
23
+ @registry = @store.data
24
+
25
+ @printer = Printer.new @options
26
+
27
+ load_config @config_file if @config_file
28
+ load_yaml
29
+ end
30
+
31
+ def load_config(file)
32
+ @config = {}
33
+
34
+ File.open(file, 'r') do |f|
35
+ @config = ERB.new(f.read).result @context
36
+ end
37
+
38
+ @config = Psych.load @config
39
+ end
40
+
41
+ def load_yaml
42
+ @specs = Loader.new(@file).specs
43
+
44
+ if @options['e']
45
+ File.open(Herdic.edit_request_file, 'w+') do |f|
46
+ f.write Psych.dump(@specs)
47
+ end
48
+
49
+ system "$EDITOR '%s'" % Herdic.edit_request_file
50
+
51
+ File.open(Herdic.edit_request_file, 'r') do |f|
52
+ @specs = Psych.load f.read
53
+ end
54
+ end
55
+ end
56
+
57
+ def run_all
58
+ @printer.start_message @specs
59
+
60
+ @specs.each do |spec|
61
+ # FIXME: figure out better way
62
+ spec = Psych.dump spec
63
+ spec = ERB.new(spec).result @context
64
+ spec = Psych.load spec
65
+
66
+ run spec
67
+ end
68
+
69
+ @store.save
70
+ end
71
+
72
+ private def run(spec)
73
+ setup_spec spec
74
+
75
+ uri = URI.parse @meta['endpoint']
76
+ http = Net::HTTP.new uri.host, uri.port
77
+ http.use_ssl = @options['use-ssl']
78
+
79
+ response = http.start do
80
+ case @meta['method']
81
+ when 'GET'
82
+ uri.query = @body.to_query
83
+ http.get uri.request_uri, @header
84
+ when 'POST'
85
+ http.post uri.request_uri, @body.to_query, @header
86
+ when 'PATCH'
87
+ http.patch uri.request_uri, @body.to_query, @header
88
+ when 'DELETE'
89
+ http.delete uri.request_uri, @header
90
+ else
91
+ raise "Unsupported method: #{@meta['method']}"
92
+ end
93
+ end
94
+
95
+ body = response.body
96
+ body = JSON.parse body if 'application/json' == response.content_type
97
+
98
+ @printer.title @meta
99
+ @printer.request @header, @body
100
+ @printer.response response, body
101
+
102
+ register response, body
103
+ end
104
+
105
+ private def setup_spec(spec)
106
+ @meta = spec.slice 'title', 'method', 'endpoint'
107
+ @register = spec['register'] || {}
108
+ @header = spec['header'] || {}
109
+ @body = spec['body'] || {}
110
+
111
+ @meta['method'].upcase!
112
+ @header = @header.map { |k, v| [k, v.to_s] }.to_h
113
+ end
114
+
115
+ private def register(response, body)
116
+ return unless 'application/json' == response.content_type
117
+
118
+ @register.each do |name, path|
119
+ next if path.empty?
120
+
121
+ val = body
122
+
123
+ path.to_s.split('.').each do |key|
124
+ key = key.to_i if key === /\A\d+\z/
125
+ val = val.try :[], key
126
+ end
127
+
128
+ @registry[name] = val
129
+ end
130
+ end
131
+
132
+ end
133
+ end
@@ -0,0 +1,38 @@
1
+ require 'psych'
2
+
3
+
4
+ module Herdic
5
+ class Loader
6
+
7
+ attr_accessor :specs
8
+
9
+ def initialize(file)
10
+ @specs = []
11
+
12
+ load file
13
+ end
14
+
15
+ def load(file, included_from = nil)
16
+ file = File.expand_path file, included_from
17
+
18
+ File.open(file, 'r') do |f|
19
+ @_specs = f.read
20
+ end
21
+
22
+ @_specs = Psych.load @_specs
23
+
24
+ @_specs.each do |spec|
25
+ spec['file'] = file
26
+
27
+ if spec['include']
28
+ load spec['include'], File.dirname(file)
29
+ else
30
+ @specs << spec
31
+ end
32
+ end
33
+
34
+ @_specs = nil
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,84 @@
1
+ require 'ap'
2
+
3
+
4
+ module Herdic
5
+ class Printer
6
+
7
+ def initialize(options)
8
+ @options = options
9
+ end
10
+
11
+ def start_message(specs)
12
+ puts 'Running %d spec(s)' % specs.size
13
+ end
14
+
15
+ def title(meta)
16
+ puts "\n\n"
17
+ hr rule: '=', color: :green
18
+
19
+ puts [meta['method'].green, meta['endpoint']].join(' ')
20
+
21
+ if meta['title']
22
+ indent = ' ' * meta['method'].size
23
+ puts [indent, meta['title'].white].join(' ')
24
+ end
25
+
26
+ hr rule: '=', color: :green
27
+ end
28
+
29
+ def subtitle(text)
30
+ puts
31
+ puts "==> #{text}".blue
32
+ puts
33
+ end
34
+
35
+ def request(header, body)
36
+ subtitle 'Request'
37
+
38
+ if header.empty?
39
+ puts 'no header'.white
40
+ else
41
+ print_header header
42
+ end
43
+
44
+ hr
45
+
46
+ ap body
47
+ end
48
+
49
+ def response(response, body)
50
+ subtitle "Response: #{response.code} #{response.message}"
51
+
52
+ print_header response.to_hash
53
+
54
+ hr
55
+
56
+ if body.empty?
57
+ puts 'no body'.white
58
+ elsif 'application/json' == response.content_type
59
+ ap body
60
+ elsif @options['html']
61
+ puts body
62
+ else
63
+ puts 'N/A'.white
64
+ end
65
+ end
66
+
67
+ private def print_header(header)
68
+ max_cols = header.keys.map(&:size).max
69
+ header.each do |k, v|
70
+ puts [("%#{max_cols}s" % k).white, v].join ' '
71
+ end
72
+ end
73
+
74
+ private def hr(rule: '-', color: :black)
75
+ puts (rule * terminal_cols).send(color)
76
+ end
77
+
78
+ private def terminal_cols
79
+ @terminal_cols ||= Integer(`tput cols`)
80
+ end
81
+
82
+ end
83
+ end
84
+
@@ -0,0 +1,28 @@
1
+ require 'digest'
2
+
3
+
4
+ module Herdic
5
+ class Store
6
+
7
+ attr_reader :data
8
+
9
+ def initialize(path = nil)
10
+ hash = Digest::MD5.hexdigest File.expand_path(path.to_s)
11
+ @file = File.join Herdic.store_path, hash
12
+
13
+ load
14
+ end
15
+
16
+ def load
17
+ data = nil
18
+ File.open(@file, 'a+b') { |f| data = f.read }
19
+ @data = Marshal.load(data) rescue {}
20
+ end
21
+
22
+ def save
23
+ data = Marshal.dump @data
24
+ File.open(@file, 'wb') { |f| f.write data }
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ module Herdic
2
+ module Util
3
+
4
+ def self.find_files_upward(filename, base, count = 0)
5
+ found = []
6
+
7
+ while count >= 1 || count < 0
8
+ file = File.join base, filename
9
+
10
+ if File.exists? file
11
+ found << file
12
+ count -= 1
13
+ end
14
+
15
+ break if '.' == base
16
+
17
+ base = File.dirname base
18
+ end
19
+
20
+ found
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ module Herdic
2
+
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ REVISION = 0
6
+
7
+ VERSION = [MAJOR, MINOR, REVISION].compact.join '.'
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: herdic
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuki Iwanaga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: '3.2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '3.2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: awesome_print
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.2'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.2'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.6'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.6'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.3'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.3'
75
+ description: A command line HTTP client intended to create and test API documentation
76
+ with ease
77
+ email:
78
+ - yuki@creasty.com
79
+ executables:
80
+ - herdic
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - ".gitignore"
85
+ - ".ruby-version"
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - bin/herdic
91
+ - herdic.gemspec
92
+ - lib/herdic.rb
93
+ - lib/herdic/client.rb
94
+ - lib/herdic/loader.rb
95
+ - lib/herdic/printer.rb
96
+ - lib/herdic/store.rb
97
+ - lib/herdic/util.rb
98
+ - lib/herdic/version.rb
99
+ homepage: https://github.com/creasty/herdic
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.2.2
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: A command line HTTP client intended to create and test API documentation
123
+ with ease
124
+ test_files: []