Class: NimbleApi::Base
- Inherits:
-
Object
- Object
- NimbleApi::Base
- Defined in:
- lib/nimble/base.rb
Instance Method Summary collapse
- #base_url ⇒ Object
- #contact ⇒ Object
- #contacts ⇒ Object
- #delete(endpoint) ⇒ Object
- #get(endpoint, params = {}) ⇒ Object
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
- #post(endpoint, params) ⇒ Object
- #put(endpoint, params) ⇒ Object
- #refresh ⇒ Object
Constructor Details
#initialize(options) ⇒ Base
Returns a new instance of Base.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/nimble/base.rb', line 4 def initialize() NimbleApi.client_id = .fetch(:client_id) { raise ArgumentError.new(":client_id is a required argument to initialize NimbleApi") if NimbleApi.client_id.nil?} NimbleApi.client_secret = .fetch(:client_secret) { raise ArgumentError.new(":client_secret is a required argument to initialize NimbleApi") if NimbleApi.client_secret.nil?} NimbleApi.refresh_token = .fetch(:refresh_token) { raise ArgumentError.new(":refresh_token is a required argument to initialize NimbleApi") if NimbleApi.refresh_token.nil?} NimbleApi.host = .fetch(:host) {"api.nimble.com"} NimbleApi.callback_path = .fetch(:callback_path) { "/auth/nimble/callback" } NimbleApi.api_version = .fetch(:api_version) { "v1" } @conn = Faraday.new(:url => "https://#{NimbleApi.host}" ) do |faraday| faraday.request :url_encoded # form-encode POST params faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end self.refresh end |
Instance Method Details
#base_url ⇒ Object
20 21 22 |
# File 'lib/nimble/base.rb', line 20 def base_url "https://#{NimbleApi.host}/api/#{NimbleApi.api_version}" end |
#contact ⇒ Object
73 74 75 |
# File 'lib/nimble/base.rb', line 73 def contact NimbleApi::Contact.new(self) end |
#contacts ⇒ Object
69 70 71 |
# File 'lib/nimble/base.rb', line 69 def contacts NimbleApi::Contacts.new(self) end |
#delete(endpoint) ⇒ Object
63 64 65 66 67 |
# File 'lib/nimble/base.rb', line 63 def delete endpoint resp = @conn.delete "#{base_url}/#{endpoint}" raise NimbleApi::Error.new(resp) if resp['errors'] return JSON resp.body end |
#get(endpoint, params = {}) ⇒ Object
37 38 39 40 41 |
# File 'lib/nimble/base.rb', line 37 def get endpoint, params={} resp = @conn.get "#{base_url}/#{endpoint}", params raise NimbleApi::Error.new(resp) if resp['errors'] return JSON resp.body end |
#post(endpoint, params) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/nimble/base.rb', line 43 def post endpoint, params resp = @conn.post do |req| req.url "#{base_url}/#{endpoint}" req.headers['Content-Type'] = 'application/json' req.body = params.to_json end raise NimbleApi::Error.new(resp) if resp['errors'] return JSON resp.body end |
#put(endpoint, params) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/nimble/base.rb', line 53 def put endpoint, params resp = @conn.put do |req| req.url "#{base_url}/#{endpoint}" req.headers['Content-Type'] = 'application/json' req.body = params.to_json end raise NimbleApi::Error.new(resp) if resp['errors'] return JSON resp.body end |
#refresh ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/nimble/base.rb', line 24 def refresh params = { client_id: NimbleApi.client_id, client_secret: NimbleApi.client_secret, grant_type: 'refresh_token', refresh_token: NimbleApi.refresh_token, redirect_uri: NimbleApi.callback_path } resp = @conn.post '/oauth/token', params access_token = (JSON resp.body)['access_token'] @conn.headers = { 'Authorization' => "Bearer #{access_token}" } end |