typed_form 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/.yardopts +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +1 -0
- data/README.md +17 -1
- data/lib/typed_form/form.rb +8 -9
- data/lib/typed_form/form_data/form_submission.rb +1 -1
- data/lib/typed_form/form_data/parsed_json.rb +4 -2
- data/lib/typed_form/version.rb +1 -1
- data/lib/typed_form/virtual_model.rb +96 -0
- data/lib/typed_form/webhook.rb +1 -1
- data/lib/typed_form.rb +1 -0
- metadata +4 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: d5ead981248e841e93b35f762ad6bfa365603a40
         | 
| 4 | 
            +
              data.tar.gz: 47d2587d6a6bfc5405ae5c1263e9b912d65e0853
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 21dc2335dac2fbc928e5e640612cda0aa05df5b88f75274351b25c916d0cfaa5e210f172efa033fdfa699c32474d3c451c1b84567797c04a1d5f4a16a28d6b91
         | 
| 7 | 
            +
              data.tar.gz: 9973d47d3053017994a482de7584dc12c8001bb1ab2a68d10bf1693c302502dc3f2f59175437efcc0aabc604cfd3a34ef1a8332b8ea2d54dbf359aa57bd20c3a
         | 
    
        data/.yardopts
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            --markup=markdown
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -1,5 +1,9 @@ | |
| 1 1 | 
             
            # Change Log
         | 
| 2 2 |  | 
| 3 | 
            +
            ## [0.1.1] - 2017-03-12
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Adds support for TypedForm::VirtualModel. Adds a decorator-like model which allows user-defined VirtualModels which respond with Typeform Data values.
         | 
| 6 | 
            +
             | 
| 3 7 | 
             
            ## [0.1.0] - 2017-03-12
         | 
| 4 8 |  | 
| 5 9 | 
             
            Adds logic for extracting data needed for Data API from incoming Webhooks. 
         | 
    
        data/Gemfile
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -105,7 +105,18 @@ questions.first.type | |
| 105 105 |  | 
| 106 106 | 
             
            The most common use case for this is extrapolating question and answers into a simple object that can provide a clean interface for displaying them. Question type information can be used to allow helpers to format and display different field types (most specifically dates) in a more user-friendly format.
         | 
