Module: FlowCommerce

Defined in:
lib/flow_commerce/client.rb

Constant Summary collapse

DEFAULT_TOKEN_FILE_LOCATION =
"~/.flow/token"

Class Method Summary collapse

Class Method Details

.instance(opts = {}) ⇒ Object

Creates a new instance of the flow cient, using standard conventions to identify the API TOKEN, checking in order:

1. an environment variable named FLOW_TOKEN
2. an environment variable named FLOW_TOKEN_FILE containing
   the path of the file with the token in it

Parameters:

  • base_url

    Alternate URL for the API



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
39
40
41
42
43
44
45
46
# File 'lib/flow_commerce/client.rb', line 13

def FlowCommerce.instance(opts={})
  base_url = opts[:base_url].to_s.strip
  token = opts[:token].to_s.strip
  http_handler = opts[:http_handler]

  if token.empty?
    token = ENV['FLOW_TOKEN'].to_s.strip

    if token.empty?
      file = ENV['FLOW_TOKEN_FILE'].to_s.strip
      if file.empty?
        file = DEFAULT_TOKEN_FILE_LOCATION
      end
      path = File.expand_path(file)

      if !File.exists?(path)
        raise "File %s does not exist. You can specify environment variable FLOW_TOKEN or FLOW_TOKEN_FILE to explicitly provide the token" % path
      end

      token = IO.read(path).strip
      if token.empty?
        raise "File %s did not contain an API Token" % path
      end
    end
  end

  auth = Io::Flow::V0::HttpClient::Authorization.basic(token)

  if base_url.empty?
    Io::Flow::V0::Client.at_base_url(:authorization => auth, :http_handler => http_handler)
  else
    Io::Flow::V0::Client.new(base_url, :authorization => auth, :http_handler => http_handler)
  end
end