ogc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rubocop.yml +20 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +13 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +28 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +76 -0
  10. data/Rakefile +10 -0
  11. data/bin/console +14 -0
  12. data/bin/guard +16 -0
  13. data/bin/setup +8 -0
  14. data/lib/ogc.rb +9 -0
  15. data/lib/ogc/clean_xml.rb +13 -0
  16. data/lib/ogc/exceptions.rb +8 -0
  17. data/lib/ogc/exceptions/exception_report.rb +44 -0
  18. data/lib/ogc/exceptions/ogc_exception.rb +5 -0
  19. data/lib/ogc/exceptions/request_error_exception.rb +7 -0
  20. data/lib/ogc/exceptions/request_failed_exception.rb +12 -0
  21. data/lib/ogc/query.rb +11 -0
  22. data/lib/ogc/version.rb +3 -0
  23. data/lib/ogc/web_feature_service.rb +19 -0
  24. data/lib/ogc/web_feature_service/base.rb +95 -0
  25. data/lib/ogc/web_feature_service/get_capabilities.rb +9 -0
  26. data/lib/ogc/web_feature_service/get_feature.rb +14 -0
  27. data/ogc.gemspec +50 -0
  28. data/test/ogc/clean_xml_test.rb +31 -0
  29. data/test/ogc/exceptions/exception_report_test.rb +74 -0
  30. data/test/ogc/exceptions/ogc_exception_test.rb +11 -0
  31. data/test/ogc/exceptions/request_error_exception_test.rb +11 -0
  32. data/test/ogc/exceptions/request_failed_exception_test.rb +25 -0
  33. data/test/ogc/exceptions_test.rb +9 -0
  34. data/test/ogc/query_test.rb +21 -0
  35. data/test/ogc/version_test.rb +15 -0
  36. data/test/ogc/web_feature_service/base_test.rb +145 -0
  37. data/test/ogc/web_feature_service/get_capabilities_test.rb +34 -0
  38. data/test/ogc/web_feature_service/get_feature_test.rb +38 -0
  39. data/test/ogc/web_feature_service_test.rb +22 -0
  40. data/test/ogc_test.rb +7 -0
  41. data/test/test_helper.rb +51 -0
  42. data/test/xml/wfs/exception_report.xml +6 -0
  43. data/test/xml/wfs/fake_report.xml +4 -0
  44. data/test/xml/wfs/get_capabilities_request.xml +2 -0
  45. data/test/xml/wfs/get_capabilities_response.xml +188 -0
  46. data/test/xml/wfs/get_feature_response.xml +28 -0
  47. data/test/xml/wfs/service_exception_report.xml +4 -0
  48. metadata +231 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3b4a0d0329246bcf846876e206fdb9cfff55897e
