Class: Intelipost::Client
- Inherits:
-
Object
- Object
- Intelipost::Client
- Defined in:
- lib/intelipost/client.rb
Constant Summary collapse
- HOST =
'api.intelipost.com.br'
- SERVICE =
'api/'
- API_VERSION =
'v1'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #camelize(term) ⇒ Object
- #get(endpoint, args = {}) ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #method_missing(method, *args, &block) ⇒ Object
- #post(endpoint, args = {}) ⇒ Object
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/intelipost/client.rb', line 8 def initialize() @options = {ssl: true} @options.merge! uri = @options[:ssl] ? 'https://' : 'http://' uri.concat HOST @uri = URI.join uri, SERVICE, API_VERSION @api_key = @options[:api_key] @connection = connection end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/intelipost/client.rb', line 45 def method_missing(method, *args, &block) method = camelize method if Intelipost.const_defined? method return Intelipost.const_get(method).new self else super end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
6 7 8 |
# File 'lib/intelipost/client.rb', line 6 def api_key @api_key end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
6 7 8 |
# File 'lib/intelipost/client.rb', line 6 def connection @connection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/intelipost/client.rb', line 6 def @options end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
6 7 8 |
# File 'lib/intelipost/client.rb', line 6 def uri @uri end |
Instance Method Details
#camelize(term) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/intelipost/client.rb', line 54 def camelize(term) string = term.to_s string = string.sub(/^[a-z\d]*/) { $&.capitalize } string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" } string.gsub!('/', '::') string end |
#get(endpoint, args = {}) ⇒ Object
37 38 39 |
# File 'lib/intelipost/client.rb', line 37 def get(endpoint, args={}) connection.get(endpoint, args).body end |
#post(endpoint, args = {}) ⇒ Object
41 42 43 |
# File 'lib/intelipost/client.rb', line 41 def post(endpoint, args = {}) connection.post(endpoint, args).body end |