oak-api 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: 42742f45f4e185f8a2ca56d893f1f27da0a9b00c321a5260e90d95b74556d31c
4
- data.tar.gz: 0b8e7861ea2bf38585fcf42be718941c0f0caeb5a847e62eccf18b3d90f5fc5a
3
+ metadata.gz: b76688f0f340c45b69ee2ecd0a979d357fe5079d6e0c4af0e233d1dd63c1b499
4
+ data.tar.gz: 07aade4191b0334d2adde947bacc897cc27fe673593fbed84a06458da03aefef
5
5
  SHA512:
6
- metadata.gz: 69cd81baf442638b28f14729011a9c6173fdf2acc19de62ffc449690e81ca01b8defa9f029353cd2dfc8cfcab7964c33613cf02c3236889105741630fbd93725
7
- data.tar.gz: df97d1fee1917a92d8e7cf2aab598fd9113c60c37ec6f1c2acd4fde63a1e1ce5588112cc751886bc124469f8f4af05195f062bce0cd8ab4b47eb51a0d30af16c
6
+ metadata.gz: d2b05cb1a62d437db88eab0d4d229e981a3e5c8da10529c16092f84fd188f7a0bdfc277b55e85efaa0050ff6a79f2c2b79433bc360b5048f7c2e45c9058e9739
7
+ data.tar.gz: 58e207df921218b6deac9215f72736343ca8bda5d37a21fc48e4d1611cf5e3c143d9b911787ab6914cffb5c8f3b9e4d833c154709c02e85aaf483a756623b0b0
data/.gitignore CHANGED
@@ -20,6 +20,7 @@
20
20
  /log/*
21
21
  /public/*
22
22
  /db/*
23
+ /etc/*
23
24
 
24
25
  # files
25
26
  *.dump
@@ -44,6 +45,7 @@ core.*
44
45
  cscope.out
45
46
  secrets.yml
46
47
  .tags
48
+ NOTES*
47
49
 
48
50
  # don't ignore
49
51
  !.gitignore
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 sergioro
3
+ Copyright (c) 2021 Oak Software
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,39 +1,26 @@
1
- # Oak::Api
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/oak/api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'oak-api'
1
+ # Oak API tool
2
+
3
+ This gem looks for your API key in the `OAK_API_KEY` shell variable.
4
+
5
+ After setting such variable let the magic begin:
6
+
7
+ ```bash
8
+ # list available API endpoints
9
+ $ oak -l
10
+ ["/api/docs/?",
11
+ "/api/blog/ids/?",
12
+ "/api/endpoints",
13
+ ...
14
+
15
+ # get a blog post data
16
+ $ oak g blog/3
17
+ {"post_title"=>"Third Post",
18
+ "post_markdown"=>"Testing 3",
19
+ "post_slug"=>"third-post",
20
+ "post_draft"=>false,
21
+ "post_categories"=>"cook",
22
+ "post_created_at"=>"2021-03-13",
23
+ "post_prev"=>"second-post",
24
+ "post_next"=>"starting-the-oak-blog"}
13
25
  ```
14
26
 
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install oak-api
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/oak-api.
36
-
37
- ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/exe/oak CHANGED
@@ -4,4 +4,4 @@ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
4
 
5
5
  require "oak_api"
6
6
 
7
- Oak.greetings
7
+ Oak.go(ARGV.dup)
@@ -2,13 +2,13 @@ require 'zlib'
2
2
 
3
3
  module Oak
4
4
  OakString = <<~tree
5
- eJyNksFqxCAQhu8+hXhJC9E8QI/tykBbcujCUggYRNjeAj3nXfJEeaf+E+Ou
6
- ruzSIWLi/834j1HIm3CtbpTCi0KMWhtRyFAz1rWt1kNOtM0uaM0Fgo3LzxnT
7
- YRityEcoIZ0oa3AZ8ijlglXasC9R6ljiPTavMGqci8BT7TTFmNoZUTDqbh/X
8
- eElOdgLTqBvyMOGcMbmVYZPRiwFAozaBrLo0guPjCpClJJIyZFoKFqEFWyks
9
- EcXjcXejTpMzEW1ZOOnT4eO1/zxUzHAnb+b5unTsK+h/iZxLxbaPEuWQdcTf
10
- p3Xpt+dtXb7WJfbfPapQNJffpmsEsL5aHWpjJtgwe/zU280SOl1AsoFm8scC
11
- ngQcfPN5oK8W2wICRT8TDOASvf9O5zMI8QdEy6kN
5
+ eJyNUrFqwzAQ3fULXQ4tbsGSWzxmTCMO2uKhhlBqkBGCdAmGzv74PsVWLFsk
6
+ 9EDI3Hv37t1ZgjZhS1XIGL1SWqxgoAnXlqVS1KWUspgRpYKCN1P6KeFUOFpJ
7
+ dhMpUiqx1ggy7CBlvZFKB2NijSMVeuBLSjjV1k6Ex9xqjD7O00Nwwu18lthF
8
+ JzMDV68KdjBhrdaple4CYxYNAvdKezbyOgj2FxQAEzET+QSLEUBg3mRIgJin
9
+ 9dibkZfRyMyXqooevl929a6uz3Q8vO+bjwMyz+dtQXdDZAx3klvU2oZa3oiN
10
+ /1TJxI5N8/oZte6pUJcMvllLda9uNWz61Jbw4Los2+V2tDd+dPjj22aROlyJ
11
+ bDyP7NoVeRBw8BVWgmlKtAUJLP4ZYAAv7O13OJ3AEH+N0q0z
12
12
  tree
13
13
  .delete("\s")
14
14
 
data/lib/oak/errors.rb CHANGED
@@ -1,5 +1,6 @@
1
- class Oak
1
+ module Oak
2
2
  class OakError < StandardError; end
3
3
  class SomeWeirdError < OakError; end
4
+ class UnknownHTTPVerb < OakError; end
4
5
  end
5
6
 
data/lib/oak/monkey.rb ADDED
@@ -0,0 +1,8 @@
1
+ # monkey patches
2
+
3
+ class Object
4
+ # use for debugging
5
+ def info(*args)
6
+ puts "\e[32m=> #{args}\e[0m"
7
+ end
8
+ end
data/lib/oak/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Oak
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/oak_api.rb CHANGED
@@ -2,46 +2,127 @@ $LOAD_PATH.unshift __dir__
2
2
 
3
3
  # stdlib
4
4
  require 'optparse'
5
+ require 'net/http'
6
+ require 'json'
5
7
 
6
8
  # internal
7
- require 'oak/api'
8
9
  require 'oak/version'
10
+ require 'oak/monkey'
9
11
  require 'oak/easter_egg'
10
12
 
11
13
  module Oak
12
- def self.run(args)
13
- options = Options.parse(args)
14
+ def self.go(args)
15
+ Oak::Request.new(args).go
14
16
  end
15
17
 
16
- class Options
17
- def self.parse(args)
18
- options = {}
18
+ class Request
19
+ DEFAULT_HEADERS = {'authorization' => ENV['OAK_API_KEY']}
19
20
 
20
- OptionParser.new do |opts|
21
- opts.program_name = File.basename(bash)
22
- opts.banner = "Usage: #{opts.program_name} [options]"
21
+ class HTTP
22
+ attr_reader :verb, :uri, :headers, :body
23
23
 
24
- opts.separator = ""
25
- opts.separator = "Options:"
24
+ VERBS = { g: :get, p: :post, c: :patch, d: :delete }
26
25
 
27
- opts.on("-oNAME", "--output=NAME", "Specify output file name") do |v|
28
- options[:output] = v
29
- end
26
+ def initialize(args, base_url, api_version, headers, debug=nil)
27
+ @verb = args.shift.to_sym
28
+ @uri = URI("#{base_url}/api/#{api_version}/#{args.shift}")
29
+ @body = args.shift
30
+ @verb = VERBS[@verb] || @verb
31
+ @headers = headers
32
+ raise UnknownHTTPVerb unless VERBS.values.include?(@verb)
33
+ end
30
34
 
31
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
32
- options[:verbose] = v
33
- end
35
+ def set_request
36
+ @req = Net::HTTP.const_get(verb.to_s.capitalize).new(uri, headers)
37
+ @req.body = body
38
+ @req
39
+ end
40
+ end
41
+
42
+ attr_reader :options, :base_url, :args, :debug, :headers
43
+
44
+ def initialize(args)
45
+ @options = Options.parse(args)
46
+ @args = args
47
+ @base_url = development? ? 'http://localhost:3000' : 'https://oak.software'
48
+ @options[:headers] = @options[:headers].to_h.merge({'authorization' => ENV['OAK_API_KEY_DEVEL']}) if development?
49
+ @headers = DEFAULT_HEADERS.merge(@options[:headers].to_h)
50
+ @debug = @options[:debug]
51
+ info self if @debug
52
+ end
53
+
54
+ def development?
55
+ @options[:environment] == 'development'
56
+ end
57
+
58
+ def go
59
+ if @options[:list]
60
+ args.unshift(?g, "endpoints")
61
+ end
62
+
63
+ req = HTTP.new(args, base_url, options[:api], headers, debug).set_request
64
+
65
+ info [req, req.uri, req.uri.host, req.uri.port, req.body] if debug
66
+ info req.to_hash if debug
67
+
68
+ http = Net::HTTP.new(req.uri.host, req.uri.port)
69
+ http.use_ssl = true unless development?
70
+ res = http.request(req)
71
+
72
+ puts res.body
73
+ end
74
+
75
+ class Options
76
+ def self.parse(args)
77
+ options = {}
78
+
79
+ OptionParser.new do |opts|
80
+ opts.program_name = 'oak-api'
81
+ opts.banner = "Usage: #{opts.program_name} [options] [http_verb] [endpoint] [http_body]"
82
+
83
+ opts.separator ""
84
+ opts.separator "Options:"
85
+
86
+ opts.on("-d", "--debug", "Enable debug mode") do |v|
87
+ options[:debug] = v
88
+ end
89
+
90
+ opts.on("-e", "--environment=ENV", "Enable debug mode ") do |v|
91
+ options[:environment] = v
92
+ end
93
+
94
+ opts.on("-l", "--list", "List API endpoints") do |v=x|
95
+ options[:list] = v
96
+ end
97
+
98
+ opts.on_tail("-a", "--api=VERSION", "Target a specific API version (defaults to latest)") do |v|
99
+ options[:api] = v
100
+ end
101
+
102
+ opts.on_tail("-H", "--headers=HEADERS", "HEADERS is a JSON string with HTTP headers") do |v|
103
+ options[:headers] = JSON.parse(v)
104
+ end
105
+
106
+ opts.on("-o", "--output=NAME", "Specify output file name") do |v|
107
+ options[:output] = v
108
+ end
109
+
110
+ opts.on("-t", "--tree", "Print a welcome message") do |v|
111
+ Oak.greetings
112
+ abort
113
+ end
34
114
 
35
- opts.on_tail("-h", "--help", "Print help", "something else") do |v|
36
- abort opts.to_s
37
- end
115
+ opts.on_tail("-h", "--help", "Print help", "something else") do |v|
116
+ abort opts.to_s
117
+ end
38
118
 
39
- opts.on_tail("-V", "--version", "Print version") do |v|
40
- abort "#{opts.program_name} #{VERSION}"
41
- end
42
- end.parse!(args)
119
+ opts.on_tail("-v", "--version", "Print version") do |v|
120
+ abort "#{opts.program_name} #{VERSION}"
121
+ end
122
+ end.parse!(args)
43
123
 
44
- options
124
+ options
125
+ end
45
126
  end
46
127
  end
47
128
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oak-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oak Software
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-26 00:00:00.000000000 Z
11
+ date: 2021-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -178,12 +178,10 @@ files:
178
178
  - LICENSE.txt
179
179
  - README.md
180
180
  - Rakefile
181
- - bin/console
182
- - bin/setup
183
181
  - exe/oak
184
- - lib/oak/api.rb
185
182
  - lib/oak/easter_egg.rb
186
183
  - lib/oak/errors.rb
184
+ - lib/oak/monkey.rb
187
185
  - lib/oak/version.rb
188
186
  - lib/oak_api.rb
189
187
  - oak-api.gemspec
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require "bundler/setup"
5
- require "oak/api"
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require "irb"
15
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/lib/oak/api.rb DELETED
@@ -1,6 +0,0 @@
1
- module Oak
2
- module Api
3
- class Error < StandardError; end
4
- # Your code goes here...
5
- end
6
- end