4
+ data.tar.gz: 1328d76adc737a5eef0f7307571816a2cdfab737
5
+ SHA512:
6
+ metadata.gz: 46337235bb4f7fcd81ddbcad19e535d09c89e17df875401ec5a5bee601cec975b38669d57aebba7472ca98bde0eae1e4a2dae35a25de2f2100cfaa7a4b607fd0
7
+ data.tar.gz: cbaf676f1fd2441deec67ea49e8b942f04e701450a9eb8bccd3ba24f229533f55441515dfca6b385a1c318f46c89c871ca3578f07bae27ab3ef171c843ff2b59
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ # Bundler files
2
+ /.bundle/
3
+ /Gemfile.lock
4
+
5
+ # YarDOC files
6
+ /.yardoc
7
+ /_yardoc/
8
+
9
+ # Miscellaneous files
10
+ /coverage/
11
+ /pkg/
12
+ /spec/reports/
13
+ /tmp/
14
+
15
+ # RBenv files
16
+ .ruby-version
17
+ .rbenv-gemsets
18
+ .gems/*
data/.rubocop.yml ADDED
@@ -0,0 +1,20 @@
1
+ Documentation:
2
+ Enabled: false
3
+
4
+ Style/AccessModifierIndentation:
5
+ EnforcedStyle: outdent
6
+
7
+ Style/AlignParameters:
8
+ EnforcedStyle: with_fixed_indentation
9
+
10
+ Style/AndOr:
11
+ EnforcedStyle: always
12
+
13
+ Style/Encoding:
14
+ Enabled: false
15
+
16
+ Style/SignalException:
17
+ EnforcedStyle: only_raise
18
+
19
+ Metrics/LineLength:
20
+ Max: 82
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.7
4
+ - 2.2.3
5
+ before_install: gem install bundler
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ogc.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,28 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ guard :minitest do
19
+ # with Minitest::Unit
20
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
21
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
22
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
23
+
24
+ # with Minitest::Spec
25
+ # watch(%r{^spec/(.*)_spec\.rb$})
26
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
27
+ # watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
28
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Geoffrey ROGUELON
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Ogc
2
+ [![Build Status](https://travis-ci.org/GRoguelon/ogc.svg?branch=master)](https://travis-ci.org/GRoguelon/ogc)
3
+ [![Code Climate](https://codeclimate.com/github/GRoguelon/ogc/badges/gpa.svg)](https://codeclimate.com/github/GRoguelon/ogc)
4
+ [![GitHub version](https://badge.fury.io/gh/groguelon%2Fogc.svg)](https://badge.fury.io/gh/groguelon%2Fogc)
5
+
6
+ This gems allow you to make calls in using WFS or WPS protocol defined by OGC organization.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'ogc'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install ogc
23
+
24
+ ## Usage
25
+
26
+ Currently the gem OGC handles only some Web Feature Service calls.
27
+
28
+ To use it, you have two ways.
29
+
30
+ Either, you use the shortcut method of `WebFeatureService`module:
31
+
32
+ ```ruby
33
+ # GetCapabilities
34
+ Ogc::WebFeatureService.get_capabilities('http://localhost/wfs', version: '1.0.0', key: 'SECRET')
35
+ #=> #<Ogc::WebFeatureService::GetCapabilities:0x007ffba414adc8 @url="http://localhost/wfs", @params={"version"=>"1.0.0", "key"=>"SECRET", "service"=>"wfs"}>
36
+
37
+ # GetFeature
38
+ Ogc::WebFeatureService.get_feature('http://localhost/wfs', version: '1.0.0', key: 'SECRET')
39
+ #=> #<Ogc::WebFeatureService::GetFeature:0x007ffba41ad720 @url="http://localhost/wfs", @params={"version"=>"1.0.0", "key"=>"SECRET", "service"=>"wfs"}>
40
+ ```
41
+
42
+ Or use the class directly:
43
+
44
+ ```ruby
45
+ # GetCapabilities
46
+ Ogc::WebFeatureService::GetCapabilies.new('http://localhost/wfs', version: '1.0.0', key: 'SECRET')
47
+ #=> #<Ogc::WebFeatureService::GetCapabilities:0x007ffba414adc8 @url="http://localhost/wfs", @params={"version"=>"1.0.0", "key"=>"SECRET", "service"=>"wfs"}>
48
+
49
+ # GetFeature
50
+ Ogc::WebFeatureService::GetFeature.new('http://localhost/wfs', version: '1.0.0', key: 'SECRET')
51
+ #=> #<Ogc::WebFeatureService::GetFeature:0x007ffba41ad720 @url="http://localhost/wfs", @params={"version"=>"1.0.0", "key"=>"SECRET", "service"=>"wfs"}>
52
+ ```
53
+
54
+ After that, you can use the methods `#get` and `#post` to resquest the endpoint.
55
+
56
+ ```ruby
57
+ ogc = Ogc::WebFeatureService::GetFeature.new('http://localhost/wfs', version: '1.0.0', key: 'SECRET')
58
+ ogc.get(:FEATURE_1, filter '…')
59
+ #=> Nokogiri::XML::Document
60
+ ```
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
65
+
66
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ogc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
71
+
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
76
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'ogc'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/guard ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'guard' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('guard', 'guard')
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/bin/bash
2
+
3
+ set -euo pipefail
4
+ IFS=$'\n\t'
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/ogc.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Ogc; end
2
+
3
+ # Refinements:
4
+ require 'ogc/clean_xml'
5
+ require 'ogc/query'
6
+
7
+ require 'ogc/exceptions'
8
+ require 'ogc/version'
9
+ require 'ogc/web_feature_service'
@@ -0,0 +1,13 @@
1
+ require 'active_support/core_ext/string/filters'
2
+
3
+ module Ogc
4
+ module CleanXML
5
+ refine String do
6
+ def clean_xml!
7
+ self.gsub!(/>\n+ *\t*</, '><')
8
+ self.squish!
9
+ self
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ module Ogc
2
+ module Exceptions; end
3
+ end
4
+
5
+ require 'ogc/exceptions/ogc_exception'
6
+ require 'ogc/exceptions/exception_report'
7
+ require 'ogc/exceptions/request_error_exception'
8
+ require 'ogc/exceptions/request_failed_exception'
@@ -0,0 +1,44 @@
1
+ require 'ogc/exceptions/ogc_exception'
2
+
3
+ module Ogc
4
+ module Exceptions
5
+ class ExceptionReport < OgcException
6
+ ROOTS = %w( ServiceExceptionReport ExceptionReport ).freeze
7
+
8
+ attr_reader :report
9
+
10
+ class << self
11
+ def exception?(report)
12
+ ROOTS.include?(report.root.name)
13
+ end
14
+ end
15
+
16
+ def initialize(report)
17
+ raise ArgumentError unless self.class.exception?(report)
18
+ @report = report
19
+ super(first_exception_message)
20
+ end
21
+
22
+ def at_xpath(xpath)
23
+ @report.at_xpath(xpath)
24
+ end
25
+
26
+ def xpath(xpath)
27
+ @report.xpath(xpath)
28
+ end
29
+
30
+ def version
31
+ xpath = @report.at_xpath('/*/@version')
32
+ xpath ? xpath.text : nil
33
+ end
34
+
35
+ private
36
+
37
+ def first_exception_message
38
+ message = @report.at_xpath('//xmlns:ServiceException')
39
+ message ||= @report.at_xpath('//xmlns:ExceptionText')
40
+ message && message.content
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module Ogc
2
+ module Exceptions
3
+ class OgcException < StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'ogc/exceptions/ogc_exception'
2
+
3
+ module Ogc
4
+ module Exceptions
5
+ class RequestErrorException < OgcException; end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Ogc
2
+ module Exceptions
3
+ class RequestFailedException < OgcException
4
+ attr_reader :code
5
+
6
+ def initialize(http_error)
7
+ super(http_error.message)
8
+ @code = http_error.code
9
+ end
10
+ end
11
+ end
12
+ end
data/lib/ogc/query.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'uri'
2
+
3
+ module Ogc
4
+ module Query
5
+ refine Hash.singleton_class do
6
+ def from_query(query)
7
+ Hash[URI.decode_www_form(query.to_s)]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Ogc
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,19 @@
1
+ require 'ogc/web_feature_service/base'
2
+ require 'ogc/web_feature_service/get_capabilities'
3
+ require 'ogc/web_feature_service/get_feature'
4
+
5
+ require 'active_support/core_ext/string/inflections'
6
+
7
+ module Ogc
8
+ module WebFeatureService
9
+ SUPPORTED_METHODS = %i( get_capabilities get_feature ).freeze
10
+
11
+ class << self
12
+ SUPPORTED_METHODS.each do |method|
13
+ define_method(method) do |*args|
14
+ "#{self}::#{method.to_s.camelize}".constantize.new(*args)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,95 @@
1
+ require 'ogc/exceptions'
2
+
3
+ require 'active_support/core_ext/hash/keys'
4
+ require 'active_support/core_ext/object/blank'
5
+ require 'active_support/core_ext/object/to_query'
6
+ require 'active_support/core_ext/string/inflections'
7
+ require 'nokogiri'
8
+
9
+ module Ogc
10
+ module WebFeatureService
11
+ class Base
12
+ include Exceptions
13
+
14
+ using CleanXML
15
+ using Query
16
+
17
+ attr_reader :params, :response, :url
18
+
19
+ DEFAULT_PARAMS = { 'service' => 'wfs' }.freeze
20
+
21
+ class << self
22
+ def request_name
23
+ @request_name ||= to_s.demodulize
24
+ end
25
+ end
26
+
27
+ def initialize(url, params = {})
28
+ @url = url
29
+ @params = params.stringify_keys!.merge(DEFAULT_PARAMS)
30
+ end
31
+
32
+ def get(extra_params, &block)
33
+ fetch(:get, extra_params ? extra_params.merge(@params) : @params, &block)
34
+ end
35
+
36
+ def post(body, &block)
37
+ fetch(:post, body.respond_to?(:to_xml) ? body.to_xml : body, &block)
38
+ end
39
+
40
+ def http
41
+ @http ||= Net::HTTP.new(uri.host, uri.port)
42
+ end
43
+
44
+ private
45
+
46
+ def fetch(type, content, &block)
47
+ request = request(type, content)
48
+ block.call(request) if block
49
+ handle_response(request)
50
+ end
51
+
52
+ def handle_response(request)
53
+ case (response = http.request(request))
54
+ when Net::HTTPSuccess
55
+ @response = Nokogiri::XML(response.body.clean_xml!).tap do |xml|
56
+ # rubocop:disable Style/RaiseArgs
57
+ raise ExceptionReport.new(xml) if ExceptionReport.exception?(xml)
58
+ # rubocop:enable Style/RaiseArgs
59
+ end
60
+ else
61
+ # rubocop:disable Style/RaiseArgs
62
+ raise RequestFailedException.new(response)
63
+ # rubocop:enable Style/RaiseArgs
64
+ end
65
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
66
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
67
+ Net::ProtocolError
68
+ raise RequestErrorException
69
+ end
70
+
71
+ def request(type, content)
72
+ case type
73
+ when :get then Net::HTTP::Get.new(uri_with_params(content))
74
+ when :post
75
+ Net::HTTP::Post.new(uri_without_params).tap do |req|
76
+ req.body = content
77
+ end
78
+ end
79
+ end
80
+
81
+ def uri_with_params(extra_params)
82
+ return uri_without_params if extra_params.blank?
83
+
84
+ uri_without_params.dup.tap do |uri|
85
+ uri.query = Hash.from_query(uri.query).merge!(extra_params).to_query
86
+ end
87
+ end
88
+
89
+ def uri_without_params
90
+ @uri ||= URI(@url)
91
+ end
92
+ alias_method :uri, :uri_without_params
93
+ end
94
+ end
95
+ end