Class: HaveAPI::Client::Communicator
- Inherits:
-
Object
- Object
- HaveAPI::Client::Communicator
- Defined in:
- lib/haveapi/client/communicator.rb
Class Attribute Summary collapse
-
.auth_methods ⇒ Object
readonly
Returns the value of attribute auth_methods.
Instance Attribute Summary collapse
-
#identity ⇒ Object
Returns the value of attribute identity.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #auth_save ⇒ Object
-
#authenticate(auth_method, options = {}) ⇒ Object
Authenticate user with selected
auth_method
. - #available_versions ⇒ Object
- #call(action, params, raw: false) ⇒ Object
- #describe_action(action) ⇒ Object
- #describe_api(v = nil) ⇒ Object
- #describe_resource(path) ⇒ Object
- #get_action(resources, action, args) ⇒ Object
-
#initialize(url, v = nil) ⇒ Communicator
constructor
A new instance of Communicator.
Constructor Details
#initialize(url, v = nil) ⇒ Communicator
Returns a new instance of Communicator.
22 23 24 25 26 27 28 29 |
# File 'lib/haveapi/client/communicator.rb', line 22 def initialize(url, v = nil) @url = url @auth = Authentication::NoAuth.new(self, {}, {}) @rest = RestClient::Resource.new(@url) @version = v @identity = 'haveapi-client-ruby' @desc = {} end |
Class Attribute Details
.auth_methods ⇒ Object (readonly)
Returns the value of attribute auth_methods.
11 12 13 |
# File 'lib/haveapi/client/communicator.rb', line 11 def auth_methods @auth_methods end |
Instance Attribute Details
#identity ⇒ Object
Returns the value of attribute identity.
20 21 22 |
# File 'lib/haveapi/client/communicator.rb', line 20 def identity @identity end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
19 20 21 |
# File 'lib/haveapi/client/communicator.rb', line 19 def url @url end |
Class Method Details
.register_auth_method(name, klass) ⇒ Object
13 14 15 16 |
# File 'lib/haveapi/client/communicator.rb', line 13 def register_auth_method(name, klass) @auth_methods ||= {} @auth_methods[name] = klass end |
Instance Method Details
#auth_save ⇒ Object
41 42 43 |
# File 'lib/haveapi/client/communicator.rb', line 41 def auth_save @auth.save end |
#authenticate(auth_method, options = {}) ⇒ Object
Authenticate user with selected auth_method
. auth_method
is a name of registered authentication provider. options
are specific for each authentication provider.
34 35 36 37 38 39 |
# File 'lib/haveapi/client/communicator.rb', line 34 def authenticate(auth_method, = {}) desc = describe_api(@version) @auth = self.class.auth_methods[auth_method].new(self, desc[:authentication][auth_method], ) @rest = @auth.resource || @rest end |
#available_versions ⇒ Object
45 46 47 |
# File 'lib/haveapi/client/communicator.rb', line 45 def available_versions description_for(path_for, {describe: :versions}) end |
#call(action, params, raw: false) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/haveapi/client/communicator.rb', line 100 def call(action, params, raw: false) args = [] input_namespace = action.namespace(:input) = nil if params.is_a?(Hash) && params[:meta] = params[:meta] params.delete(:meta) end if %w(POST PUT).include?(action.http_method) ns = {input_namespace => params} ns[:_meta] = if ns.update(@auth.request_payload) args << ns.to_json args << {content_type: :json, accept: :json, user_agent: @identity}.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 end .each do |k, v| get_params["_meta[#{k}]"] = v # FIXME: read _meta namespace from the description end if args << {params: get_params.update(@auth.request_url_params), accept: :json, user_agent: @identity}.update(@auth.request_headers) end begin response = parse(@rest[action.prepared_url].method(action.http_method.downcase.to_sym).call(*args)) rescue RestClient::Forbidden return error('Access forbidden. Bad user name or password? Not authorized?') rescue => e return error("Fatal API error: #{e.inspect}") end if response[:status] if raw ok(JSON.pretty_generate(response[:response])) else ok(response[:response]) end else error(response[:message], response[:errors]) end end |
#describe_action(action) ⇒ Object
67 68 69 |
# File 'lib/haveapi/client/communicator.rb', line 67 def describe_action(action) description_for(action.prepared_help) end |
#describe_api(v = nil) ⇒ Object
49 50 51 52 53 |
# File 'lib/haveapi/client/communicator.rb', line 49 def describe_api(v=nil) return @desc[v] if @desc.has_key?(v) @desc[v] = description_for(path_for(v), v.nil? ? {describe: :default} : {}) end |
#describe_resource(path) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/haveapi/client/communicator.rb', line 55 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/haveapi/client/communicator.rb', line 71 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 do |_, v| if v[:aliases].include?(action.to_s) a = v break end end end if a obj = Action.new(self, action, a, args) obj.resource_path = resources obj else false end end |