Module: PreventGo

Defined in:
lib/prevent_go/identity.rb,
lib/prevent_go.rb,
lib/prevent_go/base.rb,
lib/prevent_go/version.rb,
lib/prevent_go/tax_notice.rb,
lib/prevent_go/bank_account.rb,
lib/prevent_go/configuration.rb,
lib/prevent_go/response_error.rb,
lib/prevent_go/provider_invoice.rb,
lib/prevent_go/property_tax_notice.rb

Overview

Params to pass to bank accounts: holder: { “firstName”:“James”,“lastName”:“Bond”,“birthName”: “Martinet”,“address”: rue du Cheval blanc”, “postalCode”:“34000”, “city”:“Montpellier”} file_1: file => required file_2: file => optional

Defined Under Namespace

Classes: BankAccount, Base, Configuration, Identity, PropertyTaxNotice, ProviderInvoice, ResponseError, TaxNotice

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.api_root_urlObject



28
29
30
# File 'lib/prevent_go.rb', line 28

def api_root_url
  configuration.api_root_url
end

.api_uri(path = '') ⇒ Object



32
33
34
# File 'lib/prevent_go.rb', line 32

def api_uri(path='')
  URI(api_root_url + path)
end

.concat_params(files, params = {}) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/prevent_go.rb', line 65

def concat_params(files, params={})
  params = params.each { |k, v| params[k] = JSON.dump(v) }
  files.compact.each.with_index(1) do |file, index|
    params["file_#{index}"] = UploadIO.new(file, detect_mime_type_for(file))
  end
  params
end

.configurationObject



20
21
22
# File 'lib/prevent_go.rb', line 20

def configuration
  @configuration ||= PreventGo::Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



24
25
26
# File 'lib/prevent_go.rb', line 24

def configure
  yield(configuration)
end

.detect_mime_type_for(file) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/prevent_go.rb', line 53

def detect_mime_type_for(file)
  if file.respond_to?(:content_type)
    file.content_type
  elsif file.respond_to?(:path)
    MIME::Types.type_for(File.basename(file.path))[0]
  elsif file.is_a?(String)
    MIME::Types.type_for(File.basename(file))[0]
  else
    ''
  end
end

.request(url, params = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/prevent_go.rb', line 36

def request(url, params={})
  uri = api_uri(url)

  res = Net::HTTP.start(uri.host, uri.port, use_ssl: true, read_timeout: 1000) do |http|
    req = Net::HTTP::Post::Multipart.new(uri.request_uri, params)
    req.basic_auth(ENV['PREVENT_GO_ID'], ENV['PREVENT_GO_SECRET'])
    http.request(req)
  end

  # decode json data
  data = res.body.to_s.empty? ? {} : JSON.parse(res.body.to_s)

  raise PreventGo::ResponseError.new(uri, res.code, data) unless res.is_a?(Net::HTTPOK)

  data
end