Class: Intelipost::Client

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

Constant Summary collapse

HOST =
'api.intelipost.com.br'
SERVICE =
'api/'
API_VERSION =
'v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/intelipost/client.rb', line 10

def initialize(options)
  @options = { ssl: true }
  @options.merge! options
  uri = @options[:ssl] ? 'https://' : 'http://'
  uri.concat HOST

  @uri = URI.join uri, SERVICE, API_VERSION
  @api_key = @options[:api_key]
  @platform = @options[:platform]

  @connection = connection
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



48
49
50
51
52
53
54
55
# File 'lib/intelipost/client.rb', line 48

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_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#connectionObject (readonly)

Returns the value of attribute connection.

Raises:

  • (ArgumentError)


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

def connection
  @connection
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#platformObject (readonly)

Returns the value of attribute platform.



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

def platform
  @platform
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#camelize(term) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/intelipost/client.rb', line 57

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



40
41
42
# File 'lib/intelipost/client.rb', line 40

def get(endpoint, args={})
  connection.get(endpoint, args).body
end

#post(endpoint, args = {}) ⇒ Object



44
45
46
# File 'lib/intelipost/client.rb', line 44

def post(endpoint, args = {})
  connection.post(endpoint, args).body
end