4 Replies Latest reply on Mar 27, 2013 10:24 AM by Cristian Quintero

    Problem with tutorial Product Source Adapter

      I am getting communication with the sync server and I am doing rspecs on my source adapter as I cannot get the info passed to the device yet...

       

      This is the json i am getting from my REST service...

       

      {"inventory":[{"image":"2","name":"3","sku":"1"},{"image":"5","name":"6","sku":"40"},{"image":"56","name":"3","sku":"45"},{"image":"9","name":"38","sku":"7"},{"image":"955","name":"3568","sku":"766"},{"image":"9545","name":"35568","sku":"7666"}]}

       

      and I am getting an error in the code of the source adapter of the tutorial... this is what rspecs says...

       

      Failures:

       

       

        1) Inventory it should behave like SpecHelper should process Inventory query

       

           ←[31mFailure/Error:←[0m ←[31m@result[item["inventory"]["sku"]] = item["inv

       

      ntory"]←[0m

       

           ←[31mTypeError:←[0m

       

             ←[31mcan't convert String into Integer←[0m

       

      Cannot understand why is that error... any ideas? I am using exact same code that the source adapter in the tutorial this is my method..

       

      it "should process Inventory query" do

            @base = 'http://10.186.5.247:8080/SyncAgentWS/webresources/synccenter.inventory'

            rest_result = RestClient.get("#{@base}").body         

                    if rest_result.code != 200

      raise SourceAdapterException.new("Error connecting!")

      end

      parsed = JSON.parse(rest_result)

       

                    @result={}

      parsed.each do |item|

                      @result[item["inventory"]["sku"]] = item["inventory"]

      end if parsed

          end

        • Re: Problem with tutorial Product Source Adapter

          Can you print out `rest_result` and paste it here?  It looks like one or more of the attributes in your web service's objects are not strings.

            • Re: Problem with tutorial Product Source Adapter

              Hi Lars,

               

              I am begginer in Rho and Ruby so I don´t know how excatly print rest_result... but this is the JSON that is coming from the REST service... {"inventory":[{"image":"2","name":"3","sku":"1"},{"image":"5","name":"6","sku":"40"},{"image":"56","name":"3","sku":"45"},{"image":"9","name":"38","sku":"7"},{"image":"955","name":"3568","sku":"766"},{"image":"9545","name":"35568","sku":"7666"}]} .. Is that what you want to check?

                • Re: Problem with tutorial Product Source Adapter

                  You're not quite iterating over your data structure correctly:

                   

                  {
                    "inventory": [
                      {
                        "image": "2",
                        "name": "3",
                        "sku": "1"
                      },
                      {
                        "image": "5",
                        "name": "6",
                        "sku": "40"
                      },
                      {
                        "image": "56",
                        "name": "3",
                        "sku": "45"
                      },
                      {
                        "image": "9",
                        "name": "38",
                        "sku": "7"
                      },
                      {
                        "image": "955",
                        "name": "3568",
                        "sku": "766"
                      },
                      {
                        "image": "9545",
                        "name": "35568",
                        "sku": "7666"
                      }
                    ]
                  }
                  

                  If you want to have an object that is identified by the 'sku' and has three attributes (image, name, sku), your looping code could look something like:

                   

                  parsed = JSON.parse(rest_result)
                  parsed = parsed['inventory'] if parsed['inventory'] 
                  @result={}
                  parsed.each do |item|
                    @result[item['sku'].to_s] = item
                  end
                  @result
                  # {
                  #       "45"=>{"image"=>"56", "name"=>"3", "sku"=>"45"},
                  #       "40"=>{"image"=>"5", "name"=>"6", "sku"=>"40"},
                  #       "7666"=>{"image"=>"9545", "name"=>"35568", "sku"=>"7666"},
                  #       "766"=>{"image"=>"955", "name"=>"3568", "sku"=>"766"},
                  #       "7"=>{"image"=>"9", "name"=>"38", "sku"=>"7"},
                  #       "1"=>{"image"=>"2", "name"=>"3", "sku"=>"1"}
                  # }
                  

                   

                  Your original issue is that you have a top-level 'inventory' hash key, so you were just iterating a single object.  You needed to traverse down into the value of the 'inventory' key, which was an array.