Class: StraightServerKit::Client

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

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(url: DEFAULT_API_URL) ⇒ Client

Returns a new instance of Client.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



25
26
27
28
29
30
31
# File 'lib/straight-server-kit/client.rb', line 25

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

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



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

def resources
  @resources
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



33
34
35
36
37
# File 'lib/straight-server-kit/client.rb', line 33

def self.resources
  {
    orders: OrderResource,
  }
end

Instance Method Details

#connectionObject



19
20
21
22
23
# File 'lib/straight-server-kit/client.rb', line 19

def connection
  Faraday.new(connection_options) { |req|
    req.adapter :net_http
  }
end

#pay_url(order) ⇒ Object



15
16
17
# File 'lib/straight-server-kit/client.rb', line 15

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