Class: HaveAPI::ClientExamples::FsClient

Inherits:
HaveAPI::ClientExample show all
Defined in:
lib/haveapi/client_examples/fs_client.rb

Instance Attribute Summary

Attributes inherited from HaveAPI::ClientExample

#action, #action_name, #base_url, #host, #resource, #resource_path, #version

Instance Method Summary collapse

Methods inherited from HaveAPI::ClientExample

auth, clients, code, example, init, #initialize, label, order, register, #version_url

Constructor Details

This class inherits a constructor from HaveAPI::ClientExample

Instance Method Details

#auth(method, desc) ⇒ Object



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
# File 'lib/haveapi/client_examples/fs_client.rb', line 13

def auth(method, desc)
  case method
  when :basic
    <<~END
      # Provide credentials as file system options
      #{init} -o auth_method=basic,user=myuser,password=secret

      # If username or password isn't provided, the user is asked on stdin
      #{init} -o auth_method=basic,user=myuser
      Password: secret
    END

  when :token
    <<~END
      # Authenticate using username and password
      #{init} -o auth_method=token,user=myuser
      Password: secret

      # If you have generated a token, you can use it
      #{init} -o auth_method=token,token=yourtoken

      # Note that the file system can read config file from haveapi-client, so if
      # you set up authentication there, the file system will use it.
    END

  when :oauth2
    '# OAuth2 is not supported by haveapi-fs.'
  end
end

#class_action?Boolean

Returns:



115
116
117
# File 'lib/haveapi/client_examples/fs_client.rb', line 115

def class_action?
  action[:path].index(/:[a-zA-Z\-_]+/).nil?
end

#example(sample) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/haveapi/client_examples/fs_client.rb', line 43

def example(sample)
  cmd = [init]

  path = [mountpoint].concat(resource_path)

  unless class_action?
    if !sample[:path_params] || sample[:path_params].empty?
      raise "example {#{sample}} of action #{resource_path.join('.')}.#{action_name} is for an instance action but does not include URL parameters"
    end

    path << sample[:path_params].first.to_s
  end

  path << 'actions' << action_name

  cmd << "\n# Change to action directory"
  cmd << "$ cd #{File.join(path)}"

  if sample[:request] && !sample[:request].empty?
    cmd << "\n# Prepare input parameters"

    sample[:request].each do |k, v|
      cmd << "$ echo '#{v}' > input/#{k}"
    end
  end

  cmd << "\n# Execute the action"
  cmd << '$ echo 1 > exec'

  cmd << "\n# Query the action's result"
  cmd << '$ cat status'
  cmd << (sample[:status] ? '1' : '0')

  if sample[:status]
    if sample[:response] && !sample[:response].empty? \
       && %i[hash object].include?(action[:output][:layout])
      cmd << "\n# Query the output parameters"

      sample[:response].each do |k, v|
        cmd << "$ cat output/#{k}"

        cmd << if v === true
                 '1'

               elsif v === false
                 '0'

               else
                 v.to_s
               end

        cmd << "\n"
      end
    end

  else
    cmd << "\n# Get the error message"
    cmd << '$ cat message'
    cmd << sample[:message]

    cmd << "\n# Parameter errors can be seen in the `errors` directory"
    cmd << '$ ls errors'
    cmd << (sample[:errors] || {}).keys.join("\n")
  end

  cmd.join("\n")
end

#initObject



9
10
11
# File 'lib/haveapi/client_examples/fs_client.rb', line 9

def init
  "# Mount the file system\n$ haveapi-fs #{base_url} #{mountpoint} -o version=#{version}"
end

#mountpointObject



111
112
113
# File 'lib/haveapi/client_examples/fs_client.rb', line 111

def mountpoint
  "/mnt/#{host}"
end