reservix-client 0.0.2
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/README.md +4 -0
- data/lib/reservix.rb +27 -0
- data/lib/reservix/client.rb +110 -0
- data/lib/reservix/hash_response_wrapper.rb +9 -0
- data/lib/reservix/response_error.rb +20 -0
- data/lib/reservix/version.rb +3 -0
- metadata +136 -0
    
        data/README.md
    ADDED
    
    
    
        data/lib/reservix.rb
    ADDED
    
    | @@ -0,0 +1,27 @@ | |
| 1 | 
            +
            require 'hashie'
         | 
| 2 | 
            +
            require 'httmultiparty'
         | 
| 3 | 
            +
            require 'uri'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            require 'reservix/client'
         | 
| 6 | 
            +
            require 'reservix/hash_response_wrapper'
         | 
| 7 | 
            +
            require 'reservix/version'
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            module Reservix
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              def new(options={})
         | 
| 12 | 
            +
                Client.new(options)
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
              module_function :new
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              def method_missing(method_name, *args, &block)
         | 
| 17 | 
            +
                return super unless respond_to_missing?(method_name)
         | 
| 18 | 
            +
                Client.send(method_name, *args, &block)
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
              module_function :method_missing
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              def respond_to_missing?(method_name, include_private=false)
         | 
| 23 | 
            +
                Client.respond_to?(method_name, include_private)
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
              module_function :respond_to_missing?
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            end
         | 
| @@ -0,0 +1,110 @@ | |
| 1 | 
            +
            module Reservix
         | 
| 2 | 
            +
              class Client
         | 
| 3 | 
            +
                include HTTMultiParty
         | 
| 4 | 
            +
                USER_AGENT = "ASK HELMUT Reservix API Wrapper #{VERSION}"
         | 
| 5 | 
            +
                API_KEY_PARAM_NAME = "api-key"
         | 
| 6 | 
            +
                API_SUBHOST = "api"
         | 
| 7 | 
            +
                API_VERSION = "1"
         | 
| 8 | 
            +
                DEFAULT_OPTIONS = {
         | 
| 9 | 
            +
                  site: "reservix.de",
         | 
| 10 | 
            +
                  use_ssl: true,
         | 
| 11 | 
            +
                  api_module: "sale"
         | 
| 12 | 
            +
                }
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                attr_accessor :options
         | 
| 15 | 
            +
                headers({"User-Agent" => USER_AGENT})
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                def initialize(options = {})
         | 
| 18 | 
            +
                  store_options(options)
         | 
| 19 | 
            +
                  raise ArgumentError, "An api key must be present" if api_key.nil?
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                def get(path, query={}, options={})
         | 
| 23 | 
            +
                  handle_response {
         | 
| 24 | 
            +
                    self.class.get(*construct_query_arguments(path, options.merge(:query => query)))
         | 
| 25 | 
            +
                  }
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                def post(path, body={},  options={})
         | 
| 29 | 
            +
                  handle_response {
         | 
| 30 | 
            +
                    self.class.post(*construct_query_arguments(path, options.merge(:body => body), :body))
         | 
| 31 | 
            +
                  }
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                def put(path, body={},  options={})
         | 
| 35 | 
            +
                  handle_response {
         | 
| 36 | 
            +
                    self.class.put(*construct_query_arguments(path, options.merge(:body => body), :body))
         | 
| 37 | 
            +
                  }
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                def delete(path, query={}, options={})
         | 
| 41 | 
            +
                  handle_response {
         | 
| 42 | 
            +
                    self.class.delete(*construct_query_arguments(path, options.merge(:query => query)))
         | 
| 43 | 
            +
                  }
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                def head(path, query={}, options={})
         | 
| 47 | 
            +
                  handle_response {
         | 
| 48 | 
            +
                    self.class.head(*construct_query_arguments(path, options.merge(:query => query)))
         | 
| 49 | 
            +
                  }
         | 
| 50 | 
            +
                end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                # accessors for options
         | 
| 53 | 
            +
                def api_key
         | 
| 54 | 
            +
                  @options[:api_key]
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                def use_ssl?
         | 
| 58 | 
            +
                  @options[:use_ssl]
         | 
| 59 | 
            +
                end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                def api_module
         | 
| 62 | 
            +
                  @options[:api_module]
         | 
| 63 | 
            +
                end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                def site
         | 
| 66 | 
            +
                  @options[:site]
         | 
| 67 | 
            +
                end
         | 
| 68 | 
            +
                alias host site
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                def api_host
         | 
