Class: Fulcrum::Api
- Inherits:
-
Object
show all
- Defined in:
- lib/fulcrum/api.rb
Defined Under Namespace
Classes: Configuration
Constant Summary
collapse
- VALID_METHODS =
[:get, :post, :put, :delete]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Attribute Details
#configuration=(value) ⇒ Object
Sets the attribute configuration
15
16
17
|
# File 'lib/fulcrum/api.rb', line 15
def configuration=(value)
@configuration = value
end
|
#connection ⇒ Object
Returns the value of attribute connection.
13
14
15
|
# File 'lib/fulcrum/api.rb', line 13
def connection
@connection
end
|
#response ⇒ Object
Returns the value of attribute response.
14
15
16
|
# File 'lib/fulcrum/api.rb', line 14
def response
@response
end
|
Class Method Details
.call(method = :get, path = '', params = {}) ⇒ Object
38
39
40
41
42
43
44
45
|
# File 'lib/fulcrum/api.rb', line 38
def call(method = :get, path = '', params = {})
raise ArgumentError, "Invalid method: #{method.to_s}" unless VALID_METHODS.include?(method.to_sym)
@response = connection.send(method.to_sym, path, params)
@response.body
rescue Faraday::Error::ClientError => e
@response = e.response
{ error: { status: @response[:status], message: @response[:body] } }
end
|
.configuration ⇒ Object
51
52
53
|
# File 'lib/fulcrum/api.rb', line 51
def configuration
@configuration ||= Configuration.new
end
|
47
48
49
|
# File 'lib/fulcrum/api.rb', line 47
def configure
yield(configuration)
end
|
.connection ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/fulcrum/api.rb', line 55
def connection
if !@connection
@connection = Faraday.new(Fulcrum::Api.configuration.uri) do |b|
b.request :multipart
b.request :json
b.response :raise_error
b.response :json , :content_type => 'application/json'
b.adapter Faraday.default_adapter
end
@connection.['X-ApiToken'] = Fulcrum::Api.configuration.key
@connection.['User-Agent'] = "Ruby Fulcrum API Client, Version #{Fulcrum::VERSION}"
end
@connection
end
|
.get_key(username, password) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/fulcrum/api.rb', line 70
def get_key(username, password)
conn = Faraday.new(uri) do |b|
b.request :url_encoded
b.response :raise_error
b.response :json, :content_type => "application/json"
b.adapter Faraday.default_adapter
end
conn.['User-Agent'] = "Ruby Fulcrum API Client version #{Fulcrum::VERSION}"
conn.basic_auth(username, password)
resp = conn.get('users.json')
body = JSON.parse(resp.body)
if body['user']
body['user']['api_token']
else
nil
end
end
|
.key ⇒ Object
19
20
21
|
# File 'lib/fulcrum/api.rb', line 19
def key
@configuration.key
end
|
.parse_opts(keys = [], opts = {}) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/fulcrum/api.rb', line 31
def parse_opts(keys = [], opts = {})
opts = opts.with_indifferent_access
{}.tap do |p|
keys.each { |key| p[key.to_sym] = opts[key] if opts.has_key?(key) }
end
end
|
.response ⇒ Object
27
28
29
|
# File 'lib/fulcrum/api.rb', line 27
def response
@response
end
|
.uri ⇒ Object
23
24
25
|
# File 'lib/fulcrum/api.rb', line 23
def uri
@configuration.uri
end
|