| 107 107 |  | 
| 108 | 
            -
            Additional documentation is available at [Rubydoc.](http://www.rubydoc.info/github/useed/typed_form)
         | 
| 108 | 
            +
            Additional documentation is available at [Rubydoc.](http://www.rubydoc.info/github/useed/typed_form/master)
         | 
| 109 | 
            +
             | 
| 110 | 
            +
            ## Wishlist
         | 
| 111 | 
            +
             | 
| 112 | 
            +
            This gem is in the very early stages of development. Typeform does not provide a full example of the full JSON schema, so specific question and answer formats are not guaranteed to work. Issues and PRs are welcome, especially for functionality which focuses on working with data from the Typeform Data API.
         | 
| 113 | 
            +
             | 
| 114 | 
            +
            The API wrapper is minimalist by purpose, and currently is only designed to fetch a single form response for a specific form from the Data API. Additional functionality may be desired in the future.
         | 
| 115 | 
            +
             | 
| 116 | 
            +
            - [ ] Error Handling for JSON Responses
         | 
| 117 | 
            +
            - [ ] Error Handling for API Requests
         | 
| 118 | 
            +
            - [ ] Error Logging Configuration
         | 
| 119 | 
            +
            - [ ] Automatic retry & failover for Typeform API response failures
         | 
| 109 120 |  | 
| 110 121 | 
             
            ## Development
         | 
| 111 122 |  | 
| @@ -121,3 +132,8 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/useed/ | |
| 121 132 | 
             
            ## License
         | 
| 122 133 |  | 
| 123 134 | 
             
            The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
         | 
| 135 | 
            +
             | 
| 136 | 
            +
             | 
| 137 | 
            +
            ## Code of Conduct
         | 
| 138 | 
            +
             | 
| 139 | 
            +
            Everyone interacting in this repository is expected to follow [our code of conduct](https://github.com/useed/typed_form/blob/master/CODE_OF_CONDUCT.md)
         | 
    
        data/lib/typed_form/form.rb
    CHANGED
    
    | @@ -1,6 +1,5 @@ | |
| 1 1 | 
             
            module TypedForm
         | 
| 2 | 
            -
              # A representation of the Typeform Form Data for a single
         | 
| 3 | 
            -
              # form response.
         | 
| 2 | 
            +
              # A representation of the Typeform Form Data for a single form response.
         | 
| 4 3 | 
             
              #
         | 
| 5 4 | 
             
              # @attr_reader [String] json JSON data using Typeform's Data API schema
         | 
| 6 5 | 
             
              class Form
         | 
| @@ -16,7 +15,7 @@ module TypedForm | |
| 16 15 | 
             
                def_delegators :submission, :questions
         | 
| 17 16 |  | 
| 18 17 | 
             
                # Creates a new instance of a Form, to allow querying
         | 
| 19 | 
            -
                def initialize(json: | 
| 18 | 
            +
                def initialize(json:)
         | 
| 20 19 | 
             
                  @json = json
         | 
| 21 20 | 
             
                end
         | 
| 22 21 |  | 
| @@ -37,13 +36,17 @@ module TypedForm | |
| 37 36 | 
             
                  questions.each_with_object({}) { |q, hash| hash[q.text] = q.answer }
         | 
| 38 37 | 
             
                end
         | 
| 39 38 |  | 
| 39 | 
            +
                def response
         | 
| 40 | 
            +
                  raise StandardError, "Form expects a single response" if multi_response?
         | 
| 41 | 
            +
                  responses.first
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 40 44 | 
             
                private
         | 
| 41 45 |  | 
| 42 46 | 
             
                def submission
         | 
| 43 | 
            -
                  raise StandardError, "Form expects a single response" if multi_response?
         | 
| 44 47 | 
             
                  @_submission ||= FormData::FormSubmission.new(
         | 
| 45 48 | 
             
                    parsed_questions: parsed_json.questions,
         | 
| 46 | 
            -
                    parsed_response:  | 
| 49 | 
            +
                    parsed_response: response
         | 
| 47 50 | 
             
                  )
         | 
| 48 51 | 
             
                end
         | 
| 49 52 |  | 
| @@ -54,9 +57,5 @@ module TypedForm | |
| 54 57 | 
             
                def parsed_json
         | 
| 55 58 | 
             
                  @_parsed_json ||= FormData::ParsedJson.new(json: json)
         | 
| 56 59 | 
             
                end
         | 
| 57 | 
            -
             | 
| 58 | 
            -
                def response
         | 
| 59 | 
            -
                  responses.first
         | 
| 60 | 
            -
                end
         | 
| 61 60 | 
             
              end
         | 
| 62 61 | 
             
            end
         | 
| @@ -8,9 +8,11 @@ module TypedForm | |
| 8 8 | 
             
                  attr_reader :json
         | 
| 9 9 |  | 
| 10 10 | 
             
                  # @!method questions
         | 
| 11 | 
            -
                  #   @return [Arendelle]  | 
| 11 | 
            +
                  #   @return [Arendelle] An immutable representation of the Typeform Data
         | 
| 12 | 
            +
                  #     API JSON questions field.
         | 
| 12 13 | 
             
                  # @!method responses
         | 
| 13 | 
            -
                  #   @return [Arendelle]  | 
| 14 | 
            +
                  #   @return [Arendelle] An immutable representation of the Typeform Data
         | 
| 15 | 
            +
                  #     API JSON responses field.
         | 
| 14 16 | 
             
                  def_delegators :parsed_json, :questions, :responses
         | 
| 15 17 |  | 
| 16 18 | 
             
                  # Creates and freezes JSON data.
         | 
    
        data/lib/typed_form/version.rb
    CHANGED
    
    
| @@ -0,0 +1,96 @@ | |
| 1 | 
            +
            module TypedForm
         | 
| 2 | 
            +
              # A representation of the Typeform Response Data for a single form response.
         | 
| 3 | 
            +
              # Dynamically adds methods at initialization time, to provide a simple
         | 
| 4 | 
            +
              # interface for accessing form response data.
         | 
| 5 | 
            +
              #
         | 
| 6 | 
            +
              # ```ruby
         | 
| 7 | 
            +
              # # this example would require json data with a question of "What is your
         | 
| 8 | 
            +
              # # "email?" and an answer of "user@example.com"
         | 
| 9 | 
            +
              #
         | 
| 10 | 
            +
              # form = TypedForm::Form.build_form_from(json: your_json_source)
         | 
| 11 | 
            +
              # hash = { "user_email" => "What is your email" }
         | 
| 12 | 
            +
              # virtual_model = TypedForm::VirtualModel.new(form: form, question_attr: hash)
         | 
| 13 | 
            +
              # ```
         | 
| 14 | 
            +
              #
         | 
| 15 | 
            +
              # VirtualModel has now dynamically matched the user_email attribute to the
         | 
| 16 | 
            +
              # answer of the question, by finding the question matching the regular
         | 
| 17 | 
            +
              # expression "What is your email", and then responding with the answer.
         | 
| 18 | 
            +
              #
         | 
| 19 | 
            +
              # ```ruby
         | 
| 20 | 
            +
              # virtual_model.user_email => "user@example.com"
         | 
| 21 | 
            +
              # ```
         | 
| 22 | 
            +
              #
         | 
| 23 | 
            +
              # @attr_reader [Form] form The instance of the form used when referencing
         | 
| 24 | 
            +
              #   questions and answers in the submission.
         | 
| 25 | 
            +
              # @attr_reader [Hash] question_attrs Hash mapping attribute names to regular
         | 
| 26 | 
            +
              #   expressions, expressed as strings.
         | 
| 27 | 
            +
              #
         | 
| 28 | 
            +
              #   Format example: To match the answers of "What is your email" to an
         | 
| 29 | 
            +
              #   attribute of "user_email", the format would look like:
         | 
| 30 | 
            +
              #
         | 
| 31 | 
            +
              #   ```ruby
         | 
| 32 | 
            +
              #   question_attrs = { "user_email" => "What is your email" }
         | 
| 33 | 
            +
              #   ```
         | 
| 34 | 
            +
              #
         | 
| 35 | 
            +
              # @attr_reader [Hash] response_attrs Hash mapping attribute names to a
         | 
| 36 | 
            +
              #   chain of attributes accessbile from the form response.
         | 
| 37 | 
            +
              #
         | 
| 38 | 
            +
              #   Format example: To match the user_agent of the submission, which is under
         | 
| 39 | 
            +
              #   response["metadata"]["user_agent"], the format would look like:
         | 
| 40 | 
            +
              #
         | 
| 41 | 
            +
              #   ```ruby
         | 
| 42 | 
            +
              #   response_attrs = { "user_agent" => "metadata.user_agent" }
         | 
| 43 | 
            +
              #   ```
         | 
| 44 | 
            +
              class VirtualModel
         | 
| 45 | 
            +
                attr_reader :form, :question_attrs, :response_attrs
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                def initialize(form:, question_attrs: nil, response_attrs: nil)
         | 
| 48 | 
            +
                  @form = form
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                  _define_question_attributes(question_attrs || {})
         | 
| 51 | 
            +
                  _define_response_attributes(response_attrs || {})
         | 
| 52 | 
            +
                end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                private
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                def find_answer_to(text)
         | 
| 57 | 
            +
                  question = form.questions.detect { |q| q.text.match Regexp.new(text) }
         | 
| 58 | 
            +
                  return unless question
         | 
| 59 | 
            +
                  question.answer
         | 
| 60 | 
            +
                end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                def able_to_define?(attr)
         | 
| 63 | 
            +
                  !self.class.method_defined?(attr) &&
         | 
| 64 | 
            +
                    attr.to_s.match(/^[a-z_][a-zA-Z_0-9!?]*$/)
         | 
| 65 | 
            +
                end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                def _define_question_attributes(question_attrs)
         | 
| 68 | 
            +
                  question_attrs.each do |k, v|
         | 
| 69 | 
            +
                    next unless able_to_define?(k)
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                    answer = find_answer_to(v)
         | 
| 72 | 
            +
                    next if answer.nil?
         | 
| 73 | 
            +
             | 
| 74 | 
            +
                    define_singleton_method k do
         | 
| 75 | 
            +
                      answer
         | 
| 76 | 
            +
                    end
         | 
| 77 | 
            +
                  end
         | 
| 78 | 
            +
                end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                def response_attribute_value(messages)
         | 
| 81 | 
            +
                  messages.split(".").inject(form.response) do |message, e|
         | 
| 82 | 
            +
                    message.send(e)
         | 
| 83 | 
            +
                  end
         | 
| 84 | 
            +
                end
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                def _define_response_attributes(response_attrs)
         | 
| 87 | 
            +
                  response_attrs.each do |k, v|
         | 
| 88 | 
            +
                    next unless able_to_define?(k)
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                    define_singleton_method k do
         | 
| 91 | 
            +
                      response_attribute_value(v)
         | 
| 92 | 
            +
                    end
         | 
| 93 | 
            +
                  end
         | 
| 94 | 
            +
                end
         | 
| 95 | 
            +
              end
         | 
| 96 | 
            +
            end
         | 
    
        data/lib/typed_form/webhook.rb
    CHANGED
    
    
    
        data/lib/typed_form.rb
    CHANGED
    
    | @@ -6,6 +6,7 @@ require "typed_form/form_data/parsed_json" | |
| 6 6 | 
             
            require "typed_form/form_data/question"
         | 
| 7 7 | 
             
            require "typed_form/form_data/answers"
         | 
| 8 8 | 
             
            require "typed_form/form_data/form_submission"
         | 
| 9 | 
            +
            require "typed_form/virtual_model"
         | 
| 9 10 | 
             
            require "typed_form/form"
         | 
| 10 11 | 
             
            require "typed_form/webhook"
         | 
| 11 12 | 
             
            require "typed_form/version"
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: typed_form
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.1
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Rob Cole
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: exe
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2017-03- | 
| 12 | 
            +
            date: 2017-03-13 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: httparty
         | 
| @@ -125,6 +125,7 @@ extra_rdoc_files: [] | |
| 125 125 | 
             
            files:
         | 
| 126 126 | 
             
            - ".gitignore"
         | 
| 127 127 | 
             
            - ".rspec"
         | 
| 128 | 
            +
            - ".yardopts"
         | 
| 128 129 | 
             
            - CHANGELOG.md
         | 
| 129 130 | 
             
            - CODE_OF_CONDUCT.md
         | 
| 130 131 | 
             
            - Gemfile
         | 
| @@ -141,6 +142,7 @@ files: | |
| 141 142 | 
             
            - lib/typed_form/form_data/parsed_json.rb
         | 
| 142 143 | 
             
            - lib/typed_form/form_data/question.rb
         | 
| 143 144 | 
             
            - lib/typed_form/version.rb
         | 
| 145 | 
            +
            - lib/typed_form/virtual_model.rb
         | 
| 144 146 | 
             
            - lib/typed_form/webhook.rb
         | 
| 145 147 | 
             
            homepage: https://github.com/useed/typed_form
         | 
| 146 148 | 
             
            licenses:
         |