Class: Reviewed::Client

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

Constant Summary collapse

DEFAULT_BASE_URI =
"http://localhost:3000/api/v1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
# File 'lib/reviewed/client.rb', line 17

def initialize(opts={})
  @base_uri = opts[:base_uri] || config_base_uri
  @api_key = opts[:api_key] || config_api_key
  @request_params = opts[:request_params] || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

args are options passed to request object, for example in: client.attachments(scope: ‘article’) args = [‘article’]



59
60
61
62
63
# File 'lib/reviewed/client.rb', line 59

def method_missing(method, *args, &block)
  opts = { client: self, resource: resource(method) }
  opts = opts.merge!(args[0]) if args[0]
  Reviewed::Request.new(opts)
end

Class Attribute Details

.api_base_uriObject

Returns the value of attribute api_base_uri.



8
9
10
# File 'lib/reviewed/client.rb', line 8

def api_base_uri
  @api_base_uri
end

.api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/reviewed/client.rb', line 8

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



8
9
10
# File 'lib/reviewed/client.rb', line 8

def api_version
  @api_version
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/reviewed/client.rb', line 3

def api_key
  @api_key
end

#base_uriObject

Returns the value of attribute base_uri.



3
4
5
# File 'lib/reviewed/client.rb', line 3

def base_uri
  @base_uri
end

#request_paramsObject

Returns the value of attribute request_params.



3
4
5
# File 'lib/reviewed/client.rb', line 3

def request_params
  @request_params
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
# File 'lib/reviewed/client.rb', line 10

def configure
  yield self
  self
end

Instance Method Details

#config_api_keyObject



27
28
29
# File 'lib/reviewed/client.rb', line 27

def config_api_key
  self.class.api_key || ENV['REVIEWED_API_KEY']
end

#config_base_uriObject



23
24
25
# File 'lib/reviewed/client.rb', line 23

def config_base_uri
  self.class.api_base_uri || DEFAULT_BASE_URI
end

#connectionObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/reviewed/client.rb', line 65

def connection
  @connection ||= ::Faraday.new(url: base_uri) do |faraday|
    faraday.response :mashify
    faraday.response :errors
    faraday.response :json
    faraday.request  :api_key
    faraday.request  :url_encoded
    faraday.adapter  Faraday.default_adapter
  end
end

#delete(path, params = {}) ⇒ Object

Perform an HTTP DELETE request



47
48
49
# File 'lib/reviewed/client.rb', line 47

def delete(path, params={})
  perform(:delete, path, params)
end

#get(path, params = {}) ⇒ Object

Perform an HTTP GET request



32
33
34
# File 'lib/reviewed/client.rb', line 32

def get(path, params={})
  perform(:get, path, params)
end

#post(path, params = {}) ⇒ Object

Perform an HTTP DELETE request



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

def post(path, params={})
  perform(:post, path, params)
end

#put(path, params = {}) ⇒ Object

Perform an HTTP PUT request



37
38
39
# File 'lib/reviewed/client.rb', line 37

def put(path, params={})
  perform(:put, path, params)
end

#resource(name) ⇒ Object



51
52
53
54
# File 'lib/reviewed/client.rb', line 51

def resource(name)
  klass_string = "Reviewed::#{name.to_s.singularize.classify}"
  klass_string.constantize rescue name
end