Class: StraightServerKit::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/straight-server-kit/client.rb

Defined Under Namespace

Classes: SigningMiddleware

Constant Summary collapse

DEFAULT_API_URL =
'http://localhost:9000'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway_id:, secret:, url: DEFAULT_API_URL) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
# File 'lib/straight-server-kit/client.rb', line 10

def initialize(gateway_id:, secret:, url: DEFAULT_API_URL)
  @gateway_id = gateway_id
  @secret     = secret
  @url        = url
  @resources  = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/straight-server-kit/client.rb', line 28

def method_missing(name, *args, &block)
  if self.class.resources.has_key?(name)
    resources[name] ||= self.class.resources[name].for_gateway(@gateway_id).new(connection: connection)
  else
    super
  end
end

Instance Attribute Details

#gateway_idObject (readonly)

Returns the value of attribute gateway_id.



8
9
10
# File 'lib/straight-server-kit/client.rb', line 8

def gateway_id
  @gateway_id
end

#resourcesObject (readonly)

Returns the value of attribute resources.



8
9
10
# File 'lib/straight-server-kit/client.rb', line 8

def resources
  @resources
end

#secretObject (readonly)

Returns the value of attribute secret.



8
9
10
# File 'lib/straight-server-kit/client.rb', line 8

def secret
  @secret
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/straight-server-kit/client.rb', line 8

def url
  @url
end

Class Method Details

.resourcesObject



36
37
38
39
40
# File 'lib/straight-server-kit/client.rb', line 36

def self.resources
  {
    orders: OrderResource,
  }
end

Instance Method Details

#connectionObject



21
22
23
24
25
26
# File 'lib/straight-server-kit/client.rb', line 21

def connection
  Faraday.new(connection_options) do |faraday|
    faraday.use SigningMiddleware, @secret
    faraday.adapter :net_http
  end
end

#pay_url(order) ⇒ Object



17
18
19
# File 'lib/straight-server-kit/client.rb', line 17

def pay_url(order)
  File.join(url, order.pay_path) if order.pay_path
end