class BlazingDocs::BlazingClient
Attributes
configuration[RW]
Public Class Methods
new(api_key, config)
click to toggle source
# File lib/blazingdocs/blazing_client.rb 22 def initialize(api_key, config) 23 raise TypeError, 'api_key expects a string' unless api_key.kind_of?(String) 24 25 @configuration = config.nil? ? Configuration.new : config.clone 26 @configuration.api_key = api_key 27 end
Public Instance Methods
get_account()
click to toggle source
# File lib/blazingdocs/blazing_client.rb 29 def get_account 30 hash = get('/account') 31 AccountModel.new(to_snake_keys(hash)) 32 end
get_templates(path = nil)
click to toggle source
# File lib/blazingdocs/blazing_client.rb 34 def get_templates(path = nil) 35 hashes = get("/templates/#{path or ''}") 36 hashes.map { |hash| TemplateModel.new(to_snake_keys(hash)) } 37 end
get_usage()
click to toggle source
# File lib/blazingdocs/blazing_client.rb 39 def get_usage 40 hash = get('/usage') 41 UsageModel.new(to_snake_keys(hash)) 42 end
merge(data, file_name, merge_parameters, template)
click to toggle source
# File lib/blazingdocs/blazing_client.rb 44 def merge(data, file_name, merge_parameters, template) 45 form_data = {} 46 47 raise ArgumentError, 'data is not provided' if data.nil? 48 raise ArgumentError, 'data expects a string' unless data.kind_of?(String) 49 50 form_data['Data'] = data 51 52 raise ArgumentError, 'file_name is not provided' if file_name.nil? 53 54 form_data['OutputName'] = file_name 55 56 raise ArgumentError, 'merge_parameters is not provided' if merge_parameters.nil? 57 58 if merge_parameters.is_a?(Hash) 59 form_data['MergeParameters'] = to_camel_keys(merge_parameters).to_json 60 elsif merge_parameters.is_a?(BlazingDocs::MergeParameters) 61 form_data['MergeParameters'] = to_camel_keys(to_hash(merge_parameters)).to_json 62 else 63 raise ArgumentError, 'merge_parameters expects Hash or MergeParameters' 64 end 65 66 raise ArgumentError, 'template is not provided' if template.nil? 67 68 options = default_options 69 if template.is_a?(File) 70 form_data['Template'] = UploadIO.new(template, 'application/octet-stream', File.basename(template)) 71 options = upload_options 72 elsif template.is_a?(String) 73 form_data['Template'] = template 74 end 75 76 hash = multipart_post('/operation/merge', form_data, options) 77 OperationModel.new(to_snake_keys(hash)) 78 end
Private Instance Methods
base_uri()
click to toggle source
# File lib/blazingdocs/blazing_client.rb 136 def base_uri 137 @configuration.base_uri 138 end
default_options()
click to toggle source
# File lib/blazingdocs/blazing_client.rb 140 def default_options 141 options = { read_timeout: @configuration.read_timeout } 142 options[:open_timeout] = @configuration.connect_timeout if RUBY_VERSION > '2.2.0' 143 options 144 end
get(path, params = {}, options = {})
click to toggle source
# File lib/blazingdocs/blazing_client.rb 86 def get(path, params = {}, options = {}) 87 handle_response do 88 headers = DEFAULT_HEADERS.merge({ API_KEY_HEADER => @configuration.api_key }) 89 request = Net::HTTP::Get.new(request_uri(path, params), headers) 90 91 http(options).request(request) 92 end 93 end
handle_response() { || ... }
click to toggle source
# File lib/blazingdocs/blazing_client.rb 104 def handle_response 105 response = yield 106 status = response.code.to_i 107 108 if status != 200 109 raise( 110 BlazingError, 111 status: status, 112 body: response.body, 113 headers: response.each_header.to_h, 114 ) 115 end 116 117 JSON.parse(response.body) 118 end
http(options = {})
click to toggle source
# File lib/blazingdocs/blazing_client.rb 120 def http(options = {}) 121 options = default_options if options.empty? 122 123 http = Net::HTTP.new(base_uri.host, base_uri.port) 124 http.open_timeout = options.fetch(:open_timeout) 125 http.read_timeout = options.fetch(:read_timeout) 126 http.use_ssl = base_uri.scheme == 'https' 127 # http.set_debug_output $stderr 128 http 129 end
multipart_post(path, form_data, options = {})
click to toggle source
# File lib/blazingdocs/blazing_client.rb 95 def multipart_post(path, form_data, options = {}) 96 handle_response do 97 headers = DEFAULT_HEADERS.merge({ API_KEY_HEADER => @configuration.api_key }) 98 request = Net::HTTP::Post::Multipart.new(path, form_data, headers) 99 100 http(options).request(request) 101 end 102 end
request_uri(path, params = {})
click to toggle source
# File lib/blazingdocs/blazing_client.rb 131 def request_uri(path, params = {}) 132 query = URI.encode_www_form(params) 133 base_uri.path + path + '?' + query 134 end
upload_options()
click to toggle source
# File lib/blazingdocs/blazing_client.rb 146 def upload_options 147 options = default_options 148 options[:read_timeout] = @configuration.upload_timeout 149 options 150 end