| 71 | 
            +
                  [API_SUBHOST, host].join(".")
         | 
| 72 | 
            +
                end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
                def api_url
         | 
| 75 | 
            +
                  [api_host, API_VERSION, api_module].join("/")
         | 
| 76 | 
            +
                end
         | 
| 77 | 
            +
                private
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                  def handle_response(refreshing_enabled=true, &block)
         | 
| 80 | 
            +
                    response = block.call
         | 
| 81 | 
            +
                    if response && !response.success?
         | 
| 82 | 
            +
                      raise ResponseError.new(response)
         | 
| 83 | 
            +
                    elsif response.is_a?(Hash)
         | 
| 84 | 
            +
                      HashResponseWrapper.new(response)
         | 
| 85 | 
            +
                    elsif response && response.success?
         | 
| 86 | 
            +
                      response
         | 
| 87 | 
            +
                    end
         | 
| 88 | 
            +
                  end
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                  def store_options(options={})
         | 
| 91 | 
            +
                    @options ||= DEFAULT_OPTIONS.dup
         | 
| 92 | 
            +
                    @options.merge!(options)
         | 
| 93 | 
            +
                  end
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                  def construct_query_arguments(path_or_uri, options={}, body_or_query=:query)
         | 
| 96 | 
            +
                    uri = URI.parse(path_or_uri)
         | 
| 97 | 
            +
                    path = uri.path
         | 
| 98 | 
            +
                    scheme = use_ssl? ? "https" : "http"
         | 
| 99 | 
            +
                    options = options.dup
         | 
| 100 | 
            +
                    options[body_or_query] ||= {}
         | 
| 101 | 
            +
                    options[body_or_query][:format] = "json"
         | 
| 102 | 
            +
                    options[body_or_query][API_KEY_PARAM_NAME] = api_key
         | 
| 103 | 
            +
             | 
| 104 | 
            +
                    [
         | 
| 105 | 
            +
                      "#{scheme}://#{api_url}#{path}#{uri.query ? "?#{uri.query}" : ""}",
         | 
| 106 | 
            +
                      options
         | 
| 107 | 
            +
                    ]
         | 
| 108 | 
            +
                  end
         | 
| 109 | 
            +
              end
         | 
| 110 | 
            +
            end
         | 
| @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            module Reservix
         | 
| 2 | 
            +
              class ResponseError < HTTParty::ResponseError
         | 
| 3 | 
            +
                STATUS_CODES = {
         | 
| 4 | 
            +
                  400 => "Bad Request",
         | 
| 5 | 
            +
                  403 => "Forbidden",
         | 
| 6 | 
            +
                  404 => "Not Found",
         | 
| 7 | 
            +
                  412 => "Precondition Failed",
         | 
| 8 | 
            +
                  500 => "Internal Server Error",
         | 
| 9 | 
            +
                  501 => "Not Implemented"
         | 
| 10 | 
            +
                }
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                def message
         | 
| 13 | 
            +
                  error = response.parsed_response
         | 
| 14 | 
            +
                  "HTTP status: #{response.code} #{STATUS_CODES[response.code]} Error: #{error}"
         | 
| 15 | 
            +
                rescue
         | 
| 16 | 
            +
                  "HTTP status: #{response.code} #{STATUS_CODES[response.code]}"
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,136 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: reservix-client
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.2
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - Niels Hoffmann
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2013-11-07 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies:
         | 
| 14 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 15 | 
            +
              name: httmultiparty
         | 
| 16 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 17 | 
            +
                none: false
         | 
| 18 | 
            +
                requirements:
         | 
| 19 | 
            +
                - - ~>
         | 
| 20 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 21 | 
            +
                    version: 0.3.0
         | 
| 22 | 
            +
              type: :runtime
         | 
| 23 | 
            +
              prerelease: false
         | 
| 24 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 25 | 
            +
                none: false
         | 
| 26 | 
            +
                requirements:
         | 
| 27 | 
            +
                - - ~>
         | 
| 28 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 29 | 
            +
                    version: 0.3.0
         | 
| 30 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 31 | 
            +
              name: hashie
         | 
| 32 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 33 | 
            +
                none: false
         | 
| 34 | 
            +
                requirements:
         | 
| 35 | 
            +
                - - ~>
         | 
| 36 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 37 | 
            +
                    version: '2.0'
         | 
| 38 | 
            +
              type: :runtime
         | 
| 39 | 
            +
              prerelease: false
         | 
