Sha256: cc6c371ff65d7dbb3235bb95e60ffe973b59b4ca5192a1c7dd317d18ae7b0152

Contents?: true

Size: 1.78 KB

Versions: 11

Compression:

Stored size: 1.78 KB

Contents

# encoding: utf-8
require 'rest-client'
require 'rack'
require 'ostruct'
require 'json'

require 'apiary/agent'

module Apiary::Command
  # Retrieve blueprint from apiary
  class Fetch
    def initialize(opts)
      @options = OpenStruct.new(opts)
      @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
      }
    end

    def execute
      response = fetch_from_apiary

      return unless response.instance_of? String

      puts response
    end

    def fetch_from_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

      response = query_apiary

      if @options.output
        write_generated_path(response['code'], @options.output)
      else
        response['code']
      end
    end

    def query_apiary
      url = "https://#{@options.api_host}/blueprint/get/#{@options.api_name}"
      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

    def write_generated_path(data, outfile)
      File.open(outfile, 'w') do |file|
        file.write(data)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
apiaryio-0.10.1 lib/apiary/command/fetch.rb
apiaryio-0.9.1 lib/apiary/command/fetch.rb
apiaryio-0.9.0 lib/apiary/command/fetch.rb
apiaryio-0.8.1 lib/apiary/command/fetch.rb
apiaryio-0.8.0 lib/apiary/command/fetch.rb
apiaryio-0.7.0 lib/apiary/command/fetch.rb
apiaryio-0.6.1 lib/apiary/command/fetch.rb
apiaryio-0.6.0 lib/apiary/command/fetch.rb
apiaryio-0.5.2 lib/apiary/command/fetch.rb
apiaryio-0.5.1 lib/apiary/command/fetch.rb
apiaryio-0.5.0 lib/apiary/command/fetch.rb