3 Replies Latest reply on May 23, 2013 9:21 AM by Peter Arcuri

    Problem with Server sync

      Hi,

      I am having some problem when synchronizing. I want to sync with CRUD. I create rails but my json is differace from example(http://rhostore.herokuapp.com/products.json)

      This is my json

      [{"address":"BT","id":1,"lat":"100.24","long":"31.12","name":"Dan"},{"address":"BT2","id":2,"lat":"12.53","long":"100.1","name":"Lee"}]

       

      Here's my source adapter


      require 'json'

      require 'rest_client'

      class Map < SourceAdapter

        def initialize(source)

          @base = 'http://127.0.0.1:3000/maps'

          super(source)

        end

       

        def login

       

        end

       

        def query()

          parsed=JSON.parse(RestClient.get("#{@base}.json").body)

          @result={}

            if parsed

              parsed.each do |item|

                #key = item["blogs"]["id"].to_s

                #@result[key]=item["blogs"]

                @result[item["id"].to_s] = item

              end

            end

        end

       

        def sync

          super

        end

       

        def create(create_hash, blob=nil)

          result = RestClient.post(@base, :maps => create_hash)

          location = "#{result.headers[:location]}.json"

          new_record = RestClient.get(location).body

          JSON.parse(new_record)["map"]["id"].to_s = '100'

          JSON.parse(RestClient.get("#{result.headers[:location]}.json").body)["id"]

        end

       

        def update(update_hash)

          obj_id = update_hash['id']

          update_hash.delete('id')

          RestClient.put("#{@base}/#{obj_id}",:maps => update_hash)["id"]

        end

       

        def delete(delete_hash)

          obj_id = delete_hash['id']

          RestClient.delete("#{@base}/#{obj_id}")

        end

       

        def logoff

        end

      end

       

      I can query and delete but can't create and update

      Anyone have any idea what could be the problem? I must fix in source adapter or Rails?

      Regards, Sorry for my bad english

        • Re: Problem with Server sync
          Peter Arcuri

          Having a quick look at your source adapter, I see referrence to "maps"

           

          result = RestClient.post(@base, :maps => create_hash)

           

          while yourr source adaptor name and class is "map"?

            • Re: Problem with Server sync

              It' work!! Thank you very much Peter.

              I have a one questions.

              When i add 2 record in device and "Sync". My rails is show only 1 (first record) and log is insert only 1 record too.

              can you explain it for me?

               

              Thank you for advice.

                • Re: Problem with Server sync
                  Peter Arcuri

                  It appears as you have hard coded 100 as the record id. Possibly overriding the previous added record. Below is a sample Create method.

                   

                    def create(create_hash)

                      result = RestClient.post(@base, :map => create_hash)

                      location = "#{result.headers[:location]}.json"

                      new_record = RestClient.get(location).body

                      JSON.parse(new_record)["map"]["id"].to_s

                    end

                   

                  Also note that "id" is handled by the system, it's not same as that in your json file.