Class: Johac::Client
Overview
Base class for Johac API Client.
Extend this class and provide API specific calls
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#capture(&block) ⇒ Array
Capture requests and prevent them from going to the remote.
-
#curl_capture(&block) ⇒ String
Produce curls commands for captured requests made, within the block.
-
#env ⇒ String
Reference to the current environment.
-
#initialize(config = nil) ⇒ Client
constructor
A new instance of Client.
-
#uri ⇒ String
Reference to
base_uriset on config in configure or in the constructor.
Constructor Details
#initialize(config = nil) ⇒ Client
Returns a new instance of Client.
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
#config ⇒ Object (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.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/johac/client.rb', line 63 def capture(&block) result = [] @mutex.synchronize { begin connection..context = result yield(self) ensure connection..context = nil end } result end |
#curl_capture(&block) ⇒ String
Produce curls commands for captured requests made, within the block
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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.each { |name, value| output << "-H'#{name}: #{value}'" } output << "'#{env.url}'" if env.body output << '-d' output << "'#{env.body}'" end output.join(' ') } end |
#env ⇒ String
Reference to the current environment. Set explicitly in Johac.configure or via environment variables JOHAC_ENV or RAILS_ENV.
34 35 36 |
# File 'lib/johac/client.rb', line 34 def env config.env end |
#uri ⇒ String
Reference to base_uri set on Johac.config in Johac.configure or in the constructor.
26 27 28 |
# File 'lib/johac/client.rb', line 26 def uri config.base_uri end |