Class: Patentscope::Client

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

Constant Summary collapse

USER_AGENT_STRING =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1"
OPEN_TIMEOUT =
10
READ_TIMEOUT =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(args = {})
  @username = args[:username]
  @password = args[:password]
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#get_url(url) ⇒ Object



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

def get_url(url)
  URI.open(url, "User-Agent" => USER_AGENT_STRING, http_basic_authentication: [username, password]).read
end

#post_url(url, content_type = 'text/html', body = '') ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/patentscope/client.rb', line 23

def post_url(url, content_type = 'text/html', body = '')
  uri                     = URI(url)
  request                 = Net::HTTP::Post.new(uri)
  request.basic_auth(username, password)
  request["User-Agent"]   = USER_AGENT_STRING
  request["Content-Type"] = content_type
  request.body            = body

  Net::HTTP.start(uri.host, uri.port,
                  use_ssl: uri.scheme == 'https',
                  open_timeout: OPEN_TIMEOUT,
                  read_timeout: READ_TIMEOUT) do |http|
    response = http.request(request)
    raise WrongCredentialsError if response.is_a?(Net::HTTPUnauthorized)

    body = response.body.to_s.force_encoding("ISO-8859-1").encode("UTF-8")
    content_type = response.header["Content-Type"].to_s

    if content_type.include?("multipart/related")
      multipart_xml_body(body)
    else
      body
    end
  end
end