ruson 0.0.4 → 0.0.5
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/README.md +75 -0
 - data/lib/ruson/base.rb +55 -4
 - data/lib/ruson/version.rb +1 -1
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 78281f5b501061ceef879a28599cba16f306d28af38e975eb05251cb13aaba36
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 6c7eaa57a218e4f8efaed0c866a9603d032d3d90ec65ebe38def3ddc876a9759
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 955e3ea5b8423d1ed12a10f344736810b46391df2ed27bcdaef8474425dda5bb1116e3fd7e2fbb61fd8b98066d9f10ae0a8e78a3197e90ac13d1b262640a0428
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 5e49ec83553036fe288cab6dda5d6abd021d02b88fc8dfce4934caccd71ef620d1d40f6269e2408b3033d959e44ebcf784ef80287fff533454a12394a0026bad
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -133,6 +133,81 @@ post = Post.new(json) 
     | 
|
| 
       133 
133 
     | 
    
         
             
            post.tags.first.name #=> 'Ruby'
         
     | 
| 
       134 
134 
     | 
    
         
             
            ```
         
     | 
| 
       135 
135 
     | 
    
         | 
| 
      
 136 
     | 
    
         
            +
            ### enum
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
            article.json
         
     | 
| 
      
 139 
     | 
    
         
            +
            ```json
         
     | 
| 
      
 140 
     | 
    
         
            +
            {  
         
     | 
| 
      
 141 
     | 
    
         
            +
               "title":"Title",
         
     | 
| 
      
 142 
     | 
    
         
            +
               "status":"draft"
         
     | 
| 
      
 143 
     | 
    
         
            +
            }
         
     | 
| 
      
 144 
     | 
    
         
            +
            ```
         
     | 
| 
      
 145 
     | 
    
         
            +
             
     | 
| 
      
 146 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 147 
     | 
    
         
            +
            class Article < Ruson::Base
         
     | 
| 
      
 148 
     | 
    
         
            +
              field :title
         
     | 
| 
      
 149 
     | 
    
         
            +
              enum status: { :draft, :published }
         
     | 
| 
      
 150 
     | 
    
         
            +
            end
         
     | 
| 
      
 151 
     | 
    
         
            +
             
     | 
| 
      
 152 
     | 
    
         
            +
            article = Article.new('article.json')
         
     | 
| 
      
 153 
     | 
    
         
            +
             
     | 
| 
      
 154 
     | 
    
         
            +
            article.status #=> :draft
         
     | 
| 
      
 155 
     | 
    
         
            +
            article.draft? #=> true
         
     | 
| 
      
 156 
     | 
    
         
            +
             
     | 
| 
      
 157 
     | 
    
         
            +
            article.status = 'published'
         
     | 
| 
      
 158 
     | 
    
         
            +
            article.status #=> :published
         
     | 
| 
      
 159 
     | 
    
         
            +
             
     | 
| 
      
 160 
     | 
    
         
            +
            article.status = 'undefined'
         
     | 
| 
      
 161 
     | 
    
         
            +
              #=> undefined is not a valid status (ArgumentError)
         
     | 
| 
      
 162 
     | 
    
         
            +
            ```
         
     | 
| 
      
 163 
     | 
    
         
            +
             
     | 
| 
      
 164 
     | 
    
         
            +
            ### to_json
         
     | 
| 
      
 165 
     | 
    
         
            +
             
     | 
| 
      
 166 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 167 
     | 
    
         
            +
            class Post < Ruson::Base
         
     | 
| 
      
 168 
     | 
    
         
            +
              field :title
         
     | 
| 
      
 169 
     | 
    
         
            +
              field :url
         
     | 
| 
      
 170 
     | 
    
         
            +
            end
         
     | 
| 
      
 171 
     | 
    
         
            +
             
     | 
| 
      
 172 
     | 
    
         
            +
            json = File.read('post.json')
         
     | 
| 
      
 173 
     | 
    
         
            +
            post = Post.new(json)
         
     | 
| 
      
 174 
     | 
    
         
            +
            post.url = 'https://example.com/examples'
         
     | 
| 
      
 175 
     | 
    
         
            +
             
     | 
| 
      
 176 
     | 
    
         
            +
            post.to_json #=> "{\"title\":\"Ruson\",\"url\":\"https://example.com/examples\"}"
         
     | 
