Class: Appbaseio::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/appbaseio/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



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

def initialize(options)
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/appbaseio/client.rb', line 50

def method_missing(m, *args, &block)
  operation, target = m.to_s.split('_')
  return super unless HTTP_METHOD_MAP.keys.include? operation
  return super unless TARGETS.include? target
  options = args[0] || {}
  vertex = options[:vertex] || nil
  data = {data: options[:data]}
  data = {all: true} if operation == 'read' and target == 'properties'
  data = {filters: {}} if operation == 'read' and target == 'edges'
  data = options[:data] if target == 'search'

  method = HTTP_METHOD_MAP[operation]
  if data
    resp = rest_client.send method, path(target, operation, vertex), data do |req|
      req.body = data.to_json if operation == 'delete'
    end
  else
    resp = rest_client.send method, path(target, operation, vertex) do |req|
    end
  end
  resp
end

Instance Method Details

#common_pathObject



30
31
32
# File 'lib/appbaseio/client.rb', line 30

def common_path
  "/#{app_name}/#{api_version}/#{namespace}"
end

#path(target, operation, vertex) ⇒ Object



46
47
48
# File 'lib/appbaseio/client.rb', line 46

def path(target, operation, vertex)
  [common_path, vertex, "~#{target}"].compact.join '/'
end

#rest_clientObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/appbaseio/client.rb', line 34

def rest_client
  Faraday.new(url: "https://"+server_host) do |faraday|
    #faraday.request :url_encoded
    #faraday.response :logger
    faraday.request :json
    faraday.response :json
    faraday.response :mashify
    faraday.request :api_header_auth, app_secret, auth_header: 'Appbase-Secret'
    faraday.adapter Faraday.default_adapter
  end
end