Class: Honey::Command::Publish

Inherits:
Object
  • Object
show all
Defined in:
lib/honey/command/publish.rb

Overview

Display preview of local blueprint file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Publish

TODO: use OpenStruct to store @options



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/honey/command/publish.rb', line 14

def initialize(opts)
  @options = OpenStruct.new(opts)
  @options.path         ||= "apiary.apib"
  @options.api_host     ||= "api.apiary.io"
  @options.port         ||= 8080
  @options.api_name     ||= false
  @options.api_key      ||= ENV['APIARY_API_KEY']
  @options.headers      ||= {
    :accept => "text/html",
    :content_type => "text/plain",
    :authentication => "Token #{@options.api_key}"
  }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/honey/command/publish.rb', line 11

def options
  @options
end

Class Method Details

.execute(args) ⇒ Object



28
29
30
# File 'lib/honey/command/publish.rb', line 28

def self.execute(args)
  new(args).publish_on_apiary
end

Instance Method Details

#pathObject



51
52
53
# File 'lib/honey/command/publish.rb', line 51

def path
  @options.path || "#{File.basename(Dir.pwd)}.apib"
end

#publish_on_apiaryObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/honey/command/publish.rb', line 32

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

  self.query_apiary(@options.api_host, @options.path)

end

#query_apiary(host, path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/honey/command/publish.rb', line 55

def query_apiary(host, path)
  url  = "https://#{host}/blueprint/publish/#{@options.api_name}"
  data = {
    :code => File.read(path)
  }
  response = RestClient.post url, data, @options.headers

  unless response.code == 201
    abort "Request failed with code #{response.code}"
  end
end

#validate_apib_file(apib_file) ⇒ Object



45
46
47
48
49
# File 'lib/honey/command/publish.rb', line 45

def validate_apib_file(apib_file)
  unless File.exist?(apib_file)
    abort "Apiary definition file hasn't been found: #{apib_file.inspect}"
  end
end