Class: HaveAPI::Client::Communicator

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, v = nil, verify_ssl: true, language: nil, language_header: nil) ⇒ Communicator

Returns a new instance of Communicator.



21
22
23
24
25
26
27
28
29
30
# File 'lib/haveapi/client/communicator.rb', line 21

def initialize(url, v = nil, verify_ssl: true, language: nil, language_header: nil)
  @url = url
  @auth = Authentication::NoAuth.new(self, {}, {})
  @rest = RestClient::Resource.new(@url, verify_ssl: verify_ssl)
  @version = v
  @verify_ssl = verify_ssl
  @identity = 'haveapi-client-ruby'
  @language = language
  @language_header = language_header || I18n::DEFAULT_LANGUAGE_HEADER
end

Class Attribute Details

.auth_methodsObject (readonly)

Returns the value of attribute auth_methods.



10
11
12
# File 'lib/haveapi/client/communicator.rb', line 10

def auth_methods
  @auth_methods
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



18
19
20
# File 'lib/haveapi/client/communicator.rb', line 18

def auth
  @auth
end

#identityObject

Returns the value of attribute identity.



19
20
21
# File 'lib/haveapi/client/communicator.rb', line 19

def identity
  @identity
end

#languageObject

Returns the value of attribute language.



18
19
20
# File 'lib/haveapi/client/communicator.rb', line 18

def language
  @language
end

#language_headerObject

Returns the value of attribute language_header.



18
19
20
# File 'lib/haveapi/client/communicator.rb', line 18

def language_header
  @language_header
end

#urlObject (readonly)

Returns the value of attribute url.



18
19
20
# File 'lib/haveapi/client/communicator.rb', line 18

def url
  @url
end

#verify_sslObject (readonly)

Returns the value of attribute verify_ssl.



18
19
20
# File 'lib/haveapi/client/communicator.rb', line 18

def verify_ssl
  @verify_ssl
end

Class Method Details

.register_auth_method(name, klass) ⇒ Object



12
13
14
15
# File 'lib/haveapi/client/communicator.rb', line 12

def register_auth_method(name, klass)
  @auth_methods ||= {}
  @auth_methods[name] = klass
end

Instance Method Details

#auth_saveObject



83
84
85
# File 'lib/haveapi/client/communicator.rb', line 83

def auth_save
  @auth.save
end

#authenticate(auth_method, options = {}, &block) ⇒ Object

Authenticate user with selected auth_method. auth_method is a name of registered authentication provider. options are specific for each authentication provider.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/haveapi/client/communicator.rb', line 71

def authenticate(auth_method, options = {}, &block)
  desc = describe_api(@version)

  @auth = self.class.auth_methods[auth_method].new(
    self,
    desc[:authentication][auth_method],
    options,
    &block
  )
  @rest = @auth.resource || @rest
end

#available_versionsObject



87
88
89
# File 'lib/haveapi/client/communicator.rb', line 87

def available_versions
  description_for(path_for, { describe: :versions })
end

#call(action, params = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/haveapi/client/communicator.rb', line 140

def call(action, params = {})
  args = []
  input_namespace = action.input && action.namespace(:input)
  meta = nil

  if params.is_a?(Hash) && params[:meta]
    meta = params[:meta]
    params.delete(:meta)
  end

  if %w[POST PUT].include?(action.http_method)
    ns = {}
    ns[input_namespace] = params if input_namespace
    ns[:_meta] = meta if meta
    ns.update(@auth.request_payload)

    args << ns.to_json
    args << base_headers(content_type: :json).update(@auth.request_headers)

  elsif %w[GET DELETE].include?(action.http_method)
    get_params = {}

    params.each do |k, v|
      get_params["#{input_namespace}[#{k}]"] =
        v.nil? ? '' : v
    end

    if meta
      meta.each do |k, v|
        get_params["_meta[#{k}]"] = v.nil? ? '' : v # FIXME: read _meta namespace from the description
      end
    end

    args << base_headers(
      params: get_params.update(@auth.request_query_params)
    ).update(@auth.request_headers)
  end

  begin
    response = parse(@rest[action.prepared_path].method(action.http_method.downcase.to_sym).call(*args))
  rescue RestClient::Forbidden
    return error(client_message('errors.access_forbidden'))
  rescue RestClient::ResourceNotFound,
         RestClient::BadRequest => e
    response = parse(e.http_body)
  rescue StandardError => e
    return error(client_message('errors.fatal_api_error', error: e.inspect))
  end

  if response[:status]
    ok(response[:response])

  else
    error(response[:message], response[:errors])
  end
end

#client_message(key, **values) ⇒ Object



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

def client_message(key, **values)
  I18n.t(@language, key, values)
end

#compatible?:compatible, ...

Returns:

  • (:compatible)

    if perfectly compatible

  • (:imperfect)

    if minor version differs

  • (false)

    if not compatible



57
58
59
60
61
62
63
64
65
66
# File 'lib/haveapi/client/communicator.rb', line 57

def compatible?
  description_for(path_for, { describe: :versions })
  if @proto_version == HaveAPI::Client::PROTOCOL_VERSION
    :compatible
  else
    :imperfect
  end
rescue ProtocolError
  false
end

#describe_action(action) ⇒ Object



107
108
109
# File 'lib/haveapi/client/communicator.rb', line 107

def describe_action(action)
  description_for(action.prepared_help)
end

#describe_api(v = nil) ⇒ Object



91
92
93
# File 'lib/haveapi/client/communicator.rb', line 91

def describe_api(v = nil)
  description_for(path_for(v), v.nil? ? { describe: :default } : {})
end

#describe_resource(path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/haveapi/client/communicator.rb', line 95

def describe_resource(path)
  tmp = describe_api(@version)

  path.each do |r|
    tmp = tmp[:resources][r.to_sym]

    return false unless tmp
  end

  tmp
end

#get_action(resources, action, args) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/haveapi/client/communicator.rb', line 111

def get_action(resources, action, args)
  tmp = describe_api(@version)

  resources.each do |r|
    tmp = tmp[:resources][r.to_sym]

    return false unless tmp
  end

  a = tmp[:actions][action]

  unless a # search in aliases
    tmp[:actions].each_value do |v|
      if v[:aliases].include?(action.to_s)
        a = v
        break
      end
    end
  end

  if a
    obj = Action.new(nil, self, action, a, args)
    obj.resource_path = resources
    obj
  else
    false
  end
end

#inspectObject



50
51
52
# File 'lib/haveapi/client/communicator.rb', line 50

def inspect
  "#<#{self.class.name} @url=#{@url} @version=#{@version} @auth=#{@auth.class.name}>"
end

#language_headersObject



42
43
44
# File 'lib/haveapi/client/communicator.rb', line 42

def language_headers
  I18n.request_headers(@language, @language_header)
end