Class: Orias::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/orias/search.rb

Overview

Dedicated to search request building

Constant Summary collapse

VALID_INTERMEDIARIES_TYPE =
{ siren: 9, registrationNumber: 8 }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Search

Initialize an Orias::Search instance



12
13
14
15
# File 'lib/orias/search.rb', line 12

def initialize(attributes = {})
  super
  @client ||= Orias::Client.new
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/orias/search.rb', line 9

def client
  @client
end

Class Method Details

.set_terms(type, terms) ⇒ Object

Check & Set an intermediaries list



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/orias/search.rb', line 92

def set_terms(type, terms)
  terms.map!{ |t| t.to_s.gsub(/\D/,'') }
  lgts = terms.map(&:length).uniq
  valid_lgth = VALID_INTERMEDIARIES_TYPE[type]

  unless lgts.length == 1 && lgts.first == valid_lgth
    raise "Terms for \"#{type}\" must all have a length of #{valid_lgth}."
  end

  terms
end

.set_type(type, terms) ⇒ Object

Check & Set the type for an intermediaries list



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/orias/search.rb', line 77

def set_type(type, terms)
  type = :registrationNumber if type == :orias

  return type if VALID_INTERMEDIARIES_TYPE.key?(type)

  lgts = terms.map(&:length).uniq

  unless lgts.length == 1 && VALID_INTERMEDIARIES_TYPE.invert[lgts.first]
    raise "Orias::Search - Unknown Type Error (\"#{type}\")."
  end

  VALID_INTERMEDIARIES_TYPE.invert[lgts.first]
end

Instance Method Details

#find_by(type, terms) ⇒ Object

Request building for intermediarySearchRequest



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/orias/search.rb', line 28

def find_by(type, terms)
  responses = [terms].flatten.each_slice(@client.per_request).map do |term_collection|
    request = Orias::Request.new(
      api_endpoint: @client.api_endpoint,
      body: raw_find(raw_intermediaries(type, term_collection))
    ).build!

    Orias::Response.new(
      type: :intermediary_search,
      raw: request.response.body
    )
  end

  Orias::Response.merge(responses)
end

#find_by_orias(terms) ⇒ Object

Alias for #find_by with an :orias type



18
19
20
# File 'lib/orias/search.rb', line 18

def find_by_orias(terms)
  find_by(:orias, terms)
end

#find_by_siren(terms) ⇒ Object

Alias for #find_by with an :siren type



23
24
25
# File 'lib/orias/search.rb', line 23

def find_by_siren(terms)
  find_by(:siren, terms)
end

#raw_body(content) ⇒ Object

Build the raw request body of a search



45
46
47
48
49
50
51
# File 'lib/orias/search.rb', line 45

def raw_body(content)
  xmlns_url = 'http://schemas.xmlsoap.org/soap/envelope/'

  output = "<soapenv:Envelope xmlns:soapenv=\"#{xmlns_url}\">"
  output += "<soapenv:Body>#{content}</soapenv:Body>"
  output + '</soapenv:Envelope>'
end

#raw_find(raw_intermeds) ⇒ Object

Build the raw search request of a search



54
55
56
57
58
59
60
61
# File 'lib/orias/search.rb', line 54

def raw_find(raw_intermeds)
  content = '<intermediarySearchRequest xmlns="urn:gpsa:orias:ws.001">'
  content += "<user xmlns=\"\">#{@client.private_key}</user>"
  content += "<intermediaries xmlns=\"\">#{raw_intermeds}</intermediaries>"
  content += '</intermediarySearchRequest>'

  raw_body(content)
end

#raw_intermediaries(type, terms) ⇒ Object

Build the raw intermediaries list of a search



64
65
66
67
68
69
70
71
72
73
# File 'lib/orias/search.rb', line 64

def raw_intermediaries(type, terms)
  raise 'Orias::Search - You must at least submit one term.' if terms.empty?

  type = Search.set_type(type, terms)
  terms = Search.set_terms(type, terms)

  terms.flatten.uniq.compact.map do |term|
    "<intermediary><#{type}>#{term}</#{type}></intermediary>\n"
  end.join
end