Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #205 from apiaryio/abtris/archive
Browse files Browse the repository at this point in the history
feat: add archive command
  • Loading branch information
Ladislav Prskavec authored Aug 25, 2021
2 parents 5be01f2 + d70f970 commit 5ab038f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export APIARY_API_KEY=<your_token>
```
$ apiary help
Commands:
apiary archive # Archive All Your API Description Documents from apiary.io to local files named following [api-project-subdomain.apib] pattern.
apiary fetch --api-name=API_NAME # Fetch API Description Document from API_NAME.docs.apiary.io
apiary help [COMMAND] # Describe available commands or one specific command
apiary preview # Show API documentation in browser or write it to file
Expand All @@ -68,6 +69,18 @@ Commands:

### Details

#### archive

```
$ apiary help archive
Usage:
apiary archive
Options:
Archive All Your API Description Documents from apiary.io to local files named following [api-project-subdomain.apib] pattern.
```

#### fetch

```
Expand Down
10 changes: 10 additions & 0 deletions lib/apiary/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

require 'thor'
require 'apiary/command/fetch'
require 'apiary/command/archive'
require 'apiary/command/preview'
require 'apiary/command/publish'
require 'apiary/command/styleguide'

module Apiary
class CLI < Thor

desc 'archive', 'Archive All Your API Description Documents from apiary.io to local files named following [api-project-subdomain.apib] pattern.'
method_option :api_host, type: :string, banner: 'HOST', desc: 'Specify apiary host', hide: true

def archive
cmd = Apiary::Command::Archive.new options
cmd.execute
end

desc 'fetch', 'Fetch API Description Document from API_NAME.docs.apiary.io'
method_option :api_name, type: :string, required: true
method_option :api_host, type: :string, banner: 'HOST', desc: 'Specify apiary host', hide: true
Expand Down
71 changes: 71 additions & 0 deletions lib/apiary/command/archive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# encoding: utf-8

require 'rest-client'
require 'json'

require 'apiary/agent'

module Apiary::Command
# Retrieve blueprint from apiary
class Archive
def initialize(opts)
@options = OpenStruct.new(opts)
@options.api_host ||= 'api.apiary.io'
@options.api_key ||= ENV['APIARY_API_KEY']
@options.proxy ||= ENV['http_proxy']
@options.headers ||= {
accept: 'application/json',
content_type: 'application/json',
authorization: "Bearer #{@options.api_key}",
user_agent: Apiary.user_agent
}
end

def execute
response = apilist_from_apiary

return unless response.instance_of? String

puts response
end

def apilist_from_apiary
unless @options.api_key
abort 'API key must be provided through environment variable APIARY_API_KEY. Please go to https://login.apiary.io/tokens to obtain it.'
end

response = query_apiary

response['apis'].each do |api|
puts api['apiSubdomain']
@options = OpenStruct.new
@options.api_host ||= 'api.apiary.io'
@options.api_name ||= api['apiSubdomain']
@options.api_key ||= ENV['APIARY_API_KEY']
@options.proxy ||= ENV['http_proxy']
@options.output ||= api['apiSubdomain'] + '.apib'
@options.headers ||= {
accept: 'text/html',
content_type: 'text/plain',
authentication: "Token #{@options.api_key}",
user_agent: Apiary.user_agent
}
cmd = Apiary::Command::Fetch.new(@options)
cmd.execute
end
end

def query_apiary
url = "https://#{@options.api_host}/me/apis"
RestClient.proxy = @options.proxy

begin
response = RestClient.get url, @options.headers
rescue RestClient::Exception => e
abort "Apiary service responded with an error: #{e.message}"
end
JSON.parse response.body
end

end
end
2 changes: 1 addition & 1 deletion lib/apiary/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Apiary
VERSION = '0.15.2'.freeze
VERSION = '0.16.0'.freeze
end

0 comments on commit 5ab038f

Please sign in to comment.