Class: ConnectWiseRest::Client
- Inherits:
-
Object
- Object
- ConnectWiseRest::Client
- Defined in:
- lib/connect_wise_rest/client.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_OPTIONS =
ConnectWiseRest.configuration.to_hash.merge( { headers: {}, query: { 'page' => 1, 'pageSize' => 75 }, timeout: 300, request_options: {} } )
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #data ⇒ Object
- #fetch(query = {}) ⇒ Object
-
#initialize(resource, options = {}) ⇒ Client
constructor
A new instance of Client.
- #next_page ⇒ Object
-
#next_page? ⇒ Boolean
Pagination ###.
- #previous_page ⇒ Object
- #previous_page? ⇒ Boolean
- #request_options ⇒ Object
- #response ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(resource, options = {}) ⇒ Client
Returns a new instance of Client.
16 17 18 19 |
# File 'lib/connect_wise_rest/client.rb', line 16 def initialize(resource, = {}) @resource = resource = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/connect_wise_rest/client.rb', line 4 def end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
5 6 7 |
# File 'lib/connect_wise_rest/client.rb', line 5 def resource @resource end |
Instance Method Details
#data ⇒ Object
21 22 23 |
# File 'lib/connect_wise_rest/client.rb', line 21 def data @data || fetch end |
#fetch(query = {}) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/connect_wise_rest/client.rb', line 25 def fetch(query = {}) self.[:query].merge!(query) @response = nil raise_error(response) unless response.success? @data = response.parsed_response end |
#next_page ⇒ Object
79 80 81 82 |
# File 'lib/connect_wise_rest/client.rb', line 79 def next_page self.[:query]['page'] += 1 return fetch end |
#next_page? ⇒ Boolean
Pagination ###
75 76 77 |
# File 'lib/connect_wise_rest/client.rb', line 75 def next_page? self.data.is_a?(Array) && self.data.count >= self.[:query]['pageSize'] end |
#previous_page ⇒ Object
88 89 90 91 |
# File 'lib/connect_wise_rest/client.rb', line 88 def previous_page self.[:query]['page'] -= 1 return fetch end |
#previous_page? ⇒ Boolean
84 85 86 |
# File 'lib/connect_wise_rest/client.rb', line 84 def previous_page? self.[:query]['page'] > 1 end |
#request_options ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/connect_wise_rest/client.rb', line 40 def = [:request_options] || {} .merge!( headers: { 'Accept' => 'application/json' }.merge([:headers]), query: self.[:query], timeout: [:timeout], basic_auth: { username: "#{options[:company_id]}+#{options[:public_key]}", password: [:private_key] } ) if [:proxy_host] .merge!( http_proxyaddr: [:proxy_host], http_proxyport: [:proxy_port], http_proxyuser: [:proxy_user], http_proxypass: [:proxy_password], ) end if [:debug] && [:logger] [:debug_output] = [:logger] end end |
#response ⇒ Object
34 35 36 37 38 |
# File 'lib/connect_wise_rest/client.rb', line 34 def response @response ||= HTTParty.get(url, ) rescue StandardError => e raise ConnectionError.new(e.) end |
#url ⇒ Object
69 70 71 |
# File 'lib/connect_wise_rest/client.rb', line 69 def url "https://#{options[:url_prefix]}/v4_6_release/apis/#{options[:version]}#{resource}" end |