Module: FlowcommerceSpree::Api

Extended by:
Api
Included in:
Api
Defined in:
lib/flowcommerce_spree/api.rb

Instance Method Summary collapse

Instance Method Details

#format_default_price(amount) ⇒ Object



44
45
46
# File 'lib/flowcommerce_spree/api.rb', line 44

def format_default_price(amount)
  format('$%<price>.2f', amount)
end

#loggerObject



40
41
42
# File 'lib/flowcommerce_spree/api.rb', line 40

def logger
  @logger ||= Logger.new('./log/flow.log') # or nil for no logging
end

#run(action, path, params = {}, body = nil) ⇒ Object

builds curl command and gets remote data



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/flowcommerce_spree/api.rb', line 10

def run(action, path, params = {}, body = nil)
  body ||= params.delete(:BODY)

  remote_params = URI.encode_www_form params
  remote_path   = debug_path = path.sub('%o', ORGANIZATION).sub(':organization', ORGANIZATION)
  remote_path  += "?#{remote_params}" unless remote_params.blank?

  curl = ['curl -s']
  curl.push "-X #{action.to_s.upcase}"
  curl.push "-u #{API_KEY}:"

  if body
    body = body.to_json unless body.is_a?(Array)
    curl.push '-H "Content-Type: application/json"'
    curl.push "-d '#{body.gsub(%['], %['"'"'])}'" if body
  end

  curl.push "\"https://api.flow.io#{remote_path}\""
  command = curl.join(' ')

  puts command if defined?(Rails::Console)

  dir = Rails.root.join('log/api')
  Dir.mkdir(dir) unless Dir.exist?(dir)
  debug_file = "#{dir}/#{debug_path.gsub(/[^\w]+/, '_')}.bash"
  File.write debug_file, command + "\n"

  JSON.load `#{command}`
end