| 40 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 41 | 
            +
                none: false
         | 
| 42 | 
            +
                requirements:
         | 
| 43 | 
            +
                - - ~>
         | 
| 44 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 45 | 
            +
                    version: '2.0'
         | 
| 46 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 47 | 
            +
              name: bundler
         | 
| 48 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 49 | 
            +
                none: false
         | 
| 50 | 
            +
                requirements:
         | 
| 51 | 
            +
                - - ~>
         | 
| 52 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 53 | 
            +
                    version: '1.0'
         | 
| 54 | 
            +
              type: :development
         | 
| 55 | 
            +
              prerelease: false
         | 
| 56 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 57 | 
            +
                none: false
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - ~>
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: '1.0'
         | 
| 62 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 63 | 
            +
              name: guard
         | 
| 64 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                none: false
         | 
| 66 | 
            +
                requirements:
         | 
| 67 | 
            +
                - - ~>
         | 
| 68 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 69 | 
            +
                    version: '1.0'
         | 
| 70 | 
            +
              type: :development
         | 
| 71 | 
            +
              prerelease: false
         | 
| 72 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 73 | 
            +
                none: false
         | 
| 74 | 
            +
                requirements:
         | 
| 75 | 
            +
                - - ~>
         | 
| 76 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 77 | 
            +
                    version: '1.0'
         | 
| 78 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 79 | 
            +
              name: rb-fsevent
         | 
| 80 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 81 | 
            +
                none: false
         | 
| 82 | 
            +
                requirements:
         | 
| 83 | 
            +
                - - ~>
         | 
| 84 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 85 | 
            +
                    version: '0.9'
         | 
| 86 | 
            +
              type: :development
         | 
| 87 | 
            +
              prerelease: false
         | 
| 88 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 89 | 
            +
                none: false
         | 
| 90 | 
            +
                requirements:
         | 
| 91 | 
            +
                - - ~>
         | 
| 92 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 93 | 
            +
                    version: '0.9'
         | 
| 94 | 
            +
            description: A wrapper for the Reservix REST api. It provides simple methods to retireve
         | 
| 95 | 
            +
              resources form the Reservix REST api.
         | 
| 96 | 
            +
            email:
         | 
| 97 | 
            +
            - niels@askhelmut.com
         | 
| 98 | 
            +
            executables: []
         | 
| 99 | 
            +
            extensions: []
         | 
| 100 | 
            +
            extra_rdoc_files: []
         | 
| 101 | 
            +
            files:
         | 
| 102 | 
            +
            - lib/reservix/client.rb
         | 
| 103 | 
            +
            - lib/reservix/hash_response_wrapper.rb
         | 
| 104 | 
            +
            - lib/reservix/response_error.rb
         | 
| 105 | 
            +
            - lib/reservix/version.rb
         | 
| 106 | 
            +
            - lib/reservix.rb
         | 
| 107 | 
            +
            - README.md
         | 
| 108 | 
            +
            homepage: https://askhelmut.com
         | 
| 109 | 
            +
            licenses:
         | 
| 110 | 
            +
            - MIT
         | 
| 111 | 
            +
            post_install_message: 
         | 
| 112 | 
            +
            rdoc_options: []
         | 
| 113 | 
            +
            require_paths:
         | 
| 114 | 
            +
            - lib
         | 
| 115 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 116 | 
            +
              none: false
         | 
| 117 | 
            +
              requirements:
         | 
| 118 | 
            +
              - - ! '>='
         | 
| 119 | 
            +
                - !ruby/object:Gem::Version
         | 
| 120 | 
            +
                  version: '0'
         | 
| 121 | 
            +
                  segments:
         | 
| 122 | 
            +
                  - 0
         | 
| 123 | 
            +
                  hash: -4378129810663502477
         | 
| 124 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 125 | 
            +
              none: false
         | 
| 126 | 
            +
              requirements:
         | 
| 127 | 
            +
              - - ! '>='
         | 
| 128 | 
            +
                - !ruby/object:Gem::Version
         | 
| 129 | 
            +
                  version: 1.3.5
         | 
| 130 | 
            +
            requirements: []
         | 
| 131 | 
            +
            rubyforge_project: 
         | 
| 132 | 
            +
            rubygems_version: 1.8.25
         | 
| 133 | 
            +
            signing_key: 
         | 
| 134 | 
            +
            specification_version: 3
         | 
| 135 | 
            +
            summary: A wrapper for the Reservix REST api.
         | 
| 136 | 
            +
            test_files: []
         |