Class: Apiary::Command::Publish

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/apiary/command/publish.rb

Overview

Display preview of local blueprint file

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#api_description_source, #api_description_source_path

Constructor Details

#initialize(opts) ⇒ Publish

Returns a new instance of Publish.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/apiary/command/publish.rb', line 17

def initialize(opts)
  @options = OpenStruct.new(opts)
  @options.path           ||= '.'
  @options.api_host       ||= 'api.apiary.io'
  @options.api_name       ||= false
  @options.api_key        ||= ENV['APIARY_API_KEY']
  @options.proxy          ||= ENV['http_proxy']
  @options.headers        ||= {
    accept: 'text/html',
    content_type: 'text/plain',
    authentication: "Token #{@options.api_key}",
    user_agent: Apiary.user_agent
  }
  @options.message ||= 'Saving API Description Document from apiary-client'

  begin
    @source_path = api_description_source_path(@options.path)
  rescue StandardError => e
    abort "#{e.message}"
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/apiary/command/publish.rb', line 15

def options
  @options
end

Instance Method Details

#executeObject



39
40
41
# File 'lib/apiary/command/publish.rb', line 39

def execute
  publish_on_apiary
end

#publish_on_apiaryObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/apiary/command/publish.rb', line 43

def publish_on_apiary
  unless @options.api_name
    abort 'Please provide an api-name option (subdomain part from your http://docs.<api-name>.apiary.io/)'
  end

  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

  query_apiary
end

#query_apiaryObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/apiary/command/publish.rb', line 55

def query_apiary
  url = "https://#{@options.api_host}/blueprint/publish/#{@options.api_name}"
  source = api_description_source(@source_path)

  return if source.nil?

  data = {
    code: source,
    messageToSave: @options.message,
    shouldCommit: @options.push ? 'yes' : 'no'
  }

  RestClient.proxy = @options.proxy

  begin
    RestClient.post url, data, @options.headers
  rescue RestClient::BadRequest => e
    err = JSON.parse e.response
    if err.key? 'parserError'
      abort "#{err['message']}: #{err['parserError']}"
    else
      abort "Apiary service responded with an error: #{err['message']}"
    end
  rescue RestClient::Exception => e
    err = JSON.parse e.response
    if err.key? 'message'
      abort "Apiary service responded with an error: #{err['message']}"
    else
      abort "Apiary service responded with an error: #{e.message}"
    end
  end
end