Class: Moip2::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/moip2/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = :sandbox, auth = nil, opts = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
# File 'lib/moip2/client.rb', line 8

def initialize(env = :sandbox, auth = nil, opts = {})
  @env, @auth, @opts = env.to_sym, auth, opts

  @uri = get_base_uri
  self.class.base_uri @uri
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



6
7
8
# File 'lib/moip2/client.rb', line 6

def auth
  @auth
end

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/moip2/client.rb', line 6

def env
  @env
end

#uriObject (readonly)

Returns the value of attribute uri.



6
7
8
# File 'lib/moip2/client.rb', line 6

def uri
  @uri
end

Instance Method Details

#get(path) ⇒ Object



50
51
52
53
54
# File 'lib/moip2/client.rb', line 50

def get(path)
  resp = self.class.get path, opts()

  create_response resp
end

#optsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/moip2/client.rb', line 23

def opts
  opts = @opts
  opts[:headers] ||= {}

  opts[:headers].merge!(
      {
        "Content-Type" => "application/json",
        "Authorization" => auth.header
      }
  )

  opts
end

#post(path, resource) ⇒ Object



37
38
39
40
41
# File 'lib/moip2/client.rb', line 37

def post(path, resource)
  options = opts().merge(body: convert_hash_keys_to(:camel_case, resource).to_json)
  resp = self.class.post path, options
  create_response resp
end

#production?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/moip2/client.rb', line 19

def production?
  env == :production
end

#put(path, resource) ⇒ Object



43
44
45
46
47
48
# File 'lib/moip2/client.rb', line 43

def put(path, resource)
  options = opts().merge(body: convert_hash_keys_to(:camel_case, resource).to_json)
  resp = self.class.put path, options

  create_response resp
end

#sandbox?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/moip2/client.rb', line 15

def sandbox?
  env == :sandbox
end