| 
      
 177 
     | 
    
         
            +
            ```
         
     | 
| 
      
 178 
     | 
    
         
            +
             
     | 
| 
      
 179 
     | 
    
         
            +
            ### to_hash
         
     | 
| 
      
 180 
     | 
    
         
            +
             
     | 
| 
      
 181 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 182 
     | 
    
         
            +
            class Post < Ruson::Base
         
     | 
| 
      
 183 
     | 
    
         
            +
              field :title
         
     | 
| 
      
 184 
     | 
    
         
            +
              field :url
         
     | 
| 
      
 185 
     | 
    
         
            +
            end
         
     | 
| 
      
 186 
     | 
    
         
            +
             
     | 
| 
      
 187 
     | 
    
         
            +
            json = File.read('post.json')
         
     | 
| 
      
 188 
     | 
    
         
            +
            post = Post.new(json)
         
     | 
| 
      
 189 
     | 
    
         
            +
            post.url = 'https://example.com/examples'
         
     | 
| 
      
 190 
     | 
    
         
            +
             
     | 
| 
      
 191 
     | 
    
         
            +
            post.to_hash #=> {title: "Ruson", url: "https://example.com/examples" }
         
     | 
| 
      
 192 
     | 
    
         
            +
            ```
         
     | 
| 
      
 193 
     | 
    
         
            +
             
     | 
| 
      
 194 
     | 
    
         
            +
            ### API json parser
         
     | 
| 
      
 195 
     | 
    
         
            +
             
     | 
| 
      
 196 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 197 
     | 
    
         
            +
            class Article < Ruson::Base
         
     | 
| 
      
 198 
     | 
    
         
            +
              field :title
         
     | 
| 
      
 199 
     | 
    
         
            +
              field :description
         
     | 
| 
      
 200 
     | 
    
         
            +
            end
         
     | 
| 
      
 201 
     | 
    
         
            +
             
     | 
| 
      
 202 
     | 
    
         
            +
            conn = Faraday::Connection.new(url: 'https://your.api/articles/1') do |faraday|
         
     | 
| 
      
 203 
     | 
    
         
            +
              faraday.request :url_encoded
         
     | 
| 
      
 204 
     | 
    
         
            +
              faraday.adapter Faraday.default_adapter
         
     | 
| 
      
 205 
     | 
    
         
            +
            end
         
     | 
| 
      
 206 
     | 
    
         
            +
             
     | 
| 
      
 207 
     | 
    
         
            +
            response = conn.get
         
     | 
| 
      
 208 
     | 
    
         
            +
            article = Article.new(response.body)
         
     | 
| 
      
 209 
     | 
    
         
            +
            article.title
         
     | 
