Class: BS2::Connection

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

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



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

def initialize
  # configuration.validate_config!
end

Instance Method Details

#cancel_billet(id, reason) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bs2.rb', line 75

def cancel_billet(id, reason)
  connector = create_connector

  params = {
    justificativa: reason
  }

  resp = connector.post("/pj/forintegration/cobranca/v1/boletos/#{id}/solicitacoes/cancelamentos", params)

  resp.body
end

#create_billet(data) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/bs2.rb', line 91

def create_billet(data)
  connector = create_connector

  resp = connector.post("/pj/forintegration/cobranca/v1/boletos/simplificado", data)

  return false unless resp.success?

  resp.body
end

#create_billet_v2(data) ⇒ Object



87
88
89
# File 'lib/bs2.rb', line 87

def create_billet_v2(data)
  create_connector.post("/pj/forintegration/cobranca/v2/boletos/simplificado", data)
end

#create_connector(json = true) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bs2.rb', line 44

def create_connector(json = true)
  create_token

  Faraday.new(BS2.configuration.endpoint) do |conn|
    conn.request :json
    conn.response :json if json
    conn.response :logger, BS2.configuration.logger, { headers: true, bodies: json }
    conn.adapter Faraday.default_adapter

    conn.authorization("Bearer", @access_token)
  end
end

#create_tokenObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bs2.rb', line 101

def create_token
  # TODO check if is expired
  connection = Faraday.new(BS2.configuration.endpoint) do |conn|
    conn.basic_auth(BS2.configuration.api_key, BS2.configuration.api_secret)
    conn.response :json
    conn.adapter Faraday.default_adapter
    conn.response :logger, BS2.configuration.logger, { headers: true, bodies: true }
  end

  # TODO: test connection errors
  response = connection.post("/auth/oauth/v2/token", {
    grant_type: "password",
    scope: "forintegration",
    username: BS2.configuration.username,
    password: BS2.configuration.password
  }.map { |(key, value)| "#{key}=#{value}" }.join("&"), { 'Accept': "application/json" })

  body = response.body
  @access_token = body.fetch("access_token")
  @token_type = body.fetch("token_type")
  @expires_in = Time.now + body.fetch("expires_in").to_i
  @refresh_token = body.fetch("refresh_token")

  true
end

#fetch_billet(id) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/bs2.rb', line 67

def fetch_billet(id)
  connector = create_connector

  resp = connector.get("/pj/forintegration/cobranca/v1/boletos/#{id}")

  resp.body
end

#generate_pdf(id) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/bs2.rb', line 57

def generate_pdf(id)
  connector = create_connector(false)

  response = connector.get("/pj/forintegration/cobranca/v1/boletos/#{id}/imprimivel")

  return false unless response.success?

  response.body
end