resource_accessor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ pkg
6
+ rdoc
7
+ tmp
8
+ .idea/*.*
9
+ *.gem
10
+
@@ -0,0 +1,3 @@
1
+ <component name="ProjectDictionaryState">
2
+ <dictionary name="alex" />
3
+ </component>
@@ -0,0 +1,5 @@
1
+ <component name="DependencyValidationManager">
2
+ <state>
3
+ <option name="SKIP_IMPORT_STATEMENTS" value="false" />
4
+ </state>
5
+ </component>
data/.rvmrc ADDED
@@ -0,0 +1,4 @@
1
+ rvm_gemset_create_on_use_flag=1
2
+ rvm_pretty_print_flag=1
3
+
4
+ rvm use ruby-1.9.3@resource_accessor
data/CHANGES ADDED
@@ -0,0 +1,5 @@
1
+ = Resource Accessor Changelog
2
+
3
+ == Version 1.0.0
4
+
5
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source :rubygems
2
+
3
+ group :development do
4
+ gem "gemspec_deps_gen"
5
+ gem "gemcutter"
6
+ end
7
+
8
+ group :test do
9
+ gem "rspec"
10
+ gem "rspec-core"
11
+ gem "rspec-expectations"
12
+ gem "mocha"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ file_utils (1.0.6)
6
+ gemcutter (0.7.1)
7
+ gemspec_deps_gen (1.0.5)
8
+ file_utils
9
+ metaclass (0.0.1)
10
+ mocha (0.13.2)
11
+ metaclass (~> 0.0.1)
12
+ rspec (2.12.0)
13
+ rspec-core (~> 2.12.0)
14
+ rspec-expectations (~> 2.12.0)
15
+ rspec-mocks (~> 2.12.0)
16
+ rspec-core (2.12.2)
17
+ rspec-expectations (2.12.1)
18
+ diff-lcs (~> 1.1.3)
19
+ rspec-mocks (2.12.2)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ gemcutter
26
+ gemspec_deps_gen
27
+ mocha
28
+ rspec
29
+ rspec-core
30
+ rspec-expectations
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alexander Shvets
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # ResourceAccessor - This library is used to simplify access to protected or unprotected http resource
2
+
3
+ ## Installation
4
+
5
+ Add this line to to your Gemfile:
6
+
7
+ gem "resource_accessor"
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ require 'resource_accessor'
17
+
18
+ accessor = ResourceAccessor.new
19
+
20
+ # to get unprotected resource
21
+ response = accessor.get_response :url => some_url
22
+
23
+ # to get cookie
24
+ cookie = accessor.get_cookie login_url, user_name, password
25
+
26
+ # to get protected resource through POST and post body as hash
27
+ response = accessor.get_response :url => some_url, :method => :post, :cookie => cookie,
28
+ :body => some_hash
29
+
30
+ # to get protected resource through POST and post body as string
31
+ response = accessor.get_response :url => some_url, :method => :post, :cookie => cookie,
32
+ :body => some_string
33
+
34
+ # to get AJAX resource
35
+ response = accessor.get_ajax_response :url => some_url
36
+ # or
37
+ response = accessor.get_response {:url => some_url}, {'X-Requested-With' => 'XMLHttpRequest'}
38
+
39
+ # to get SOAP resource
40
+ response = accessor.get_soap_response :url => some_url
41
+ # or
42
+ response = accessor.get_response {:url => some_url}, {'SOAPAction' => 'someSoapOperation', 'Content-Type' => 'text/xml;charset=UTF-8'}
43
+
44
+ # to get JSON resource
45
+ response = accessor.get_response {:url => some_url}, {'Content-Type" => "application/json;charset=UTF-'}
46
+
47
+
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env rake
2
+
3
+ $LOAD_PATH.unshift File.expand_path("lib", File.dirname(__FILE__))
4
+
5
+ require "rspec/core/rake_task"
6
+ require "resource_accessor/version"
7
+ require "gemspec_deps_gen/gemspec_deps_gen"
8
+
9
+ def version
10
+ ResourceAccessor::VERSION
11
+ end
12
+
13
+ def project_name
14
+ File.basename(Dir.pwd)
15
+ end
16
+
17
+ task :build do
18
+ system "rm #{project_name}.gemspec"
19
+ generator = GemspecDepsGen.new
20
+
21
+ generator.generate_dependencies "#{project_name}.gemspec.erb", "#{project_name}.gemspec"
22
+
23
+ system "gem build #{project_name}.gemspec"
24
+ end
25
+
26
+ task :install do
27
+ system "gem install #{project_name}-#{version}.gem"
28
+ end
29
+
30
+ task :release => :build do
31
+ system "gem push #{project_name}-#{version}.gem"
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new do |task|
35
+ task.pattern = 'spec/**/*_spec.rb'
36
+ task.verbose = false
37
+ end
@@ -0,0 +1,109 @@
1
+ require 'net/https'
2
+
3
+ require 'system_timer' if RUBY_VERSION.to_f < 1.9 and RUBY_PLATFORM != 'java'
4
+
5
+ class ResourceAccessor
6
+ include SystemTimer if RUBY_VERSION.to_f < 1.9 and RUBY_PLATFORM != 'java'
7
+
8
+ def get_response params, headers = {}
9
+ locate_response(params[:url], params[:method], headers, params[:body], params[:cookie])
10
+ end
11
+
12
+ def get_soap_response params, headers = {}
13
+ headers["SOAPAction"] = params[:soap_action] if params[:soap_action]
14
+ headers["SOAPAction"] = "" unless headers["SOAPAction"]
15
+ headers["Content-Type"] = "text/xml;charset=UTF-8" unless headers["Content-Type"]
16
+
17
+ locate_response(params[:url], params[:method], headers, params[:body], params[:cookie])
18
+ end
19
+
20
+ def get_ajax_response params, headers = {}
21
+ headers['X-Requested-With'] = 'XMLHttpRequest'
22
+
23
+ locate_response(params[:url], params[:method], headers, params[:body], params[:cookie])
24
+ end
25
+
26
+ def get_cookie url, user_name, password
27
+ headers = {"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"}
28
+
29
+ body = {:username => user_name, :password => password}
30
+
31
+ response = locate_response(url, :post, headers, body)
32
+
33
+ response.response['set-cookie']
34
+ end
35
+
36
+ private
37
+
38
+ def locate_response url, method, headers, body, cookie=nil
39
+ response = execute_request url, method, headers, body, cookie
40
+
41
+ if response.class == Net::HTTPMovedPermanently
42
+ response = execute_request response['location'], method, headers, body, cookie
43
+ end
44
+
45
+ response
46
+ end
47
+
48
+ def execute_request url, method, headers, body, cookie=nil
49
+ headers["User-Agent"] = "Ruby/#{RUBY_VERSION} (Macintosh; Intel Mac OS X 10.8)" unless headers["User-Agent"]
50
+ headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8" unless headers["Content-Type"]
51
+
52
+ if cookie
53
+ headers['Cookie'] = cookie
54
+ end
55
+
56
+ uri = URI.parse(URI.escape(url))
57
+
58
+ connection = Net::HTTP.new(uri.host, uri.port)
59
+
60
+ if uri.scheme == "https"
61
+ connection.use_ssl = true
62
+
63
+ if validate_ssl_cert?
64
+ connection.ca_file = ca_file
65
+ connection.verify_mode = OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
66
+ else
67
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
68
+ end
69
+ end
70
+
71
+ #request.basic_auth(@username, @password) unless @username
72
+
73
+ method = :get if method.nil?
74
+
75
+ if method == :get
76
+ request = Net::HTTP::Get.new(uri.request_uri, headers)
77
+ elsif method == :post
78
+ request = Net::HTTP::Post.new(uri.request_uri, headers)
79
+
80
+ request.body = body if body.kind_of? String
81
+ request.set_form_data(body) if body.kind_of? Hash
82
+ elsif method == :put
83
+ request = Net::HTTP::Put.new(uri.request_uri, headers)
84
+ else
85
+ request = Net::HTTP::Get.new(uri.request_uri, headers)
86
+ end
87
+
88
+ connection.read_timeout = timeout()
89
+ connection.open_timeout = timeout()
90
+
91
+ timeout_class = defined?(SystemTimer) ? SystemTimer : Timeout
92
+
93
+ timeout_class.timeout(timeout) do
94
+ return connection.request(request)
95
+ end
96
+ end
97
+
98
+ def validate_ssl_cert?
99
+ false
100
+ end
101
+
102
+ def ca_file
103
+ nil
104
+ end
105
+
106
+ def timeout
107
+ 10000
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module ResourceAccessor
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems' unless Object.const_defined?(:Gem)
2
+
3
+ require 'resource_accessor/resource_accessor'
4
+ require 'resource_accessor/version'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/lib/resource_accessor/version')
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "resource_accessor"
7
+ spec.summary = %q{This library is used to simplify access to protected or unprotected http resource}
8
+ spec.description = %q{This library is used to simplify access to protected or unprotected http resource}
9
+ spec.email = "alexander.shvets@gmail.com"
10
+ spec.authors = ["Alexander Shvets"]
11
+ spec.homepage = "http://github.com/shvets/resource_accessor"
12
+
13
+ spec.files = `git ls-files`.split($\)
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+ spec.version = ResourceAccessor::VERSION
17
+
18
+ <%= include_dependencies %>
19
+ end
20
+
@@ -0,0 +1,14 @@
1
+ require_relative 'spec_helper'
2
+
3
+ require 'resource_accessor/resource_accessor'
4
+
5
+ describe ResourceAccessor do
6
+
7
+ it "should get simple response" do
8
+ response = subject.get_response :url => "http://www.lenta.ru"
9
+
10
+ response.should_not be_nil
11
+ end
12
+
13
+ end
14
+
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+
3
+ # add lib directory
4
+ $:.unshift File.dirname(__FILE__) + '/../lib'
5
+
6
+
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resource_accessor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Shvets
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gemspec_deps_gen
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: gemcutter
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: This library is used to simplify access to protected or unprotected http
47
+ resource
48
+ email: alexander.shvets@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .idea/dictionaries/alex.xml
55
+ - .idea/scopes/scope_settings.xml
56
+ - .rvmrc
57
+ - CHANGES
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - lib/resource_accessor.rb
64
+ - lib/resource_accessor/resource_accessor.rb
65
+ - lib/resource_accessor/version.rb
66
+ - resource_accessor.gemspec.erb
67
+ - spec/resource_accessor_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: http://github.com/shvets/resource_accessor
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.24
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: This library is used to simplify access to protected or unprotected http
93
+ resource
94
+ test_files:
95
+ - spec/resource_accessor_spec.rb
96
+ - spec/spec_helper.rb