| 
      
 210 
     | 
    
         
            +
            ```
         
     | 
| 
       136 
211 
     | 
    
         | 
| 
       137 
212 
     | 
    
         
             
            ## Development
         
     | 
| 
       138 
213 
     | 
    
         | 
    
        data/lib/ruson/base.rb
    CHANGED
    
    | 
         @@ -6,18 +6,40 @@ module Ruson 
     | 
|
| 
       6 
6 
     | 
    
         
             
              class Base
         
     | 
| 
       7 
7 
     | 
    
         
             
                class << self
         
     | 
| 
       8 
8 
     | 
    
         
             
                  def field(attr, options = {})
         
     | 
| 
      
 9 
     | 
    
         
            +
                    instance_eval("attr_accessor :#{attr.to_s}")
         
     | 
| 
       9 
10 
     | 
    
         
             
                    add_accessor attr.to_s, options
         
     | 
| 
       10 
11 
     | 
    
         
             
                  end
         
     | 
| 
       11 
12 
     | 
    
         | 
| 
       12 
     | 
    
         
            -
                  def  
     | 
| 
       13 
     | 
    
         
            -
                     
     | 
| 
       14 
     | 
    
         
            -
                     
     | 
| 
       15 
     | 
    
         
            -
                    @accessors.merge!({ name.to_sym => options })
         
     | 
| 
      
 13 
     | 
    
         
            +
                  def enum(attr, values)
         
     | 
| 
      
 14 
     | 
    
         
            +
                    define_enum_methods attr.to_s, values.map(&:to_sym)
         
     | 
| 
      
 15 
     | 
    
         
            +
                    add_accessor attr.to_s
         
     | 
| 
       16 
16 
     | 
    
         
             
                  end
         
     | 
| 
       17 
17 
     | 
    
         | 
| 
       18 
18 
     | 
    
         
             
                  def accessors
         
     | 
| 
       19 
19 
     | 
    
         
             
                    @accessors
         
     | 
| 
       20 
20 
     | 
    
         
             
                  end
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
                  private
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  def define_enum_methods(name, values)
         
     | 
| 
      
 25 
     | 
    
         
            +
                    instance_eval("attr_reader :#{name}")
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                    define_method "#{name}=" do |v|
         
     | 
| 
      
 28 
     | 
    
         
            +
                      raise ArgumentError, "#{v} is not a valid #{name}" unless values.include? v.to_sym
         
     | 
| 
      
 29 
     | 
    
         
            +
                      eval "@#{name} = :#{v.to_s}"
         
     | 
| 
      
 30 
     | 
    
         
            +
                    end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                    values.each do |v|
         
     | 
| 
      
 33 
     | 
    
         
            +
                      define_method "#{v}?" do
         
     | 
| 
      
 34 
     | 
    
         
            +
                        eval "@#{name} == :#{v.to_s}"
         
     | 
| 
      
 35 
     | 
    
         
            +
                      end
         
     | 
| 
      
 36 
     | 
    
         
            +
                    end
         
     | 
| 
      
 37 
     | 
    
         
            +
                  end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                  def add_accessor(name, options = {})
         
     | 
| 
      
 40 
     | 
    
         
            +
                    @accessors ||= {}
         
     | 
| 
      
 41 
     | 
    
         
            +
                    @accessors.merge!({ name.to_sym => options })
         
     | 
| 
      
 42 
     | 
    
         
            +
                  end
         
     | 
| 
       21 
43 
     | 
    
         
             
                end
         
     | 
| 
       22 
44 
     | 
    
         | 
| 
       23 
45 
     | 
    
         
             
                def initialize(json, root_key: nil)
         
     | 
| 
         @@ -31,6 +53,18 @@ module Ruson 
     | 
|
| 
       31 
53 
     | 
    
         
             
                  end
         
     | 
| 
       32 
54 
     | 
    
         
             
                end
         
     | 
| 
       33 
55 
     | 
    
         | 
| 
      
 56 
     | 
    
         
            +
                def to_hash
         
     | 
| 
      
 57 
     | 
    
         
            +
                  self.class.accessors.keys.inject({}) do |result, key|
         
     | 
| 
      
 58 
     | 
    
         
            +
                    value = send(key)
         
     | 
| 
      
 59 
     | 
    
         
            +
                    result[key.to_sym] = convert_array_to_hash_value(value)
         
     | 
| 
      
 60 
     | 
    
         
            +
                    result
         
     | 
| 
      
 61 
     | 
    
         
            +
                  end
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
                def to_json
         
     | 
| 
      
 65 
     | 
    
         
            +
                  to_hash.to_json
         
     | 
| 
      
 66 
     | 
    
         
            +
                end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
       34 
68 
     | 
    
         
             
                private
         
     | 
| 
       35 
69 
     | 
    
         | 
| 
       36 
70 
     | 
    
         
             
                def set_attribute(attr_name, val)
         
     | 
| 
         @@ -64,5 +98,22 @@ module Ruson 
     | 
|
| 
       64 
98 
     | 
    
         
             
                  return json if json.class == ActiveSupport::HashWithIndifferentAccess
         
     | 
| 
       65 
99 
     | 
    
         
             
                  (json.class == Hash ? json : JSON.parse(json)).with_indifferent_access
         
     | 
| 
       66 
100 
     | 
    
         
             
                end
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                def ruson_class?(value)
         
     | 
| 
      
 103 
     | 
    
         
            +
                  value.class < Ruson::Base
         
     | 
| 
      
 104 
     | 
    
         
            +
                end
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
                def convert_ruson_to_hash_value(value)
         
     | 
| 
      
 107 
     | 
    
         
            +
                  return value.to_hash if ruson_class?(value)
         
     | 
| 
      
 108 
     | 
    
         
            +
                  value
         
     | 
| 
      
 109 
     | 
    
         
            +
                end
         
     | 
| 
      
 110 
     | 
    
         
            +
             
     | 
| 
      
 111 
     | 
    
         
            +
                def convert_array_to_hash_value(value)
         
     | 
| 
      
 112 
     | 
    
         
            +
                  if value.instance_of?(Array)
         
     | 
| 
      
 113 
     | 
    
         
            +
                    value.inject([]) { |result, v| result << convert_ruson_to_hash_value(v) }
         
     | 
| 
      
 114 
     | 
    
         
            +
                  else
         
     | 
| 
      
 115 
     | 
    
         
            +
                    convert_ruson_to_hash_value(value)
         
     | 
| 
      
 116 
     | 
    
         
            +
                  end
         
     | 
| 
      
 117 
     | 
    
         
            +
                end
         
     | 
| 
       67 
118 
     | 
    
         
             
              end
         
     | 
| 
       68 
119 
     | 
    
         
             
            end
         
     | 
    
        data/lib/ruson/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: ruson
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.5
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - klriutsa
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: exe
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2019-04- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2019-04-14 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: bundler
         
     |