Class: Patentscope::Client
- Inherits:
-
Object
- Object
- Patentscope::Client
- 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"
Instance Attribute Summary collapse
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #get_url(url) ⇒ Object
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
- #post_url(url, content_type = 'text/html', body = '') ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
11 12 13 14 |
# File 'lib/patentscope/client.rb', line 11 def initialize(args = {}) @username = args[:username] @password = args[:password] end |
Instance Attribute Details
#password ⇒ Object (readonly)
Returns the value of attribute password.
7 8 9 |
# File 'lib/patentscope/client.rb', line 7 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
7 8 9 |
# File 'lib/patentscope/client.rb', line 7 def username @username end |
Instance Method Details
#get_url(url) ⇒ Object
16 17 18 |
# File 'lib/patentscope/client.rb', line 16 def get_url(url) open(url, "User-Agent" => USER_AGENT_STRING, http_basic_authentication: [username, password]).read end |
#post_url(url, content_type = 'text/html', body = '') ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/patentscope/client.rb', line 20 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: true) do |http| response = http.request(request) response.body = response.body.force_encoding("ISO-8859-1").encode("UTF-8") if response.header["Content-Type"].include? "text/html" response.body elsif response.header["Content-Type"].include? "multipart/related" response.body.split("\r\n\r\n")[1].split("\r\n")[0] end end end |