Class: Johac::Client

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/johac/client.rb

Overview

Base class for Johac API Client.

Extend this class and provide API specific calls

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Client

Returns a new instance of Client.

Parameters:



18
19
20
21
# File 'lib/johac/client.rb', line 18

def initialize(config=nil)
  @config = Johac.merged_config(config)
  @mutex = Mutex.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/johac/client.rb', line 8

def config
  @config
end

Instance Method Details

#capture(&block) ⇒ Array

Capture requests and prevent them from going to the remote. Useful for testing.

Parameters:

  • the (Block)

    block which invokes one or more requests

Returns:

  • (Array)

    faraday env structs which were captured in the block



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/johac/client.rb', line 65

def capture(&block)
  result = []
  @mutex.synchronize {
    begin
      connection.options.context = result
      yield(self)
    ensure
      connection.options.context = nil
    end
  }
  result
end

#curl_capture(&block) ⇒ String

Produce curls commands for captured requests made, within the block

Parameters:

  • the (Block)

    block which invokes one or more requests

Returns:

  • (String)

    requests which were captured as curl commands



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/johac/client.rb', line 42

def curl_capture(&block)
  capture(&block).map { |env|
    output = []
    output << "curl -s -X#{env.method.to_s.upcase}"
    env.request_headers.select { |name, value|
      name.downcase != 'user-agent'
    }.each { |name, value|
      output << "-H'#{name}: #{value}'"
    }
    output << "'#{env.url}'"
    if env.body
      output << '-d'
      output << "'#{env.body}'"
    end
    output.join(' ')
  }
end

#envString

Reference to the current environment. Set explicitly in Johac.configure or via environment variables JOHAC_ENV or RAILS_ENV.

Returns:

  • (String)

    One of “testing”, “development”, or “production”



34
35
36
# File 'lib/johac/client.rb', line 34

def env
  config.env
end

#uriString

Reference to base_uri set on Johac.config in Johac.configure or in the constructor.

Returns:

  • (String)


26
27
28
# File 'lib/johac/client.rb', line 26

def uri
  config.base_uri
end